testdst_rules.cpp

来自「Boost provides free peer-reviewed portab」· C++ 代码 · 共 452 行 · 第 1/2 页

CPP
452
字号
/* Copyright (c) 2002,2003 CrystalClear Software, Inc. * Use, modification and distribution is subject to the  * Boost Software License, Version 1.0. (See accompanying * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) * Author: Jeff Garland  */#include "boost/date_time/posix_time/posix_time.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 1struct paraguay_dst_traits {   typedef boost::gregorian::date           date_type;  typedef boost::gregorian::date::day_type day_type;  typedef boost::gregorian::date::month_type month_type;  typedef boost::gregorian::date::year_type year_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(year_type) {return 1;}  static month_type start_month(year_type) {return boost::date_time::Oct;}  static day_type end_day(year_type) {return 1;}  static month_type end_month(year_type) {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; }  static date_type local_dst_start_day(year_type year)  {    start_rule_functor start(start_day(year),                              start_month(year));    return start.get_date(year);        }  static date_type local_dst_end_day(year_type year)  {    end_rule_functor end(end_day(year),                          end_month(year));    return end.get_date(year);        }};// see http://www.timeanddate.com/time/aboutdst.html for some info// also intmain() {  using namespace boost::posix_time;  using namespace boost::gregorian;  date d(2002,Feb,1);  ptime t(d);  //The following defines the US dst boundaries, except that the  //start and end dates are hard coded.  typedef boost::date_time::us_dst_rules<date, time_duration, 120, 60> us_dst;  date dst_start(2002,Apr, 7);  date dst_end(2002,Oct, 27);  ptime t3a(dst_start, time_duration(2,0,0));   //invalid time label   ptime t3b(dst_start, time_duration(2,59,59)); //invalid time label  ptime t4(dst_start, time_duration(1,59,59));   //not ds   ptime t5(dst_start, time_duration(3,0,0));   //always dst  ptime t6(dst_end, time_duration(0,59,59)); //is dst  ptime t7(dst_end, time_duration(1,0,0));   //ambiguous  ptime t8(dst_end, time_duration(1,59,59));   //ambiguous   ptime t9(dst_end, time_duration(2,0,0));   //always not dst  check("dst start", us_dst::local_dst_start_day(2002) == dst_start);  check("dst end",   us_dst::local_dst_end_day(2002) == dst_end);  check("dst boundary",   us_dst::is_dst_boundary_day(dst_start));  check("dst boundary",   us_dst::is_dst_boundary_day(dst_end));  check("check if time is dst -- not",           us_dst::local_is_dst(t.date(), t.time_of_day())==boost::date_time::is_not_in_dst);  check("label on dst boundary invalid",         us_dst::local_is_dst(t3a.date(),t3a.time_of_day())==boost::date_time::invalid_time_label);  check("label on dst boundary invalid",           us_dst::local_is_dst(t3b.date(),t3b.time_of_day())==boost::date_time::invalid_time_label);   check("check if time is dst -- not",            us_dst::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::local_is_dst(t5.date(),t5.time_of_day())==boost::date_time::is_in_dst);   check("check if time is dst -- not",            us_dst::local_is_dst(t6.date(),t6.time_of_day())==boost::date_time::is_in_dst);   check("check if time is dst -- ambig",          us_dst::local_is_dst(t7.date(),t7.time_of_day())==boost::date_time::ambiguous);   check("check if time is dst -- ambig",          us_dst::local_is_dst(t8.date(),t8.time_of_day())==boost::date_time::ambiguous);   check("check if time is dst -- not",            us_dst::local_is_dst(t9.date(),t9.time_of_day())==boost::date_time::is_not_in_dst);  //Now try a local without dst  typedef boost::date_time::null_dst_rules<date, time_duration> no_dst_adj;  check("check null dst rules",           no_dst_adj::local_is_dst(t4.date(),t4.time_of_day())==boost::date_time::is_not_in_dst);  check("check null dst rules",           no_dst_adj::local_is_dst(t5.date(),t5.time_of_day())==boost::date_time::is_not_in_dst);  check("check null dst rules",           no_dst_adj::utc_is_dst(t4.date(),t4.time_of_day())==boost::date_time::is_not_in_dst);  check("check null dst rules",           no_dst_adj::utc_is_dst(t5.date(),t5.time_of_day())==boost::date_time::is_not_in_dst);    //Try a southern hemisphere adjustment calculation  //This is following the rules for South Australia as best I can  //decipher them.  Basically conversion to DST is last Sunday in  //October 02:00:00 and conversion off of dst is last sunday in  //March 02:00:00.    //This stuff uses the dst calculator directly...  date dst_start2(2002,Oct,27); //last Sunday in Oct  date dst_end2(2002,Mar,31); //last Sunday in March  typedef boost::date_time::dst_calculator<date,time_duration> dstcalc;  //clearly not in dst  boost::date_time::time_is_dst_result a1 =    dstcalc::local_is_dst(date(2002,May,1),hours(3),                          dst_start2, 120,                          dst_end2, 180,                          60);  check("check southern not dst",  a1==boost::date_time::is_not_in_dst);  boost::date_time::time_is_dst_result a2 =    dstcalc::local_is_dst(date(2002,Jan,1),hours(3),                          dst_start2, 120,                          dst_end2, 180,                          60);  check("check southern is dst",  a2==boost::date_time::is_in_dst);  boost::date_time::time_is_dst_result a3 =    dstcalc::local_is_dst(date(2002,Oct,28),hours(3),                          dst_start2, 120,                          dst_end2, 180,                          60);  check("check southern is dst",  a3==boost::date_time::is_in_dst);  boost::date_time::time_is_dst_result a4 =    dstcalc::local_is_dst(date(2002,Oct,27),time_duration(1,59,59),                          dst_start2, 120,                          dst_end2, 180,                          60);  check("check southern boundary-not dst",  a4==boost::date_time::is_not_in_dst);  boost::date_time::time_is_dst_result a5 =    dstcalc::local_is_dst(date(2002,Oct,27),hours(3),                          dst_start2, 120,                          dst_end2, 180,                          60);  check("check southern boundary-is dst",  a5==boost::date_time::is_in_dst);  boost::date_time::time_is_dst_result a6 =    dstcalc::local_is_dst(date(2002,Oct,27),hours(2),                          dst_start2, 120,                          dst_end2, 180,                          60);  check("check southern boundary-invalid time",  a6==boost::date_time::invalid_time_label);  boost::date_time::time_is_dst_result a7 =    dstcalc::local_is_dst(date(2002,Mar,31),time_duration(1,59,59),                          dst_start2, 120,                          dst_end2, 180,                          60);  check("check southern boundary-is dst",  a7==boost::date_time::is_in_dst);  boost::date_time::time_is_dst_result a8 =    dstcalc::local_is_dst(date(2002,Mar,31),time_duration(2,0,0),                          dst_start2, 120,                          dst_end2, 180,                          60);  check("check southern boundary-ambiguous",  a8==boost::date_time::ambiguous);  boost::date_time::time_is_dst_result a9 =    dstcalc::local_is_dst(date(2002,Mar,31),time_duration(2,59,59),                          dst_start2, 120,                          dst_end2, 180,                          60);  check("check southern boundary-ambiguous",  a9==boost::date_time::ambiguous);  boost::date_time::time_is_dst_result a10 =    dstcalc::local_is_dst(date(2002,Mar,31),time_duration(3,0,0),                          dst_start2, 120,                          dst_end2, 180,                          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);  }  {    //some new checks for the new 2007 us dst rules    date dst_start07(2007,Mar, 11);    date dst_end07(2007,Nov, 4);    check("dst start07", us_dst_calc2::local_dst_start_day(2007) == dst_start07);    check("dst end07",   us_dst_calc2::local_dst_end_day(2007) == dst_end07);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?