views:

476

answers:

1

Hi all,

I have this routine that calculates the seconds-to-date for a struct tm. On Linux my implementation using mktime works fine, but mktime on windows VS2003/.NET 1.1 returns -1 for pre-epoch datetimes.

How do I calculate meaningful time_t values (i.e.

value + secondsToEpoch == secondsToDatetime

) from a for pre-epoch dates, using ANSI C++ only (no boost, Qt either)?

Thanks for your help.

+1  A: 

Looking at a couple of mktime sources on the net, they all look pretty portable so you ought to be able to grab one and just put it in your source, paying attention to legal requirements, of course.

That said, I think you have to look for the right one. What dates are you working with? If you're working with pre-1970 dates, there's a good chance you're going to need a 64-bit time_t.

There are also some weird calendar changes very early on that might be a concern, e.g., the switch to the Gregorian calendar.

David Norman
could you post some links to mktime sources? Thanks.
andreas buykx
I just googled "mktime source" and came up with things like http://www.raspberryginger.com/jbailey/minix/html/mktime_8c-source.html
David Norman
According to the request, the requirement was to support pre-Epoch dates (i.e. with a negative number). The sample code provided returns an unsigned value, so does not support pre-Epoch dates.
selwyn