DateTime Calculations

Would appreciate any pointers to how I could solve the following problem in Go…

I’m building an application to prepare data prior to being ingested into an analytics cluster. Part of that process requires me to calculate in minutes, hours and days the elapsed time between the event being created and then later closed. I have these values as strings as part of each line, or event.

So it’s trivial to parse each value into a time.Time object then use sub to get a duration which gives me access to mins and hours. So far so easy…

I now need to subtract from this duration the following operational factors:

  • Minutes elapsing over weekends, as that does not count. So how to work out how many, or if at all, Saturdays and Sundays exist inside this duration?

  • A full working day (mon-fri) is between set hours, let’s say 08:00 and 17:00 so any minutes out with those hours must also be subtracted.

I’m veering toward trying to create a slice of time.Time for every day between the start and end time stamps (inclusive) then I could range over them applying the test conditionals and accumulate minutes from each test. Does anyone know how to create a slice of time.Time dates between two time stamps?

I can’t help feel that this is the kind of datetime calculation that there may be a helper package for that I’m missing?

Would be extremely grateful for any suggestions, thanks.

Not quite sue what you’re trying to do - expect it’s more complicated and needs to take into account vacation and holiday days, etc. Don’t know that you need to create a slice of dates; just process the dates in sequence: https://play.golang.org/p/QeHK2QmXfTJ

1 Like

@clbanning
Thank you very much. I was able to use your example code to come up with a solution that scales. Yes, there is quite a bit more to it, but your suggestion unlocked some fresh thinking.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.