|
Boost : |
Subject: Re: [boost] [datetime] IANA Time Zone Database
From: Maxim Yegorushkin (maxim.yegorushkin_at_[hidden])
Date: 2012-11-09 05:22:23
On 08/11/12 17:36, Maxim Yegorushkin wrote:
> On 08/11/12 15:15, Artyom Beilis wrote:
>> You can use Boost.Locale that is compiled with ICU backend.
>>
>> ICU supports Time Zone database and you can specify the timezone by
>> its name, parse time or create
>>
>> date_time object and get POSIX (UTC) time form it.
>>
>>
>> Artyom Beilis
>> --------------
>> CppCMS - C++ Web Framework: http://cppcms.com/
>> CppDB - C++ SQL Connectivity: http://cppcms.com/sql/cppdb/
>>> ________________________________
>>> From: Maxim Yegorushkin <maxim.yegorushkin_at_[hidden]>
>>> To: boost_at_[hidden]
>>> Sent: Thursday, November 8, 2012 4:53 PM
>>> Subject: [boost] [datetime] IANA Time Zone Database
>>>
>>> Hi Boost,
>>>
>>> I have files with timestamps in various timezones. I would like to be
>>> able to convert these timestamps, no matter what timezone they are in
>>> (I know the names of the timezones) to UTC timestamps.
>>>
>>> I looked through datatime library documentation and found
>>> date_time_zonespec.csv database which doesn't seem to support the
>>> notion of different rules for the same timezone depending on the
>>> date. For example, Russia used to have summer time but not any more.
>>> Another method seems to be passing a timezone specification string
>>> manually, which requires the user to figure what it should be for a
>>> particular date.
>>>
>>> In other words, given a timestamp and its timezone, I would like to
>>> be able to convert that timestamp into a UTC timestamp using
>>> conversion rules in force at that particular date in the past.
>>> Ideally, this would use IANA Time Zone Database or something similar.
>>> Is there a facility in boost to do just that?
>
> Oh, thanks Artyom.
>
> That is quite a surprising place to find this functionality. Not sure
> why it is not in datetime library.
>
> Anyway, I managed to get a small test working:
>
> #include <iostream>
> #include <sstream>
> #include <boost/locale.hpp>
>
> int main() {
> boost::locale::generator gen;
> std::locale::global(gen("en_GB.UTF-8"));
> std::cout.imbue(std::locale());
>
> boost::locale::date_time d;
> std::istringstream ss("8 Nov 2012 17:00:00");
> ss >> boost::locale::as::time_zone("America/New_York") >> d;
>
> std::cout << boost::locale::as::time_zone("UTC") << d << '\n';
> // prints 8 Nov 2012 22:00:00
> }
>
> Thanks a lot!
Thinking more about it, my local times are coming in as broken down time
representation (year, month, ...), so I won't be dealing with streams at
all.
Is there a way to do timezone conversions without using streams?
Something similar to pytz:
timezone paris("Europe/Paris"), utc("UTC");
date_time t(2012, 10, 9, 10, 19, 0); // no timezone associated
date_time utc_t(paris.localize(t).astimezone(utc));
?
-- Maxim
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk