Update to allow traits-based local time adjustment

[SVN r17304]
This commit is contained in:
Jeff Garland
2003-02-10 13:23:54 +00:00
parent facde48090
commit 6d3227d5a9
11 changed files with 436 additions and 43 deletions

View File

@@ -13,11 +13,18 @@ main()
using namespace boost::gregorian;
partial_date pd(1,1);
date d = pd.get_date(2000);
partial_date pd1(1,Jan);
date d = pd1.get_date(2000);
check("Partial date getdate", date(2000,1,1) == d);
d = pd.get_date(2001);
d = pd1.get_date(2001);
check("Partial date getdate", date(2001,1,1) == d);
partial_date pd2(1,Feb);
check("Partial date operator==", pd1 == pd1);
check("Partial date operator==", !(pd1 == pd2));
check("Partial date operator==", !(pd2 == pd1));
check("Partial date operator<", !(pd1 < pd1));
check("Partial date operator<", pd1 < pd2);
check("Partial date operator<", !(pd2 < pd1));
typedef boost::date_time::last_kday_of_month<date> lastkday;

View File

@@ -4,9 +4,30 @@
*/
#include "boost/date_time/posix_time/posix_time.hpp"
#include "boost/date_time/dst_rules.hpp"
#include "boost/date_time/local_timezone_defs.hpp"
#include "boost/date_time/testfrmwk.hpp"
// Define dst rule for Paraguay which is transitions forward on Oct 1 and
// back Mar 1
struct paraguay_dst_traits {
typedef boost::gregorian::date::day_type day_type;
typedef boost::gregorian::date::month_type month_type;
typedef boost::date_time::partial_date<boost::gregorian::date> start_rule_functor;
typedef boost::date_time::partial_date<boost::gregorian::date> end_rule_functor;
static day_type start_day() {return 1;}
static month_type start_month() {return boost::date_time::Oct;}
static day_type end_day() {return 1;}
static month_type end_month() {return boost::date_time::Mar;}
static int dst_start_offset_minutes() { return 120;}
static int dst_end_offset_minutes() { return 120; }
static int dst_shift_length_minutes() { return 60; }
};
// see http://www.timeanddate.com/time/aboutdst.html for some info
// also
int
main()
{
@@ -145,9 +166,240 @@ main()
60);
check("check southern boundary-not", a10==boost::date_time::is_not_in_dst);
/******************** post release 1 -- new dst calc engine ********/
typedef boost::date_time::us_dst_trait<date> us_dst_traits;
typedef boost::date_time::dst_calc_engine<date, time_duration, us_dst_traits>
us_dst_calc2;
// us_dst_calc2
check("dst start", us_dst_calc2::local_dst_start_day(2002) == dst_start);
check("dst end", us_dst_calc2::local_dst_end_day(2002) == dst_end);
// std::cout << us_dst_calc2::local_dst_end_day(2002) << std::endl;
check("dst boundary", us_dst_calc2::is_dst_boundary_day(dst_start));
check("dst boundary", us_dst_calc2::is_dst_boundary_day(dst_end));
check("check if time is dst -- not",
us_dst_calc2::local_is_dst(t.date(), t.time_of_day())==boost::date_time::is_not_in_dst);
check("label on dst boundary invalid",
us_dst_calc2::local_is_dst(t3a.date(),t3a.time_of_day())==boost::date_time::invalid_time_label);
check("label on dst boundary invalid",
us_dst_calc2::local_is_dst(t3b.date(),t3b.time_of_day())==boost::date_time::invalid_time_label);
check("check if time is dst -- not",
us_dst_calc2::local_is_dst(t4.date(),t4.time_of_day())==boost::date_time::is_not_in_dst);
check("check if time is dst -- yes",
us_dst_calc2::local_is_dst(t5.date(),t5.time_of_day())==boost::date_time::is_in_dst);
check("check if time is dst -- not",
us_dst_calc2::local_is_dst(t6.date(),t6.time_of_day())==boost::date_time::is_in_dst);
check("check if time is dst -- ambig",
us_dst_calc2::local_is_dst(t7.date(),t7.time_of_day())==boost::date_time::ambiguous);
check("check if time is dst -- ambig",
us_dst_calc2::local_is_dst(t8.date(),t8.time_of_day())==boost::date_time::ambiguous);
check("check if time is dst -- not",
us_dst_calc2::local_is_dst(t9.date(),t9.time_of_day())==boost::date_time::is_not_in_dst);
/******************** post release 1 -- new dst calc engine - eu dst ********/
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;
date eu_dst_start(2002,Mar, 31);
date eu_dst_end(2002,Oct, 27);
ptime eu_invalid1(eu_dst_start, time_duration(2,0,0)); //invalid time label
ptime eu_invalid2(eu_dst_start, time_duration(2,59,59)); //invalid time label
ptime eu_notdst1(eu_dst_start, time_duration(1,59,59)); //not ds
ptime eu_isdst1(eu_dst_start, time_duration(3,0,0)); //always dst
ptime eu_isdst2(eu_dst_end, time_duration(1,59,59)); //is dst
ptime eu_amgbig1(eu_dst_end, time_duration(2,0,0)); //ambiguous
ptime eu_amgbig2(eu_dst_end, time_duration(2,59,59)); //ambiguous
ptime eu_notdst2(eu_dst_end, time_duration(3,0,0)); //always not dst
check("eu dst start", eu_dst_calc::local_dst_start_day(2002) == eu_dst_start);
check("eu dst end", eu_dst_calc::local_dst_end_day(2002) == eu_dst_end);
check("eu dst boundary", eu_dst_calc::is_dst_boundary_day(eu_dst_start));
check("eu dst boundary", eu_dst_calc::is_dst_boundary_day(eu_dst_end));
// on forward shift boundaries
check("eu label on dst boundary invalid",
eu_dst_calc::local_is_dst(eu_invalid1.date(),eu_invalid1.time_of_day())==boost::date_time::invalid_time_label);
check("eu label on dst boundary invalid",
eu_dst_calc::local_is_dst(eu_invalid2.date(),eu_invalid2.time_of_day())==boost::date_time::invalid_time_label);
check("eu check if time is dst -- not",
eu_dst_calc::local_is_dst(eu_notdst1.date(),eu_notdst1.time_of_day())==boost::date_time::is_not_in_dst);
check("check if time is dst -- yes",
eu_dst_calc::local_is_dst(eu_isdst1.date(),eu_isdst1.time_of_day())==boost::date_time::is_in_dst);
//backward shift boundary
check("eu check if time is dst -- yes",
eu_dst_calc::local_is_dst(eu_isdst2.date(),eu_isdst2.time_of_day())==boost::date_time::is_in_dst);
check("eu check if time is dst -- ambig",
eu_dst_calc::local_is_dst(eu_amgbig1.date(),eu_amgbig1.time_of_day())==boost::date_time::ambiguous);
check("eu check if time is dst -- ambig",
eu_dst_calc::local_is_dst(eu_amgbig2.date(),eu_amgbig2.time_of_day())==boost::date_time::ambiguous);
check("eu check if time is dst -- not",
eu_dst_calc::local_is_dst(eu_notdst2.date(),eu_notdst2.time_of_day())==boost::date_time::is_not_in_dst);
/******************** post release 1 -- new dst calc engine - gb dst ********/
/* Several places in Great Britan use eu start and end rules for the
day, but different local conversion times (eg: forward change at 1:00
am local and backward change at 2:00 am dst instead of 2:00am
forward and 3:00am back for the EU).
*/
typedef boost::date_time::uk_dst_trait<date> uk_dst_traits;
typedef boost::date_time::dst_calc_engine<date, time_duration, uk_dst_traits> uk_dst_calc;
date uk_dst_start(2002,Mar, 31);
date uk_dst_end(2002,Oct, 27);
ptime uk_invalid1(uk_dst_start, time_duration(1,0,0)); //invalid time label
ptime uk_invalid2(uk_dst_start, time_duration(1,59,59)); //invalid time label
ptime uk_notdst1(uk_dst_start, time_duration(0,59,59)); //not ds
ptime uk_isdst1(uk_dst_start, time_duration(2,0,0)); //always dst
ptime uk_isdst2(uk_dst_end, time_duration(0,59,59)); //is dst
ptime uk_amgbig1(uk_dst_end, time_duration(1,0,0)); //ambiguous
ptime uk_amgbig2(uk_dst_end, time_duration(1,59,59)); //ambiguous
ptime uk_notdst2(uk_dst_end, time_duration(3,0,0)); //always not dst
check("uk dst start", uk_dst_calc::local_dst_start_day(2002) == uk_dst_start);
check("uk dst end", uk_dst_calc::local_dst_end_day(2002) == uk_dst_end);
check("uk dst boundary", uk_dst_calc::is_dst_boundary_day(uk_dst_start));
check("uk dst boundary", uk_dst_calc::is_dst_boundary_day(uk_dst_end));
// on forward shift boundaries
check("uk label on dst boundary invalid",
uk_dst_calc::local_is_dst(uk_invalid1.date(),uk_invalid1.time_of_day())==boost::date_time::invalid_time_label);
check("uk label on dst boundary invalid",
uk_dst_calc::local_is_dst(uk_invalid2.date(),uk_invalid2.time_of_day())==boost::date_time::invalid_time_label);
check("uk check if time is dst -- not",
uk_dst_calc::local_is_dst(uk_notdst1.date(),uk_notdst1.time_of_day())==boost::date_time::is_not_in_dst);
check("uk check if time is dst -- yes",
uk_dst_calc::local_is_dst(uk_isdst1.date(),uk_isdst1.time_of_day())==boost::date_time::is_in_dst);
//backward shift boundary
check("uk check if time is dst -- yes",
uk_dst_calc::local_is_dst(uk_isdst2.date(),uk_isdst2.time_of_day())==boost::date_time::is_in_dst);
check("uk check if time is dst -- ambig",
uk_dst_calc::local_is_dst(uk_amgbig1.date(),uk_amgbig1.time_of_day())==boost::date_time::ambiguous);
check("uk check if time is dst -- ambig",
uk_dst_calc::local_is_dst(uk_amgbig2.date(),uk_amgbig2.time_of_day())==boost::date_time::ambiguous);
check("uk check if time is dst -- not",
uk_dst_calc::local_is_dst(uk_notdst2.date(),uk_notdst2.time_of_day())==boost::date_time::is_not_in_dst);
// /******************** post release 1 -- new dst calc engine ********/
// //Define dst rule for Paraguay which is transitions forward on Oct 1 and back Mar 1
typedef boost::date_time::dst_calc_engine<date, time_duration,
paraguay_dst_traits> pg_dst_calc;
{
date pg_dst_start(2002,Oct, 1);
date pg_dst_end(2002,Mar, 1);
date pg_indst(2002,Dec, 1);
date pg_notdst(2002,Jul, 1);
ptime pg_invalid1(pg_dst_start, time_duration(2,0,0)); //invalid time label
ptime pg_invalid2(pg_dst_start, time_duration(2,59,59)); //invalid time label
ptime pg_notdst1(pg_dst_start, time_duration(1,59,59)); //not ds
ptime pg_isdst1(pg_dst_start, time_duration(3,0,0)); //always dst
ptime pg_isdst2(pg_dst_end, time_duration(0,59,59)); //is dst
ptime pg_amgbig1(pg_dst_end, time_duration(1,0,0)); //ambiguous
ptime pg_amgbig2(pg_dst_end, time_duration(1,59,59)); //ambiguous
ptime pg_notdst2(pg_dst_end, time_duration(2,0,0)); //always not dst
check("pg dst start", pg_dst_calc::local_dst_start_day(2002) == pg_dst_start);
check("pg dst end", pg_dst_calc::local_dst_end_day(2002) == pg_dst_end);
check("pg dst boundary", pg_dst_calc::is_dst_boundary_day(pg_dst_start));
check("pg dst boundary", pg_dst_calc::is_dst_boundary_day(pg_dst_end));
// on forward shift boundaries
check("pg label on dst boundary invalid",
pg_dst_calc::local_is_dst(pg_invalid1.date(),pg_invalid1.time_of_day())==boost::date_time::invalid_time_label);
check("pg label on dst boundary invalid",
pg_dst_calc::local_is_dst(pg_invalid2.date(),pg_invalid2.time_of_day())==boost::date_time::invalid_time_label);
check("pg check if time is dst -- not",
pg_dst_calc::local_is_dst(pg_notdst1.date(),pg_notdst1.time_of_day())==boost::date_time::is_not_in_dst);
check("check if time is dst -- yes",
pg_dst_calc::local_is_dst(pg_isdst1.date(),pg_isdst1.time_of_day())==boost::date_time::is_in_dst);
//backward shift boundary
check("pg check if time is dst -- yes",
pg_dst_calc::local_is_dst(pg_isdst2.date(),pg_isdst2.time_of_day())==boost::date_time::is_in_dst);
check("pg check if time is dst -- ambig",
pg_dst_calc::local_is_dst(pg_amgbig1.date(),pg_amgbig1.time_of_day())==boost::date_time::ambiguous);
check("pg check if time is dst -- ambig",
pg_dst_calc::local_is_dst(pg_amgbig2.date(),pg_amgbig2.time_of_day())==boost::date_time::ambiguous);
check("pg check if time is dst -- not",
pg_dst_calc::local_is_dst(pg_notdst2.date(),pg_notdst2.time_of_day())==boost::date_time::is_not_in_dst);
// a couple not on the boudnary
check("pg check if time is dst -- yes",
pg_dst_calc::local_is_dst(pg_indst,time_duration(0,0,0))==boost::date_time::is_in_dst);
check("pg check if time is dst -- not",
pg_dst_calc::local_is_dst(pg_notdst,time_duration(0,0,0))==boost::date_time::is_not_in_dst);
}
// /******************** post release 1 -- new dst calc engine ********/
// //Define dst rule for Adelaide australia
typedef boost::date_time::acst_dst_trait<date> acst_dst_traits;
typedef boost::date_time::dst_calc_engine<date, time_duration,
acst_dst_traits> acst_dst_calc;
{
date acst_dst_start(2002,Oct, 27);
date acst_dst_end(2002,Mar, 31);
date acst_indst(2002,Dec, 1);
date acst_notdst(2002,Jul, 1);
ptime acst_invalid1(acst_dst_start, time_duration(2,0,0)); //invalid time label
ptime acst_invalid2(acst_dst_start, time_duration(2,59,59)); //invalid time label
ptime acst_notdst1(acst_dst_start, time_duration(1,59,59)); //not ds
ptime acst_isdst1(acst_dst_start, time_duration(3,0,0)); //always dst
ptime acst_isdst2(acst_dst_end, time_duration(0,59,59)); //is dst
ptime acst_amgbig1(acst_dst_end, time_duration(1,0,0)); //ambiguous
ptime acst_amgbig2(acst_dst_end, time_duration(1,59,59)); //ambiguous
ptime acst_notdst2(acst_dst_end, time_duration(2,0,0)); //always not dst
std::cout << "acst dst_start: " << acst_dst_calc::local_dst_start_day(2002)
<< std::endl;
check("acst dst start", acst_dst_calc::local_dst_start_day(2002) == acst_dst_start);
check("acst dst end", acst_dst_calc::local_dst_end_day(2002) == acst_dst_end);
check("acst dst boundary", acst_dst_calc::is_dst_boundary_day(acst_dst_start));
check("acst dst boundary", acst_dst_calc::is_dst_boundary_day(acst_dst_end));
// on forward shift boundaries
check("acst label on dst boundary invalid",
acst_dst_calc::local_is_dst(acst_invalid1.date(),acst_invalid1.time_of_day())==boost::date_time::invalid_time_label);
check("acst label on dst boundary invalid",
acst_dst_calc::local_is_dst(acst_invalid2.date(),acst_invalid2.time_of_day())==boost::date_time::invalid_time_label);
check("acst check if time is dst -- not",
acst_dst_calc::local_is_dst(acst_notdst1.date(),acst_notdst1.time_of_day())==boost::date_time::is_not_in_dst);
check("check if time is dst -- yes",
acst_dst_calc::local_is_dst(acst_isdst1.date(),acst_isdst1.time_of_day())==boost::date_time::is_in_dst);
//backward shift boundary
check("acst check if time is dst -- yes",
acst_dst_calc::local_is_dst(acst_isdst2.date(),acst_isdst2.time_of_day())==boost::date_time::is_in_dst);
check("acst check if time is dst -- ambig",
acst_dst_calc::local_is_dst(acst_amgbig1.date(),acst_amgbig1.time_of_day())==boost::date_time::ambiguous);
check("acst check if time is dst -- ambig",
acst_dst_calc::local_is_dst(acst_amgbig2.date(),acst_amgbig2.time_of_day())==boost::date_time::ambiguous);
check("acst check if time is dst -- not",
acst_dst_calc::local_is_dst(acst_notdst2.date(),acst_notdst2.time_of_day())==boost::date_time::is_not_in_dst);
// a couple not on the boudnary
check("acst check if time is dst -- yes",
acst_dst_calc::local_is_dst(acst_indst,time_duration(0,0,0))==boost::date_time::is_in_dst);
check("acst check if time is dst -- not",
acst_dst_calc::local_is_dst(acst_notdst,time_duration(0,0,0))==boost::date_time::is_not_in_dst);
ptime utc_t = ptime(acst_dst_start, hours(2)) - time_duration(16,30,0);
std::cout << "UTC date/time of Adelaide switch over: " << utc_t << std::endl;
}
return printTestStats();
printTestStats();
return 0;
}
/*

View File

@@ -5,6 +5,7 @@
#include "boost/date_time/posix_time/posix_time.hpp"
#include "boost/date_time/local_time_adjustor.hpp"
#include "boost/date_time/local_timezone_defs.hpp"
#include "boost/date_time/testfrmwk.hpp"
int
@@ -125,6 +126,19 @@ main()
check("check us_local_adjustor", t7c == t7a);
typedef boost::date_time::us_dst_trait<date> us_dst_traits;
typedef boost::date_time::dst_calc_engine<date, time_duration, us_dst_traits>
us_dst_calc2;
typedef boost::date_time::local_adjustor<ptime, -5, us_dst_calc2> us_eastern3;
{
ptime t7a(date(2002,May,31), hours(17));
ptime t7b = us_eastern3::local_to_utc(t7a);
ptime t7c = us_eastern3::utc_to_local(t7b);
//converted to local then back ot utc
check("check us_local_adjustor3", t7c == t7a);
}
// std::cout << to_simple_string(t7) << " in Arizona is "
// << to_simple_string(t8) << " UTC time "
// << std::endl;