Re: obtaining the time offset from UTC



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
.



Relevant Pages

  • Re: obtaining the time offset from UTC
    ... > I need to find out the time difference between UTC and local time. ... > saving and always returns local time zone offset. ... of time zones and seasonal adjustments, ...
    (comp.lang.c)
  • Re: C1X calendar and long time proposals
    ... [from a personal email sent to me] ... The ISO 8601 offset is the ... | thus a given local time represents an earlier UTC. ...
    (comp.std.c)
  • Re: Is this the official clock changing day?
    ... At least Daylight saving Time is good for giving you a chance to get ... For PHP, it gets way more complex than that..... ... This local time is some offset from Universal Time This info is ...
    (alt.home.repair)
  • Re: TOD Clock the same as the BIOS clock in PCs?
    ... Hardware clock is set to UTC and the Displayed time (the desktop ... If you really don't care that your time is 100% accurate you can set your TOD clock to your local time and then set a local offset to zero. ...
    (bit.listserv.ibm-main)
  • Re: Kind of OT : GMT timezones in linux
    ... UTC = Athens time - 2h ... If you spezify a positive offset this means local time is 2 hours after UTC, ...
    (comp.protocols.time.ntp)