1

Similar to:

Convert historical time to GMT

This time my scenario is: I need to convert some string times in the format "2011061411322100" into GMT. The problem is that the times are coming from another PC and is a historical time. So I am not getting the times in real time so I cannot simply get the GMT from the local time on the box that my code is running.

The times represent Start and End times. If my code is running during a time change, the time change will bre applied on the remote box where I am getting the times. So if the change happens betwwen the start and end time, then the end time will be offset.

I assume I must first convert to tm:

// add to tm struct
tm tmTime;
tmTime.tm_hour = hour; 
tmTime.tm_min = min; 
tmTime.tm_sec = sec; 
tmTime.tm_mday = day; 
tmTime.tm_mon = (month-1); 
tmTime.tm_year = (year - 1900); 

Then convert to time_t

time_t tmInTime_t = mktime( &tmTime );

Then use gmtime:

struct tm *gmt = gmtime( &tmInTime_t );

This will still cause large delta if time change happens between start and end. How do I fix? Do I need to set .tm_isdst? How do I know what to set to?

Community
  • 1
  • 1
Paligulus
  • 493
  • 1
  • 4
  • 14
  • 2
    Looking at your other question I'm a bit confused: is 2011061411322100 GMT/UTC or local time? Is the other PC always going to be in the same time zone as your host PC? What form do you want the output in - a struct tm with GMT/UTC values? – Alan Stokes Jun 28 '11 at 20:13
  • 2
    The tz database http://en.wikipedia.org/wiki/Tz_database has information on historic time zones; but that only helps if you know the time zone and you know the other PC is obeying the rules. – Alan Stokes Jun 28 '11 at 20:16
  • 1
    could that be used in a c++ system? – Robben_Ford_Fan_boy Jun 28 '11 at 20:33
  • 1
    Is it possible to listen for a Windows Event when the time change occurs? – Paligulus Jun 29 '11 at 10:13

1 Answers1

1

You ABSOLUTELY must know what the reference timezone was on the source data. Simply saying it was local time on the source machine doesn't provide enough info. Consider (for one example out of many): The DST rules changed in the US in 2007, and any date calculations spanning across that range must take that into account. When you start dealing with timezones across political borders things get vastly more complicated.

http://www.boost.org/doc/libs/1_47_0/doc/html/date_time.html is an example of a robust time/date library written in C++ and works on most platforms. It will allow you to perform various 'arithmetic' operations on dates and times. It also includes support for the TZ database, which will allow you to perform operations on datetimes spanning timezone rule changes.

mtdarland
  • 86
  • 3