Re: [dev] golang: time.Tick() and ntp

From: Brandon Mulcahy <brandon_AT_jangler.info>
Date: Wed, 8 Oct 2014 23:09:44 -0400

On Thu, Oct 09, 2014 at 04:17:29AM +0200, Markus Teich wrote:
> Brandon Mulcahy wrote:
> > Is there a reason this wouldn't work?
> >
> > for {
> > // ...
> > time.Now().Format("Mon 02 Ý 15:04:05"),
> > // ...
> > time.Sleep(time.Second)
> > }
>
> The for loop you recommended takes one second plus the time it takes to run
> the remainder of the loop body, while my current implementation fires exactly
> every one second.

Yes, of course there's that issue. On my machine the loop body takes ~6 ms,
meaning a skip every 2-3 minutes.

I think a "correct" solution would be:

    time.Sleep(-time.Since(time.Now().Add(time.Second).Truncate(time.Second)))

Not as elegant as the Ticker channel, but it works for me. :)
Received on Thu Oct 09 2014 - 05:09:44 CEST

This archive was generated by hypermail 2.3.0 : Thu Oct 09 2014 - 05:12:07 CEST