[gambit-list] leap seconds

Marc Feeley feeley at iro.umontreal.ca
Sun Jul 2 03:21:23 EDT 2017


Gambit's current-time procedure uses one of these functions, in order of priority and if available on the OS:

- clock_gettime(CLOCK_REALTIME, ...)
- getclock(...)
- GetSystemTimeAsFileTime(...)
- gettimeofday(...)
- ftime(...)
- time(...)

So on recent versions of linux I'm pretty sure it will use clock_gettime(CLOCK_REALTIME, ...) which is not guaranteed to be monotonic.

There is another source of time which is monotonic and accessible with the procedures ##get-monotonic-time! and ##get-monotonic-time-frequency!.  Here's how they can be used:

  (define (monotonic-time)
    (let ((v (u64vector 0)))
      (##get-monotonic-time! v 0)
      (u64vector-ref v 0)))

  (define (monotonic-time-frequency)
    (let ((v (u64vector 0)))
      (##get-monotonic-time-frequency! v 0)
      (u64vector-ref v 0)))

  (define (linux-seconds-since-epoch)
    (quotient (monotonic-time) (monotonic-time-frequency)))

They are based on the first available of these OS functions:

- mach_absolute_time(...)
- QueryPerformanceCounter(...)
- clock_gettime(CLOCK_MONOTONIC, ...)
- the OS function used by current-time

So on recent linux, clock_gettime(CLOCK_MONOTONIC, ...) will probably be used.  However, the man page says:

       CLOCK_MONOTONIC
              Clock that cannot be set and  represents  monotonic  time
              since some unspecified starting point.  This clock is not
              affected by discontinuous jumps in the system time (e.g.,
              if  the system administrator manually changes the clock),
              but is affected by the incremental adjustments  performed
              by adjtime(3) and NTP.

So you won't get time relative to the epoch.

If you do find what is the correct linux function to use please let me know.

Also, do you know of a way to test if a time source is affected by time adjustments, leap seconds, etc?  This could be added as a unit test.

Marc



> On Jul 1, 2017, at 7:14 PM, Faré <fahree at gmail.com> wrote:
> 
> Dear Gambit list,
> 
> I'm using Gambit on Unix, and I'd like to store some real time data
> with a date stamp.
> (current-time) seems appropriate, but I'm not sure I understand the
> treatment of leap seconds -- particularly leap seconds inserted.
> 
> Is (current-time) output monotonic (assuming a monotonic system
> clock), or will the output go back in time during leap seconds?
> What are proper functions to extract a date from (current-time) ? Is
> it guaranteed to use the Unix Epoch when running on Linux?
> 
> -#f
> _______________________________________________
> Gambit-list mailing list
> Gambit-list at iro.umontreal.ca
> https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list




More information about the Gambit-list mailing list