Thread: strptime and gmtime

  1. #16
    Registered User
    Join Date
    Oct 2013
    Posts
    9
    Quote Originally Posted by Adak View Post
    The full list is here:
    Time zone abbreviations

    The way I'd do this is to prototype a struct above main():
    Code:
    typedef struct TimeZone {
       char zone[12];
       int offset;
    }tz;
    Then load all those time zone abbrev. into a file, and read them one time into an array of this struct you declare in main(). After that, it's just a matter of searching them to find the tz.zone string that matches, and then reading the tz.offset.
    Loading the structure is all well and good and faster then calling gmtime(). The problem comes when the data changes. Probably doesn't happen much. A region no longer follows GMT or additional time is added or removed.

  2. #17
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by wjblack View Post
    Loading the structure is all well and good and faster then calling gmtime(). The problem comes when the data changes. Probably doesn't happen much. A region no longer follows GMT or additional time is added or removed.
    I don't know any server that acts as a source for world time, including all updates. Most of the standards for weights and measures are kept in France. I believe the French keep the official time for the world, as well. Would they keep the official time zones updated? Probably.

    Google around on official time zones and such, and see what you can find.

    If they change their offset amount, then they would need to change their official tz abbreviation - so your program would have no match for that string. I'm thinking that would be your signal to look it up, and get the new data.

    Note: On the tz struct, I would add another (larger) char field so you have a better indication of what the cryptic abbreviations refer to.
    Last edited by Adak; 10-13-2013 at 03:43 PM.

  3. #18
    Registered User
    Join Date
    Oct 2013
    Posts
    9
    Quote Originally Posted by Adak View Post
    The full list is here:
    Time zone abbreviations

    The way I'd do this is to prototype a struct above main():
    Code:
    typedef struct TimeZone {
       char zone[12];
       int offset;
    }tz;
    Then load all those time zone abbrev. into a file, and read them one time into an array of this struct you declare in main(). After that, it's just a matter of searching them to find the tz.zone string that matches, and then reading the tz.offset.
    That's all well and good, but what about when a time zone is changed and/or added. The method proposed would even be faster than calling gmtime().

  4. #19
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    So the search for the new TZ would fail - no abbreviation would match with it. Then you have to d/l the new set of timezone data, from <somewhere, they keep the world standards for timezones, perhaps in France>. Google on that one, because I don't know a source of completely up to date and accurate timezone data (a reference standard).

    There should be one, however.

  5. #20
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    FYI: Are there NOT differences on which day of the Year the daylight vs standard time changes?

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #21
    Registered User
    Join Date
    Oct 2013
    Posts
    9
    Quote Originally Posted by stahta01 View Post
    FYI: Are there NOT differences on which day of the Year the daylight vs standard time changes?

    Tim S.
    There are differences on which day of the year the DST vs standard time starts and ends. I wonder if the C methods are smart enough to figure this out when setting the TZ environment variable?

  7. #22
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I don't see how your compiler would know about recent changes in the world's timezones. I don't believe it knows about DST changes, except through the actual clock changes on the system - which means it wouldn't work unless the timezone was set in the global environment, etc.

    I wonder how international companies deal with this?

  8. #23
    .
    Join Date
    Nov 2003
    Posts
    307
    DST changes are political. Some countries , like in Irsrael, DST may actually be legally mandated after it has already started... Lunar calendars like Hijri are so unrelated to the Gregorian calendar that Saudi Arabia has another, civil, version of the Hijri calendar. I was told the country bailed on DST because it would cause too many issues. True or not - do not know.

    Adak said:
    Afterward, he doesn't need to reset tzset() with his own local time zone, because it was never changed in his environment.

    That would be by far the easiest way to do this.
    I was not clear. My bad. tzset() declares what TZ you want to operate in. %Z in strptime is not required by POSIX to work as you would think unless:
    putenv("TZ=my other timezone") and then tzset() is called. I ran into this in Solaris 9. In modern Linux it seems to work without tzset(). With TZ set to somethingnew.

  9. #24
    Registered User
    Join Date
    Oct 2013
    Posts
    9
    Quote Originally Posted by Adak View Post
    I don't see how your compiler would know about recent changes in the world's timezones. I don't believe it knows about DST changes, except through the actual clock changes on the system - which means it wouldn't work unless the timezone was set in the global environment, etc.

    I wonder how international companies deal with this?
    [LINK]http://www.iana.org/time-zones[/LINK]
    All of my development is on a 64-bit Linux Redhat server. After further investigation, I happen upon the above iana site. It's essentially a timezone database that can be loaded and are loaded on different technologies distributions to control timezones. In addition, the methods we have been mentioning 'setenv()' seems to be pulling from a timezone db on disk when being set. You are right in that the compiler would not know of any recent timezone changes. One would have to subscribe to a site like such for new updates and apply it to their respective system.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strptime Warning
    By TheSquid in forum C Programming
    Replies: 9
    Last Post: 10-06-2009, 08:24 AM
  2. FYI: asctime(gmtime(&mytime)) = crash!
    By anonytmouse in forum C Programming
    Replies: 2
    Last Post: 09-29-2003, 02:24 AM