Discussion:
CEST timezone conversion, boost date_time
Erik Thiele
2003-10-27 13:36:57 UTC
Permalink
hi.

i am trying to convert between UTC and CEST/CET (Europe/Berlin)

i don't find documentation or suitable functions in the Wiki, the
examples or the documentation.

also i don't understand the sourcecode of the time zone stuff. but i
found that eu_dst_trait exists, but don't know what that is and how to
use it.

any ideas?

cu & thx
erik
--
Erik Thiele
Jeff Garland
2003-10-28 22:47:31 UTC
Permalink
Post by Erik Thiele
hi.
i am trying to convert between UTC and CEST/CET (Europe/Berlin)
i don't find documentation or suitable functions in the Wiki, the
examples or the documentation.
Eric -

The local time conversion code is still experimental which is why it is
undocumented.
Post by Erik Thiele
also i don't understand the sourcecode of the time zone stuff. but i
found that eu_dst_trait exists, but don't know what that is and how
to use it.
any ideas?
You can try something like this:
typedef boost::date_time::eu_dst_trait<date> eu_dst_traits;
typedef boost::date_time::dst_calc_engine<date, time_duration,
eu_dst_traits> eu_dst_calc;
//adjustment from UTC is +2
typedef boost::date_time::local_adjustor<ptime, 2, eu_dst_calc>
eu_cet_adjustor;

ptime t1(...); //construct a 'local time'
ptime t2 = eu_cet_adjustor::local_to_utc(t1);
ptime t3 = eu_cet_adjustor::utc_to_local(t2);

I believe this code works, but do some tests to make sure. You can find
examples of code like this in libs/date_time/test/testlocal_adjustor.cpp and
testdst_rules.cpp.

As I said, this is experimental code and I think this approach has proven to
be rather cumbersome. I'm working on a 'dynamic timezone' class that will
provide the ability to construct zone and offset data at runtime instead of
using traits for timezone configurations. This won't be in the 1.31 release,
however...

Jeff
Erik Thiele
2003-10-29 08:34:55 UTC
Permalink
On Tue, 28 Oct 2003 15:47:31 -0700
Post by Jeff Garland
Post by Erik Thiele
hi.
i am trying to convert between UTC and CEST/CET (Europe/Berlin)
i don't find documentation or suitable functions in the Wiki, the
examples or the documentation.
Eric -
The local time conversion code is still experimental which is why it
is undocumented.
i see :-)

i now use c_local_adjustor, and rewrote my code. it now always uses utc
and converts only in one direction. utc->local. which is what the
c_local_adjustor can do.
Post by Jeff Garland
As I said, this is experimental code and I think this approach has
proven to be rather cumbersome. I'm working on a 'dynamic timezone'
class that will provide the ability to construct zone and offset data
at runtime instead of using traits for timezone configurations. This
won't be in the 1.31 release, however...
something like:

ptime a,b,c,d;
timezone_converter zc ("CET"); // calls setzone("CET");

timezone_converter_result_t result;
zc.local_to_utc(a, b, result); // converts "CET" a to utc b. function return value is "void"

// result is enum "unique", "undefined", "double_possibilities", choose
// better words of course :)

i.e. the hour that does not exist results in b unchanged, but
result==undefined.

the hour that exists twice results in b being the defaulting one of the two (see below) and result==double_possibilities

a normal time does result==unique.

zc.setzone("Europe/Berlin");

the returned value in the conversion routine when there are double_possibilities should be determined by the set timezone as follows:
zc.setzone("CET"); // return the utc corresponding to the given local time in winter time
zc.setzone("CEST"); // return the utc corresponding to the given local time in summer time
but of course in both cases result==double_possibilities.


then of course one could also have shortcuts like:

ptime local_to_utc(const ptime local); // throws exception if "undefined", returns default if "double_possibilities"


********

the utc_to_local function is quite interesting:

void time_converter::utc_to_local(ptime &utc, ptime &localtime, time_zone &adjzone);

see, even though the time_converter has a setzone("CET"); still the adjzone is returned.
this i think could be interesting because:

zc.setzone("CET"); // there is implicit conversion char* -> time_zone
zc.setzone("CEST"); // we only use utc_to_local, so this has the same effect than "CET".
timezone myzone;
zc.utc_to_local(a, b, myzone);
if (myzone == timezone("CET")) cout << "hey it's winter\n";
else if (myzone == timezone("CEST")) cout << "hey, it's summer\n";
else fatal("internal impossibility");

if (myzone.timekind()==time_zone::SUMMERTIME)
if (myzone.timekind()==time_zone::WINTERTIME)
if (myzone.timekind()==time_zone::HIGHSUMMERTIME) // stupid rules ...
if (myzone.timekind()==time_zone::NORMALTIME) // there exists no summer,winter,etc.

******

timezone baba ("CET");
timezone lulu ("Europe/Berlin"); // defaults to default-time, i.e. winter time
timezone kuku ("CEST");
assert (baba==lulu);
assert (lulu != kuku);

******

there are major design questions in timezone stuff.
for example, is CET == CEST.. as you see above i decided they are not equal.


maybe my ideas are of help.


cu & keep up good work
erik
--
Erik Thiele
Loading...