Hi,

I wrote a library that allows to compute the hour/day/week/month/year 
bounds in the location of a given time.Time value. This deals with wall 
clock, not monotonic clock.
I just read the monotonic clock proposal 
at https://github.com/golang/proposal/blob/master/design/12914-monotonic.md
I fear that my code may be affected by the monotonic clock change.

Here is the critical part for which I would like to be sure that 
computations occurs only on wall clock time, even if given time.Now().

// hourBegin returns the time of the beginning of the hour in the same 
timezone
// This is not always the same as t.Truncate(time.Hour)
func hourBegin(t time.Time) time.Time {
h := t.Hour()
for {
t = t.Add(-durationFromHourBegin(t))
r := t.Add(-time.Nanosecond)
if r.Hour() != h {
return t
}
t = r
}
}

func durationFromHourBegin(t time.Time) time.Duration {
return (time.Duration(t.Minute())*time.Minute +
time.Duration(t.Second())*time.Second +
time.Duration(t.Nanosecond())*time.Nanosecond)
}

 

func hourNext(t time.Time) time.Time {
h := t.Hour()
for {
t = t.Add(time.Hour - durationFromHourBegin(t))
if t.Hour() != h {
return t
}
}
}

 

My use case is only for time Locations in periods where daylight change 
occur only of whole hours, but any suggestion for improving the code to be 
more generic would be welcome.

Olivier.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to