Re: obtaining the time offset from UTC
- From: Dave Thompson <david.thompson1@xxxxxxxxxxxxxxxx>
- Date: Mon, 16 May 2005 04:35:55 GMT
On Tue, 10 May 2005 10:24:39 -0400, Eric Sosman <eric.sosman@xxxxxxx>
wrote:
>
>
> __jakal__ wrote:
> > Hello,
> > I need to find out the time difference between UTC and local time.
> > I am [using (POSIX) struct timeb and ftime()]
> > But the problem is that the timezone value is not adjusted for daylight
> > saving and always returns local time zone offset.
> > My TZ environment variable is set to MET
> > Now the DST rules apply and the offset should be +2 hours
> > When daylight saving is not in effect, then the offset should be +1 hour.
> >
> > Is there any way to find out the correct offset . i.e. it should return +2
> > when DST is in effect and +1 otherwise
>
> First, the C Standard makes no guarantees about how
> good the system's timekeeping is. Some systems keep track
> of time zones and seasonal adjustments, while others do not
> and must have their clocks adjusted manually. (A very few
> systems have no clocks at all.) From C's point of view,
> then, your problem may not be solvable.
>
And if it does, standard C doesn't require it to use env var TZ.
POSIX/SUS does, and provides ftime() and struct timeb, which also
includes member dstflag, which is nonzero when (for a time that) DST
is in effect (assuming that the daylight/summer change is always +1hr,
which is very common but probably not universal).
> However, if you are using a system that keeps track of
> such things, you can determine the current UTC offset with
> something like (error-checking omitted):
>
> time_t now = time(NULL);
> struct tm lcl = *localtime(&now);
> struct tm gmt = *gmtime(&now);
<snip>
> Examine the corresponding elements of `lcl' and `gmt' to
> determine your UTC offset.
Or just look at lcl.tm_isdst to determine if there is _any_
daylight/summer in effect and if so assume +1hr.
These are I believe the only fully-standard-C ways. Note that
localtime(), and its difference from gmtime(), can be applied to times
other than the current time, if you wish.
- David.Thompson1 at worldnet.att.net
.
- References:
- obtaining the time offset from UTC
- From: __jakal__
- Re: obtaining the time offset from UTC
- From: Eric Sosman
- obtaining the time offset from UTC
- Prev by Date: Re: function pointers as function parameters
- Next by Date: Re: Returning results through the function parameter list?
- Previous by thread: Re: obtaining the time offset from UTC
- Next by thread: Re: obtaining the time offset from UTC
- Index(es):
Relevant Pages
|