Re: Convert UTC to Local Time?



On Thu, 01 Feb 2007 23:40:08 +0000, Daniel Rock wrote:

Michael B Allen <mba2000@xxxxxxxxxx> wrote:
What is a good method for converting an arbitrary UTC time to local time
adjusted for DST and time zone?

. time_t has no timezone concept. It is the number of seconds since
01/01/1970 00:00 UTC
. mktime() always uses local timezone. You either have to set TZ=UTC before
calling mktime(), or do a manual calculation:

int moff[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
int lyears;

lyears = (year - 1968) / 4;
if(year % 4 == 0 && month < 3) --lyears;

time = ((year - 1970) * 365 + mday - 1 + moff[month - 1] + lyears) * 86400
+ hour * 3600 + minute * 60 + second;

Assumes:
year = 1901..2099 (or 1901..2038, depending on the size of time_t)
month = 1..12
mday = 1..31

This is nuts. It can't be that complicated to convert between UTC and
local time. And I thought the way I did it was complicated.

What's wrong with the way I did it? Is it that my code compute the
wrong value at the turn of a leap year?

Mike

.



Relevant Pages

  • Re: convert time string in UTC to time in local time
    ... I have a time stamp that looks like this (corresponding to UTC time): ... I want to convert it to my local time. ... time zone magic to treat the supplied time as a local time, ... UTC time - this seems to be an artifact of the way mktime is defined ...
    (comp.lang.python)
  • Re: Convert UTC to Local Time?
    ... adjusted for DST and time zone? ... time_t has no timezone concept. ... 01/01/1970 00:00 UTC ... mktime() always uses local timezone. ...
    (comp.unix.programmer)
  • Re: Question about ISO8601 vs. strftime()
    ... online references to ISO8601 just get it wrong? ... that link only shows the form with the UTC ... but not the offset as far as I can tell. ... assumed to be in some local time zone. ...
    (comp.programming)
  • Re: Time Zones as politics
    ... be in a different time zone from his arch-rival, ... we are not permitted to criticize him in any way because after ... UTC +5 AKA Eastern Standard Time. ... Soon upon the establishment of the Greenwhich Meridian as the time ...
    (alt.horology)
  • Re: Received mail timestamp is off by 7 hours
    ... It should be one hour ahead of UTC right now. ... Notice that the timestamp on your local e-mail server corresponds to ... the CMOS real-time clock should be set to ... UTC and fix your time zone to match your location. ...
    (freebsd-questions)