Converting time historic GMT to local time by specifying a timezone

Discussion in 'C++' started by Tharinda, Aug 25, 2010.

  1. Tharinda

    Tharinda Guest

    Hi,
    I have this problem of converting a set of times (from 2009)
    reported in GMT (you may assume this as UTC) to different local times.
    Further I have to consider the daylight savings settings applicable at
    that time. It seems this is very difficult in c++ since the timezone
    information is taken from the environment variable and I cannot find a
    thread safe way to set the environment variables.

    Is there any way of getting date time converted to local time by
    specifying the GMT and the timezone ?

    something like convert_to_local(tm *pTime, const char *TZ)

    Thanks in advance!

    Tharinda
     
    Tharinda, Aug 25, 2010
    #1
    1. Advertising

  2. Tharinda

    Goran Pusic Guest

    On Aug 25, 3:10 pm, Tharinda <> wrote:
    > Hi,
    >         I have this problem of converting a set of times (from 2009)
    > reported in GMT (you may assume this as UTC) to different local times.
    > Further I have to consider the daylight savings settings applicable at
    > that time. It seems this is very difficult in c++ since the timezone
    > information is taken from the environment variable and I cannot find a
    > thread safe way to set the environment variables.
    >
    > Is there any way of getting date time converted to local time by
    > specifying the GMT and the timezone ?
    >
    > something like convert_to_local(tm *pTime, const char *TZ)


    As far as I know, this is not possible with "standard" C++. I need to
    do this (I need to calculate some future local dates for time zones
    other than one I am running on), and I use system-provided information
    (in my case, Windows). Time zones are less of a problem, but daylight
    saving time is a massive PITA, because the way it's computed is
    __utterly__ crazy (but unfortunately necessary).

    In Windows, DST switch is explainedin terms of "first/last X in Y"
    where X is random day of week, Y is random month. According to
    information found in Windows, some time zones (Egypt, IIRC), do DST
    switch one second before midnight, and there are other sorts of crazy
    dates/hours.

    Then, with later system versions (Vista+), DST switch is year-
    dependent, which only adds to confusion, and in my case simply makes
    me aware that I just can't calculate my dates correctly, cause, what's
    to prevent authorities to change DST switch moment at any time in
    future and invalidate my calculation based on when I think DST switch
    shall occur). Therefore, I put up a warning to users about that.

    I already feel sorry for you (or, if there is some comprehensive time
    library I know not about, perhaps not).

    Goran.
     
    Goran Pusic, Aug 25, 2010
    #2
    1. Advertising

  3. Tharinda

    Tharinda Guest

    On Aug 25, 7:35 pm, Goran Pusic <> wrote:
    > On Aug 25, 3:10 pm, Tharinda <> wrote:
    >
    > > Hi,
    > >         I have this problem of converting a set of times (from 2009)
    > > reported in GMT (you may assume this as UTC) to different local times.
    > > Further I have to consider the daylight savings settings applicable at
    > > that time. It seems this is very difficult in c++ since the timezone
    > > information is taken from the environment variable and I cannot find a
    > > thread safe way to set the environment variables.

    >
    > > Is there any way of getting date time converted to local time by
    > > specifying the GMT and the timezone ?

    >
    > > something like convert_to_local(tm *pTime, const char *TZ)

    >
    > As far as I know, this is not possible with "standard" C++. I need to
    > do this (I need to calculate some future local dates for time zones
    > other than one I am running on), and I use system-provided information
    > (in my case, Windows). Time zones are less of a problem, but daylight
    > saving time is a massive PITA, because the way it's computed is
    > __utterly__ crazy (but unfortunately necessary).
    >
    > In Windows, DST switch is explainedin terms of "first/last X in Y"
    > where X is random day of week, Y is random month. According to
    > information found in Windows, some time zones (Egypt, IIRC), do DST
    > switch one second before midnight, and there are other sorts of crazy
    > dates/hours.
    >
    > Then, with later system versions (Vista+), DST switch is year-
    > dependent, which only adds to confusion, and in my case simply makes
    > me aware that I just can't calculate my dates correctly, cause, what's
    > to prevent authorities to change DST switch moment at any time in
    > future and invalidate my calculation based on when I think DST switch
    > shall occur). Therefore, I put up a warning to users about that.
    >
    > I already feel sorry for you (or, if there is some comprehensive time
    > library I know not about, perhaps not).
    >
    > Goran.


    Thanks Goran for the reply, I am doing this on a solaris box, and I
    have this thing called "tzdatabase" (http://www.twinsun.com/tz/tz-
    link.htm) which is used by the libc to determine the localtimes. The
    problem is the standard methods provided in the time.h header doesn't
    take timezone information as parameters, instead they take it from the
    environment. The sad thing is I can't make the function calls thread
    safe due to this environment variables issue. Only solution that I see
    is going through the localtime function implementation and create my
    own thread safe version (which takes timezone as an input parameter).
    This is very easy to be said than done. ex:-
    http://faculty.qu.edu.qa/rriley/cmpt507/minix/localtime_8c-source.html

    Tharinda
     
    Tharinda, Aug 25, 2010
    #3
  4. Tharinda

    peter koch Guest

    On 25 Aug., 16:35, Goran Pusic <> wrote:
    > On Aug 25, 3:10 pm, Tharinda <> wrote:
    >
    > > Hi,
    > >         I have this problem of converting a set of times (from 2009)
    > > reported in GMT (you may assume this as UTC) to different local times.
    > > Further I have to consider the daylight savings settings applicable at
    > > that time. It seems this is very difficult in c++ since the timezone
    > > information is taken from the environment variable and I cannot find a
    > > thread safe way to set the environment variables.

    >
    > > Is there any way of getting date time converted to local time by
    > > specifying the GMT and the timezone ?

    >
    > > something like convert_to_local(tm *pTime, const char *TZ)

    >
    > As far as I know, this is not possible with "standard" C++. I need to
    > do this (I need to calculate some future local dates for time zones
    > other than one I am running on), and I use system-provided information
    > (in my case, Windows). Time zones are less of a problem, but daylight
    > saving time is a massive PITA, because the way it's computed is
    > __utterly__ crazy (but unfortunately necessary).
    >
    > In Windows, DST switch is explainedin terms of "first/last X in Y"
    > where X is random day of week, Y is random month. According to
    > information found in Windows, some time zones (Egypt, IIRC), do DST
    > switch one second before midnight, and there are other sorts of crazy
    > dates/hours.
    >
    > Then, with later system versions (Vista+), DST switch is year-
    > dependent, which only adds to confusion, and in my case simply makes
    > me aware that I just can't calculate my dates correctly, cause, what's
    > to prevent authorities to change DST switch moment at any time in
    > future and invalidate my calculation based on when I think DST switch
    > shall occur). Therefore, I put up a warning to users about that.
    >
    > I already feel sorry for you (or, if there is some comprehensive time
    > library I know not about, perhaps not).
    >
    > Goran.


    Not only that - you also need knowledge about leap-seconds.

    /Peter
     
    peter koch, Aug 25, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. asaf
    Replies:
    3
    Views:
    6,236
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=
    Sep 11, 2006
  2. ZR

    Convert GMT to Local Time

    ZR, Mar 16, 2007, in forum: C Programming
    Replies:
    2
    Views:
    1,550
  3. Josef Dalcolmo
    Replies:
    1
    Views:
    1,797
    John Machin
    May 3, 2007
  4. David Joseph Bonnici
    Replies:
    1
    Views:
    231
    Gunnar Hjalmarsson
    May 15, 2005
  5. Rohit
    Replies:
    6
    Views:
    210
    Rohit
    Jul 3, 2008
Loading...

Share This Page