[vox-tech] Timezone conversions with zoneinfo

Ken Bloom kbloom at gmail.com
Thu Jun 29 20:11:24 PDT 2006


On Thu, Jun 29, 2006 at 06:23:07PM -0500, Ken Bloom wrote:
> I'm trying to do timezone conversions from UTC using the zoneinfo
> database, by using the following routine:
> 
> date convert_to_tz(date theday, double thetime, std::string timezone){
>    struct tm tm;
>    time_t timenum;
> 
>    //mktime has the side effect of normalizing tm
>    tm.tm_min=tm.tm_hour=0;
>    tm.tm_sec=static_cast<int>(thetime*60*60);
>    tm.tm_mon=theday.month()-1;
>    tm.tm_mday=theday.day();
>    tm.tm_year=theday.year()-1900;
> 
>    //Get timenum for the UTC time.
>    setenv("TZ","UTC",1);
>    tzset();
>    timenum=mktime(&tm);
> 
>    //Convert back to the requested local timezone
>    setenv("TZ",timezone.c_str(),1);
>    tzset();
>    localtime_r(&timenum,&tm);
>    
>    //return a value
>    date returnDate(tm.tm_mon+1,tm.tm_mday,tm.tm_year+1900);
>    returnDate.hour(tm.tm_hour);
>    returnDate.min(tm.tm_min);
>    return returnDate;
> }
> 
> date is a class I wrote for handling dates. thetime is a double
> representing a fractional number of hours. (I've been using
> approximately 10 and 25 for this number)
> 
> The code behaves correctly for the DST change between 4/1/2006 and
> 4/2/2006, but the upcoming DST change between 3/10/2007 and 3/11/2007
> is being encountered a day early (i.e. between 3/9/2007 and
> 3/10/2007).
> 
> However the date(1) gets the time change right.
> 
> [bloom at little-cat-a zmanim-simpletz]$ date -d '3/10/2007 10:00pm'
> Sat Mar 10 22:00:00 EST 2007
> [bloom at little-cat-a zmanim-simpletz]$ date -d '3/11/2007 10:00pm'
> Sun Mar 11 22:00:00 EDT 2007
> 
> Any idea what's wrong with my code? Is it my code at all? Is there
> someone appropriate to ask whether it's a system bug?
> 
> --Ken Bloom

Fixed the bug. It wasn't the timezone conversion, it was the number
"thetime" that I was feeding to the conversion. The number was the
slightly buggy result of a sunrise/sunset calculation which gave me
sane numbers in April, but not in March.

-- 
I usually have a GPG digital signature included as an attachment.
See http://www.gnupg.org/ for info about these digital signatures.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://localhost.localdomain/pipermail/vox-tech/attachments/20060629/fb506b4a/attachment.pgp


More information about the vox-tech mailing list