📄 testlocal_time.cpp
字号:
}catch(std::exception& e){
check(std::string("Caught unexpected exception ") + e.what(), false);
}
try{
local_date_time blt(date(2004,Apr,4), time_duration(2,30,0), ny_tz, true);
check("Did not catch expected exception (Invalid_Time_Label)", false);
}catch(time_label_invalid& e){
check(std::string("Caught expected exception (Invalid_Time_Label) ") + e.what(), true);
}catch(std::exception& e){
check(std::string("Caught unexpected exception ") + e.what(), false);
}
// thorough is_dst() tests, tests againts null_tz and non dst tz are
// done where those local times were tested
{
date d(2004,Apr,4);
time_duration td(1,15,0); // local
local_date_time lt1(d,td,ny_tz,false);
local_date_time lt2(ptime(d,time_duration(6,15,0)), ny_tz);
check("are local_times equal", lt1.utc_time() == lt2.utc_time());
check("is_dst - transition in 1", lt1.is_dst() == false);
check("is_dst - transition in 2", lt2.is_dst() == false);
lt1 += hours(1);
lt2 += hours(1);
check("is_dst - transition in 1", lt1.is_dst() == true);
check("is_dst - transition in 2", lt2.is_dst() == true);
}
{
date d(2004,Oct,31);
time_duration td(1,15,0); // local
local_date_time lt1(d,td,ny_tz,true);
/*try{
//local_date_time lt1(d,td,ny_tz,false);
local_date_time lt1(d,td,ny_tz,true);
std::cout << "no exception thrown" << std::endl;
}catch(time_label_invalid& e){
std::cout << "caught: " << e.what() << std::endl;
}*/
local_date_time lt2(ptime(d,time_duration(5,15,0)), ny_tz);
check("are local_times equal", lt1.utc_time() == lt2.utc_time());
check("is_dst - transition out 1", lt1.is_dst() == true);
check("is_dst - transition out 2", lt2.is_dst() == true);
lt1 += hours(1);
lt2 += hours(1);
check("is_dst - transition out 1", lt1.is_dst() == false);
check("is_dst - transition out 2", lt2.is_dst() == false);
}
{ // southern hemisphere
date d(2004,Oct,31);
time_duration td(1,15,0); // local
local_date_time lt1(d,td,sydney,false);
check("is_dst - transition in (sydney)", lt1.is_dst() == false);
lt1 += hours(1);
check("is_dst - transition in (sydney)", lt1.is_dst() == true);
}
{
date d(2004,Mar,28);
time_duration td(2,15,0); // local; sydney has a weird trans time
local_date_time lt1(d,td,sydney,true);
check("is_dst - transition out (sydney)", lt1.is_dst() == true);
lt1 += hours(1);
check("is_dst - transition out (sydney)", lt1.is_dst() == false);
}
std::cout << "\nTest conversion of time zone from Arizona to New York" << std::endl;
local_date_time ny_time = az_time.local_time_in(ny_tz);
check("Zone abbreviation", ny_time.zone()->std_zone_abbrev() == std::string("EST"));
check("base offset", ny_time.zone()->base_utc_offset() == hours(-5));
check("base offset", ny_time.zone()->has_dst() == true);
check("to_string: " + ny_time.to_string(),
ny_time.to_string() == "2003-Dec-20 07:00:00 EST");
ny_time += hours(3);
check("to_string after add 3 hours: " + ny_time.to_string(),
ny_time.to_string() == "2003-Dec-20 10:00:00 EST");
ny_time += days(3);
check("to_string after add 3 days: " + ny_time.to_string(),
ny_time.to_string() == "2003-Dec-23 10:00:00 EST");
{ // test comparisons & math operations
date d(2003, Aug, 28);
ptime sv_pt(pos_infin);
local_date_time sv_lt(sv_pt, ny_tz);
ptime utc_pt(d, hours(12));
// all 4 of the following local times happen at the same instant
// so they are all equal
local_date_time utc_lt(utc_pt, null_tz); // noon in utc
local_date_time az_lt(d, hours(5), az_tz, false); // 5am local std
local_date_time ny_lt(d, hours(8), ny_tz, true); // 8am local dst
local_date_time au_lt(d, hours(22), sydney, false);// 10pm local std
check("local_date_time to tm",
std::string("4 239 08/28/2003 05:00:00 STD") == tm_out(to_tm(az_lt)));
check("local_date_time to tm",
std::string("4 239 08/28/2003 08:00:00 DST") == tm_out(to_tm(ny_lt)));
check("local_date_time to tm",
std::string("4 239 08/28/2003 22:00:00 STD") == tm_out(to_tm(au_lt)));
try{
local_date_time ldt(not_a_date_time);
tm ldt_tm = to_tm(ldt);
check("Exception not thrown (special_value to_tm)", false);
}catch(std::out_of_range e){
check("Caught expected exception (special_value to_tm)", true);
}catch(...){
check("Caught un-expected exception (special_value to_tm)", false);
}
// check that all are equal to sv_pt
check("local == utc", az_lt == utc_lt);
check("local == utc", ny_lt == utc_lt);
check("local == utc", au_lt == utc_lt);
check("local <= utc", au_lt <= utc_lt);
check("local >= utc", au_lt >= utc_lt);
check("local == local", az_lt == ny_lt);
check("local < local", az_lt < ny_lt+seconds(1));
check("local > local", az_lt+seconds(1) > ny_lt);
check("local <= local", az_lt <= ny_lt);
check("local >= local", az_lt >= ny_lt);
check("local != local", az_lt+seconds(1) != ny_lt);
au_lt += hours(1);
check("local != after +=", au_lt != utc_lt);
check("local <= after +=", utc_lt <= au_lt);
check("local >= after +=", au_lt >= utc_lt);
check("local < after +=", utc_lt < au_lt);
check("local > after +=", au_lt > utc_lt);
au_lt -= hours(1);
check("local == utc after -=", au_lt == utc_lt);
check("local + days",
(az_lt + days(2)).to_string() == "2003-Aug-30 05:00:00 MST");
check("local - days",
(az_lt - days(2)).to_string() == "2003-Aug-26 05:00:00 MST");
check("local += days",
(az_lt += days(2)).to_string() == "2003-Aug-30 05:00:00 MST");
check("local -= days",
(az_lt -= days(2)).to_string() == "2003-Aug-28 05:00:00 MST");
check("local + time_duration",
(az_lt + hours(2)).to_string() == "2003-Aug-28 07:00:00 MST");
check("local - time_duration",
(az_lt - hours(2)).to_string() == "2003-Aug-28 03:00:00 MST");
// special_values is more thoroughly tested in posix_time
check("pos_infinity > local", sv_lt > au_lt);
local_date_time sv_lt2(sv_lt + days(2));
check("pos_infin + duration == pos_infin", sv_lt2 == sv_lt);
#if defined(BOOST_DATE_TIME_OPTIONAL_GREGORIAN_TYPES)
months m(2);
years y(2);
check("Local + months",
(az_lt + m).to_string() == "2003-Oct-28 05:00:00 MST");
az_lt += m;
check("Local += months",
az_lt.to_string() == "2003-Oct-28 05:00:00 MST");
check("Local - months",
(az_lt - m).to_string() == "2003-Aug-28 05:00:00 MST");
az_lt -= m;
check("Local -= months",
az_lt.to_string() == "2003-Aug-28 05:00:00 MST");
check("Local + years",
(az_lt + y).to_string() == "2005-Aug-28 05:00:00 MST");
az_lt += y;
check("Local += years",
az_lt.to_string() == "2005-Aug-28 05:00:00 MST");
check("Local - years",
(az_lt - y).to_string() == "2003-Aug-28 05:00:00 MST");
az_lt -= y;
check("Local -= years",
az_lt.to_string() == "2003-Aug-28 05:00:00 MST");
#endif // BOOST_DATE_TIME_OPTIONAL_GREGORIAN_TYPES
}
}
catch(std::exception& e) {
check(std::string("test failed due to exception: ") + e.what(), false);
}
return printTestStats();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -