[LEAPSECS] Coding this week, and a trick for timeouts over leap seconds.

Hal Murray hmurray at megapathdsl.net
Sun Oct 2 04:21:10 EDT 2011

> Try using clock() instead of gettimeofday_in_millisecs(). The former nicely

> increments with CLOCKS_PER_SEC resolution and is immune from UTC, timezones,

> and leap seconds. At least it does on windows. Can someone comment on unix/

> linux?

My Linux box has a man page that says:
The clock() function returns an approximation of processor time
used by the program.
So I don't think that's interesting for wait-a-while.

select, sleep, and usleep are the wait-a-while routines on POSIX systems. I
don't know how they are actually implemented. I could easily imagine
somebody turning them into wait until now+delta which might get confused by
leap seconds.

unsigned int sleep(unsigned int seconds);
sleep() makes the calling process sleep until seconds seconds
have elapsed or a signal arrives which is not ignored.

int usleep(useconds_t usec);
The usleep() function suspends execution of the calling process
for (at least) usec microseconds. The sleep may be lengthened
slightly by any system activity or by the time spent processing
the call or by the granularity of system timers.

int select(int nfds, fd_set *readfds, fd_set *writefds,
fd_set *exceptfds, struct timeval *timeout);
timeout is an upper bound on the amount of time elapsed before
select() returns. If both fields of the timeval stucture are
zero, then select() returns immediately. (This is useful for
polling.) If timeout is NULL (no timeout), select() can block

If I wanted to test/debug this sort of code, I would use the cycle-counter.
Most modern chips have one. On i386 it's called TSC. It's not "standard" or
portable, but you can hide it in a subroutine if you want to try to make the
code somewhat clean.

Should we setup a web page with a recipe to fake a leap second at midnight
I think it would be something like:
kill ntpd
run a program to tell the kernel leap-tonight
wait till midnight
run a program to tell the kernel no-leap
restart ntpd

I think that needs a simple program to set the leap-tonight bits.

These are my opinions, not necessarily my employer's. I hate spam.

More information about the LEAPSECS mailing list