Nick Keighley wrote:
On 18 Dec, 13:41, Nick Keighley <nick_keighley_ nos...@hotmail. com>
wrote:
>Hi,
this is probably quite easy...
How do I convert a UTC string into a time_t?
eg. "2007-12-18 13:37:26" - a time_t.
Now FAQ 13.3 says use mktime() to convert a struct tm into
a time_t or you have to use some non-standard thing to parse the
string.
Assume I can parse the string and build a struct tm, how can I turn
it
into a time_t?
mktime() works on *local time* so it is going to apply the timezone
change
(the incoming string may have come from another timezone).
to save anyone the trouble of answering it looks like the way to
go is to calculate the difference between local time and UTC
by using difftime() on the results of localtime() and gmtime().
Adjusting the input value in struct tm then using mktime().
difftime() operates on time_t values, not on the
struct tm values produced by localtime() and mktime().
Still, you can inspect the elements of the struct tm
objects to get the offset in minutes.
The tricky part is that the difference varies with,
er, time. You can eliminate the seasonal variation (if
any) by setting the tm_isdst member to zero, but that
still leaves statutory variations unaccounted for. For
example, the Venezuela-to-UTC offset today is not the
same as it was two weeks ago. (True, the computer
systems' time services may not have caught up with the
change yet, but once they do they should give different
UTC offsets for 2007-12-08 and 2007-12-10.)
The only fully-portable approach I can think of is
to use both localtime() and gmtime() on the same time_t
value to get an initial estimate of the offset. Then
build a struct tm with the desired time, fudge it by the
estimated offset, call mktime() to get a time_t, and run
that time_t through gmtime(). If the result matches the
(un-fudged) original, you're done. Otherwise, adjust
the offset and try again.
It's a Darn Shame the standard library does not
include mkgmtime().
--
Er*********@sun .com