A
Angus Comber
Hello
I need to do this conversion but can't seem to find the function.
Angus
I need to do this conversion but can't seem to find the function.
Angus
Angus Comber said:I need to do this conversion but can't seem to find the function.
Angus said:Hello
I need to do this conversion but can't seem to find the function.
Angus
Richard said:mktime(). You can ignore the resulting time_t in this case - but the
side effect of this call will be that the struct tm is normalised to
2006-06-02. Very handy.
Yes, but related to this question:
I often have data sets where the date & time are given as UTC and not
local time. The elements are given separately, such as 'month' and
'hour', so they are straightforward to put into a struct tm.
Since 'mktime()' is not directly usable as it wants to adjust for the
timezone, is there a quick and handy way to do the conversion which is
also portable and standard-compliant?
(Practical solutions often end-up with some dirty tricks or a fair
amount of code to work around the timezone dependency.)
Angus said:I need to do this conversion but can't seem to find the function.
Some systems, including FreeBSD, have timegm() which is just like
mktime() except it assumes that the broken-down time is in GMT.
(mktime() undoes localtime() and timegm() undoes gmtime()).
timegm() is *NOT* a standard function while the others mentioned in
this paragraph are.
Hacks include determining the time zone offset by:
1. Pick a time near the time of interest, run localtime(), then
mktime() on the result.
2. Use the same time as above, run gmtime(), then mktime() on the
result.
3. Subtract the two time_t's obtained from mktime above in (1) and (2).
This is the time zone offset.
4. Use this to adjust the result of mktime().
I am not sure how well this works around DST changes. It may not provide
consistent results for something like Saudi Arabian solar time.
Gordon said:Some systems, including FreeBSD, have timegm() which is just like
mktime() except it assumes that the broken-down time is in GMT.
I am not sure how well this works around DST changes.
It may not provide
consistent results for something like Saudi Arabian solar time.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.