[ntp:questions] Converting from Y-m-d h:m:s

Rob nomail at example.com
Sat May 15 08:20:09 UTC 2010

Garrett Wollman <wollman at bimajority.org> wrote:
> IEEE Std.1003.1-2001 (the only version I have conveniently to hand)
> supplies the following definition of "Seconds Since the Epoch":
> 	tm_sec + tm_min*60 + tm_hour*3600 + tm_yday*86400 +
> 	    (tm_year-70)*31536000 + ((tm_year-69)/4)*86400 -
> 	    ((tm_year-1)/100)*86400 + ((tm_year+299)/400)*86400

The problem with that is that he does not have tm_yday available,
so it has to be computed from m and d.
This has to be done carefully.

In the gpsd project, we required this function as well and I went
searching on the internet and found a function that looked simple and
clever (like the above).

But, on Jan 1st 2008 it failed.  The result was off-by-one-day in
leapyears on days before Mar 1st.

Lesson learned again: functions that process dates need to be tested
very throughly, as there is always a high risk of bugs.

You could use the function that is in gpsd today (mkgmtime in gpsutils.c),
but I won't guarantee that there are no more bugs.

More information about the questions mailing list