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

From: Markus Teich <markus.teich_AT_stusta.mhn.de>
Date: Thu, 9 Oct 2014 04:17:29 +0200

Brandon Mulcahy wrote:
> Is there a reason this wouldn't work?
>
> for {
> // ...
> time.Now().Format("Mon 02 Ý 15:04:05"),
> // ...
> time.Sleep(time.Second)
> }

This would work of course, but it leaves one tiny little feature out, which I
would like to have if it is basically free:

Before the loop I synchronize with the whole second:

        // sleep until beginning of next second
        var now = time.Now()
        time.Sleep(now.Truncate(time.Second).Add(time.Second).Sub(now))

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.

In my last version of the dwm status bar written as a very ugly shellscript,
this effect was noticable at least once per minute: The clock would for example
jump from 13:33:36 to 13:33:38. ;(

--Markus
Received on Thu Oct 09 2014 - 04:17:29 CEST

This archive was generated by hypermail 2.3.0 : Thu Oct 09 2014 - 04:24:07 CEST