Re: Convert UTC to Local Time?
- From: Michael B Allen <mba2000@xxxxxxxxxx>
- Date: Thu, 01 Feb 2007 23:04:33 -0500
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
.
- Follow-Ups:
- Re: Convert UTC to Local Time?
- From: Daniel Rock
- Re: Convert UTC to Local Time?
- References:
- Convert UTC to Local Time?
- From: Michael B Allen
- Re: Convert UTC to Local Time?
- From: Daniel Rock
- Convert UTC to Local Time?
- Prev by Date: Re: Move a file content to a varibale
- Next by Date: signal and interupted system call
- Previous by thread: Re: Convert UTC to Local Time?
- Next by thread: Re: Convert UTC to Local Time?
- Index(es):
Relevant Pages
|