FW: tz interface problems

Bradley White bww at fore.com
Fri May 22 15:52:17 UTC 1998


>    The problem, of course, is that sendmail is figuring out the offset by
>    comparing the hours and minutes shown by localtime() and gmtime().
> 
> That method should be reliable, so long as you pass the same timestamp
> to both localtime and gmtime.  Can you give more details about the problem?
> 
>    Do you have a routine that, given a time, returns the offset?
> 
> There's not one in the tz packages, but one is commonly included in GNU code.
> I'll enclose a copy at the end of this message.  Of course the Right Way
> to do it is to use tm_gmtoff, but this isn't blessed by the standards.

> /* Yield A - B, measured in seconds.  */
> time_t
> difftm (a, b)
>      struct tm const *a;
>      struct tm const *b;
> {
	:
> }
> 
> /* Yield the number of seconds that local time is ahead of GMT at
>    time T, or 0 if this is not known.  */
> long
> gmtoff (t)
>      time_t t;
> {
	:
>   return difftm (ltp, &gmt);
> }

If you only need gmtoff() [and not the full generality of difftm()], a
more simple solution that utilizes the fact that localtime() and gmtime()
are never more than one day apart is ...

long
gmtoff(time_t t)
{
    struct tm lcl = *localtime(&t);
    struct tm utc = *gmtime(&t);

    if (lcl.tm_mday != utc.tm_mday)
	(((lcl.tm_mon == utc.tm_mon) == (lcl.tm_mday > utc.tm_mday))
	    ? &lcl : &utc)->tm_hour += 24;
    return ((lcl.tm_hour - utc.tm_hour) * 60) + (lcl.tm_min - utc.tm_min);
}

Bradley



More information about the tz mailing list