l i n u x - u s e r s - g r o u p - o f - d a v i s
L U G O D
 
Next Meeting:
August 5: Social gathering
Next Installfest:
TBD
Latest News:
Jul. 4: July, August and September: Security, Photography and Programming for Kids
Page last updated:
2006 Jun 30 06:23

The following is an archive of a post made to our 'vox-tech mailing list' by one of its subscribers.

Report this post as spam:

(Enter your email address)
Re: [vox-tech] Timezone conversions with zoneinfo
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [vox-tech] Timezone conversions with zoneinfo



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@little-cat-a zmanim-simpletz]$ date -d '3/10/2007 10:00pm'
> Sat Mar 10 22:00:00 EST 2007
> [bloom@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.

Attachment: signature.asc
Description: Digital signature

_______________________________________________
vox-tech mailing list
vox-tech@lists.lugod.org
http://lists.lugod.org/mailman/listinfo/vox-tech


LinkedIn
LUGOD Group on LinkedIn
Sign up for LUGOD event announcements
Your email address:
facebook
LUGOD Group on Facebook
'Like' LUGOD on Facebook:

Hosting provided by:
Sunset Systems
Sunset Systems offers preconfigured Linux systems, remote system administration and custom software development.

LUGOD: Linux Users' Group of Davis
PO Box 2082, Davis, CA 95617
Contact Us

LUGOD is a 501(c)7 non-profit organization
based in Davis, California
and serving the Sacramento area.
"Linux" is a trademark of Linus Torvalds.

Sponsored in part by:
Sunset Systems
Who graciously hosts our website & mailing lists!