Re: Timezone conversion

Discussion in 'C Programming' started by Markus Wichmann, Mar 29, 2012.

  1. On 28.03.2012 17:42, Guillaume Dargaud wrote:
    > Hello all,
    > Given a date/time string and a time zone, such as
    > 2012/03/28 14:26:19 UTC
    >
    > I would like to convert it to another time zone, in this case:
    > 2012/03/28 16:26:19 CEST
    >
    > How can I do that in standard C, taking into account daylight savings ?
    >
    > So that the winter time
    > 2012/01/01 00:00:00 UTC
    > becomes
    > 2012/01/01 01:00:00 CEST
    >
    > Is it enough to play with gmtime/localtime knowing that the PC that does the
    > conversion is in CEST ? I assume the system knows when daylight savings
    > start/end and that C uses it, right ?
    >
    > I start by filling the fields of a struct tm but then there's no way to
    > specify a timezone when calling mktime...
    >
    > The only mention of timezone I see in the reference are %z in strftime and
    > localtime/gmtime to go the other way. Am I missing something ?


    The question is what you want to do. In standard C, I don't think you
    can select the timezone to convert to. At least I don't see a way to do
    it. The standard doesn't say how localtime() should determine the
    timezone for instance, or how strftime() should do it for %z or %Z. It
    _does_ specify, that the locale category LC_TIME shall affect
    strftime(), but not in what way, and anyway, there are no standardized
    locales beyond "C" and "" (which is local).

    The only thing you can do, at least without too much work, is parse the
    time string (should be possible with sscanf()) and interpret any
    timezone other than UTC or GMT as local time, then display the other one.

    If you care to work a bit harder, you could create an array of
    timezones, that is, their names and offsets and look it up in there. The
    best course of action is probably to only store UTC times.

    HTH,
    Markus

    BTW: CEST is UTC+2. CET is UTC+1.
    Markus Wichmann, Mar 29, 2012
    #1
    1. Advertising

  2. Markus Wichmann

    Ike Naar Guest

    On 2012-03-30, Guillaume Dargaud <> wrote:
    > If you are using daylight savings and convert a date 6 months in the past,
    > it'll still use daylight savings for the conversion. That's so... 1970.


    If you're in France, then daylight savings _was_ actually
    in use six months ago (2011-09-30).
    Ike Naar, Mar 30, 2012
    #2
    1. Advertising

  3. Markus Wichmann

    ImpalerCore Guest

    On Mar 30, 5:29 am, Guillaume Dargaud
    <> wrote:
    > > If you care to work a bit harder, you could create an array of
    > > timezones, that is, their names and offsets and look it up in there. The
    > > best course of action is probably to only store UTC times.

    >
    > Thanks for the info. I'll be going this way as it's the simplest and most
    > reliable.
    >
    > I'm just really surprised that there seems to only be a way, in std C, to
    > convert between UTC and the _current_ local time of the running executable.
    > And not between any other timezone or different dates.
    > If you are using daylight savings and convert a date 6 months in the past,
    > it'll still use daylight savings for the conversion. That's so... 1970.


    That's why you convert localtime to UTC before doing any time
    arithmetic. Convert localtime to UTC, subtract 6 months, and convert
    it back to localtime at the end. If you have your DST rules setup,
    the conversion to localtime should recognize whether the time 6 months
    in the past is in daylight savings or not and make the adjustment.

    If you're really interested, the Boost C++ datetime project has some
    timezone stuff that you could retrofit into a C API.

    Best regards,
    John D.
    ImpalerCore, Mar 30, 2012
    #3
    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. Bijoy Naick

    Timezone conversion

    Bijoy Naick, Dec 17, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    6,319
    =?Utf-8?B?QnJldHQgRWRtYW4=?=
    Apr 18, 2005
  2. RAMK
    Replies:
    4
    Views:
    55,851
    juan.farre
    Jan 31, 2012
  3. asaf
    Replies:
    3
    Views:
    6,136
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=
    Sep 11, 2006
  4. axrock
    Replies:
    8
    Views:
    939
    Roedy Green
    Mar 25, 2009
  5. Ami
    Replies:
    5
    Views:
    266
Loading...

Share This Page