I don't think the behavior of that code changes. (t.Hour, etc operate
on wall time; t.Add operates on both wall and monotonic time.)

If I wanted to truncate times to hours, though, I would write code like this:

https://play.golang.org/p/el8hUeXpWK

On Mon, Feb 27, 2017 at 3:25 AM, Olivier Mengué
<olivier.men...@gmail.com> wrote:
> 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.

-- 
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