tags:

views:

915

answers:

2

I'm trying to convert a time info I reveive as a UTC string to a timestamp using std::mktime in C++. My problem is that in / time.h there is no function to convert to UTC, mktime will only return the timestamp as local time.

So I need to figure out the timezone offset and take it into account, but I don't find a plattform independent way that doesn't involve porting the whole code to boost::date_time. Is there some easy solution which I have overlooked?

+1  A: 

mktime assumes that the date value is in the local time zone. Thus you can change the timezone environment variable beforehand (setenv) and get the UTC timezone.

Windows tzset

Can also try looking at various home-made utc-mktimes, mktime-utcs, etc.

Anonymous
Thanks! I saw something like this in a google result, but does it work on windows?
VolkA
Yep, that solves it. Thanks!
VolkA
+1  A: 

The tm structure used by mktime has a timezone field.
What happens if you put 'UTC' into the timzone field?

http://www.delorie.com/gnu/docs/glibc/libc_435.html

Martin York
Yes, that looks easy for linux, but Microsoft doesn't seam to agree - they provide a _mkgmtime function instead ...
VolkA