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.
|