As you may know there is such thing as a leap second, and Dec 31 2008 is a day with a leap second. Couple of hours before New Year I got the idea to catch this leap second.

The problem is that Linux uses Unix time and thus ignores leap seconds. So if you use time(2) or gettimeofday(2), the leap second is just a repetition of the previous second. Here’s the program I used to catch the leap second:

#include <stdio.h>
#include <time.h>
#include <unistd.h>
int main()
{
    time_t t1, t2 = 0;
    char *date;
    for (;;) {
        t1 = time(NULL);
        if (t1 <= t2) {
            printf("Caught it! This is a leap second!\n");
        }
        date = asctime(gmtime(&t1));
        printf("%lu  %s", t1, date);
        usleep(510000);
        t2 = t1;
        t1 = time(NULL);
        if (t1 == t2) {
            usleep(510000);
        }
        fflush(stdout);
    }
    return 0;
}

And here’s the output of this program:

1230767995  Wed Dec 31 23:59:55 2008
1230767996  Wed Dec 31 23:59:56 2008
1230767997  Wed Dec 31 23:59:57 2008
1230767998  Wed Dec 31 23:59:58 2008
1230767999  Wed Dec 31 23:59:59 2008
Caught it! This is a leap second!
1230767999  Wed Dec 31 23:59:59 2008
1230768000  Thu Jan  1 00:00:00 2009
1230768001  Thu Jan  1 00:00:01 2009
1230768002  Thu Jan  1 00:00:02 2009
1230768003  Thu Jan  1 00:00:03 2009
1230768004  Thu Jan  1 00:00:04 2009
1230768005  Thu Jan  1 00:00:05 2009