I have seen a lot of examples of converting UTC into local time, but
none for the other way around.

I have written a log file converter that needs to convert between 2
different formats. The first file format has its times specified as
local time, and the second has times specified as UTC.

As yet, I have the date/time as a DATE, a SYSTEMTIME, a time_t and a
struct tm, the contents of all of them are specified in local time.
None of these formats seem to have a field or way of specifiying a time
zone in them.

I thought I could simply use the gmtime(time_t) function to perform the
conversion. But it just gave out the same number as I gave in, so I am
not sure what the point is.

Then I read about the _tzset() function that prepares the time zone
variables for the converter functions. However calling this function
(before calling gmtime) has no effect. It appears from the gmtime
documentation, that this function always assumes the input is in UTC
which seems strange to me, as thats what its output is supposed to be.

So I am looking for a conversion function that converts from local time
to UTC.

Any ideas? Thanks.

Re: Converting local times to UTC by David

David
Mon Jul 25 06:20:51 CDT 2005

kza wrote:
> So I am looking for a conversion function that converts from local time
> to UTC.
>
> Any ideas? Thanks.
>
TzSpecificLocalTimeToSystemTime

http://msdn.microsoft.com/library/en-us/sysinfo/base/tzspecificlocaltimetosystemtime.asp

HTH,

David

Re: Converting local times to UTC by Sten

Sten
Mon Jul 25 06:17:23 CDT 2005


"kza" <kzawah@gmail.com> wrote in message
news:1122289358.689227.312740@o13g2000cwo.googlegroups.com...
> I have seen a lot of examples of converting UTC into local time, but
> none for the other way around.
>
> I have written a log file converter that needs to convert between 2
> different formats. The first file format has its times specified as
> local time, and the second has times specified as UTC.
>
> As yet, I have the date/time as a DATE, a SYSTEMTIME, a time_t and a
> struct tm, the contents of all of them are specified in local time.
> None of these formats seem to have a field or way of specifiying a time
> zone in them.
Of course not as they are all dependent on the source of the time... the
value can even be invalid...

> I thought I could simply use the gmtime(time_t) function to perform the
> conversion. But it just gave out the same number as I gave in, so I am
> not sure what the point is.
gmtime converts a "number of seconds since startpoint" to the date and
UTC time. It doesn't give out a number but a pointer to a struct.

> Then I read about the _tzset() function that prepares the time zone
> variables for the converter functions. However calling this function
> (before calling gmtime) has no effect. It appears from the gmtime
> documentation, that this function always assumes the input is in UTC
> which seems strange to me, as thats what its output is supposed to be.
>
> So I am looking for a conversion function that converts from local time
> to UTC.
>
> Any ideas? Thanks.
Timezone is automatic in Windows environment. If for some reason you
need to change it i guess _tzset() should work...

You have some options to translate a local time to UTC.

Firstly you could just add or subtract the number of seconds the timezone
relates to from the time_t value or similar count from other values.

Secondly you could probably utilize mktime() and localtime().

Generally you should keep data in UTC time and just display
it in local time if needed using localtime() or asctime().

- Sten