Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: Timezone conversion

Reply
Thread Tools

Re: Timezone conversion

 
 
Markus Wichmann
Guest
Posts: n/a
 
      03-29-2012
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.
 
Reply With Quote
 
 
 
 
Ike Naar
Guest
Posts: n/a
 
      03-30-2012
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).
 
Reply With Quote
 
 
 
 
ImpalerCore
Guest
Posts: n/a
 
      03-30-2012
On Mar 30, 5:29*am, Guillaume Dargaud
<use_the_contact_f...@www.gdargaud.net> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Conversion of date from one timezone to another RAMK Java 4 01-31-2012 11:11 AM
TimeZone change - how to use TimeZone class axrock Java 8 03-25-2009 11:54 PM
TimeZone calculation on Windows Vista with DateTime::TimeZone Ami Perl Misc 5 09-24-2007 07:27 AM
Convert windows TimeZone to Java TimeZone asaf Java 3 09-11-2006 05:40 PM
Timezone conversion Bijoy Naick ASP .Net 6 04-18-2005 10:58 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57