📄 testlocal_time.cpp
字号:
/* Copyright (c) 2003-2005 CrystalClear Software, Inc.
* Subject to the Boost Software License, Version 1.0.
* (See accompanying file LICENSE-1.0 or http://www.boost.org/LICENSE-1.0)
* Author: Jeff Garland, Bart Garst
* $Date: 2005/05/26 12:37:49 $
*/
#include "boost/date_time/local_time/custom_time_zone.hpp"
#include "boost/date_time/posix_time/posix_time.hpp"
#include "boost/date_time/local_time/local_time.hpp"
// #include "boost/date_time/local_time/posix_time_zone.hpp"
#include "boost/date_time/testfrmwk.hpp"
//#include "boost/date_time/c_time.hpp"
#include <iostream>
#include <sstream>
// function eases testing
std::string tm_out(const tm& ptr){
std::stringstream ss;
ss
<< ptr.tm_wday << ' ' << ptr.tm_yday << ' '
<< std::setw(2) << std::setfill('0') << ptr.tm_mon + 1 << '/'
<< std::setw(2) << std::setfill('0') << ptr.tm_mday << '/'
<< std::setw(2) << std::setfill('0') << ptr.tm_year + 1900 << ' '
<< std::setw(2) << std::setfill('0') << ptr.tm_hour << ':'
<< std::setw(2) << std::setfill('0') << ptr.tm_min << ':'
<< std::setw(2) << std::setfill('0') << ptr.tm_sec << ' ';
if(ptr.tm_isdst >= 0){
ss << (ptr.tm_isdst ? "DST" : "STD");
}
else{
ss << "DST/STD unknown";
}
return ss.str();
}
int
main()
{
using namespace boost::gregorian;
using namespace boost::posix_time;
using namespace boost::local_time;
// since local_date_time inherits it's math operations from time, the
// tests here only show that the operations work. The thorough testing
// of these operations is done in the posix_time tests
try {
time_zone_ptr az_tz(new posix_time_zone("MST-07"));
time_zone_ptr ny_tz(new posix_time_zone("EST-05EDT,M4.1.0,M10.5.0"));
// EST & EST for sydney is correct, according to zoneinfo files
time_zone_ptr sydney(new posix_time_zone("EST+10EST,M10.5.0,M3.5.0/03:00"));
time_zone_ptr null_tz;
date d(2003, 12, 20);
hours h(12);
ptime t(d,h);
local_date_time az_time(t, az_tz); // ptime constructor is a UTC time
check("Zone abbreviation", az_time.zone()->std_zone_abbrev() == std::string("MST"));
check("base offset", az_time.zone()->base_utc_offset() == hours(-7));
check("zone has dst", az_time.zone()->has_dst() == false);
check("is_dst check", az_time.is_dst() == false);
check("to_string: " + az_time.to_string(),
az_time.to_string() == "2003-Dec-20 05:00:00 MST");
std::cout << "\nChecking copy construction" << std::endl;
local_date_time az_time2(az_time); //copy constructor
// Now test the copy
check("is_dst check", az_time2.is_dst() == false);
check("to_string: " + az_time2.to_string(),
az_time2.to_string() == "2003-Dec-20 05:00:00 MST");
check("zone has dst", az_time2.zone()->has_dst() == false);
check("base offset", az_time2.zone()->base_utc_offset() == hours(-7));
std::cout << "\nChecking special_value construction" << std::endl;
// since local_date_time inherits its special value operatorations
// from time, we only need to show here that they work as thorough
// testing is done in the posix_time tests
ptime svpt(not_a_date_time);
local_date_time sv_time(svpt, ny_tz);
check("is special_value", sv_time.is_not_a_date_time());
check("to_string: " + sv_time.to_string(),
sv_time.to_string() == "not-a-date-time");
check("is_dst", sv_time.is_dst() == false);
local_date_time sv_time2(pos_infin);
check("is special_value", sv_time2.is_pos_infinity());
check("to_string: " + sv_time2.to_string(),
sv_time2.to_string() == "+infinity");
check("is_dst", sv_time2.is_dst() == false);
sv_time2 += days(12); // add a duration to a special value
check("Add a duration to a special value", sv_time2.is_pos_infinity());
local_date_time sv_time3(max_date_time, ny_tz);
#ifdef BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG
check("max_date_time to_string: " + sv_time3.to_string(),
sv_time3.to_string() == "9999-Dec-31 18:59:59.999999999 EST");
#else
check("max_date_time to_string: " + sv_time3.to_string(),
sv_time3.to_string() == "9999-Dec-31 18:59:59.999999 EST");
#endif
try {
local_date_time sv_time4(min_date_time);
check("min_date_time to_string: " + sv_time4.to_string(),
sv_time4.to_string() == "1400-Jan-01 00:00:00 UTC");
}
catch (std::exception& e) {
check("min_date_time to_string -- exception" , false);
std::cout << "Exception is : " << e.what() << std::endl;
}
/** todo -- this will cause an out of range when min_date is adjusted for ny_tz
local_date_time sv_time5(min_date_time, ny_tz);
std::cout << sv_time5.to_string() << std::endl;
**/
std::cout << "\nChecking calc_options construction" << std::endl;
{ // invalid NADT
date d(2004, Apr, 4);
time_duration td(2,30,0); // invalid local time in ny_tz
local_date_time calcop(d, td, ny_tz, local_date_time::NOT_DATE_TIME_ON_ERROR);
check("is NADT", calcop.is_not_a_date_time());
}
{ // invalid exception
date d(2004, Apr, 4);
time_duration td(2,30,0); // invalid local time in ny_tz
try{
local_date_time calcop(d, td, ny_tz, local_date_time::EXCEPTION_ON_ERROR);
check("Did not catch expected exception", false);
}catch(time_label_invalid& /*i*/){
check("Caught expected exception", true);
}catch(...){
check("Caught unexpected exception", false);
}
}
{ // ambig NADT
date d(2004, Oct, 31);
time_duration td(1,30,0); // ambig local time in ny_tz
local_date_time calcop(d, td, ny_tz, local_date_time::NOT_DATE_TIME_ON_ERROR);
check("is NADT", calcop.is_not_a_date_time());
}
{ // ambig exception
date d(2004, Oct, 31);
time_duration td(1,30,0); // ambig local time in ny_tz
try{
local_date_time calcop(d, td, ny_tz, local_date_time::EXCEPTION_ON_ERROR);
check("Did not catch expected exception", false);
}catch(ambiguous_result& /*a*/){
check("Caught expected exception", true);
}catch(...){
check("Caught unexpected exception", false);
}
}
//Now construct with a date and time
std::cout << "\nChecking construct with date and time_duration" << std::endl;
local_date_time az_time3(d, h, az_tz, false);
check("Zone abbreviation", az_time3.zone()->std_zone_abbrev() == std::string("MST"));
check("base offset", az_time3.zone()->base_utc_offset() == hours(-7));
check("base offset", az_time3.zone()->has_dst() == false);
check("is_dst check", az_time3.is_dst() == false);
check("to_string: " + az_time3.to_string(),
az_time3.to_string() == "2003-Dec-20 12:00:00 MST");
// construct with a null tz
//local_date_time null_tz_time(d, h, null_tz, false);
local_date_time null_tz_time(d, h, null_tz, true);
// TODO: how to handle calls to null_tz_time.zone()->...
check("is_dst check", null_tz_time.is_dst() == false);
check("to_string: " + null_tz_time.to_string(),
null_tz_time.to_string() == "2003-Dec-20 12:00:00 UTC");
//Now construct with a date and time - invalid parameters
try{
local_date_time blt(d, h, ny_tz, true);
check("Did not catch expected exception (dst_not_valid)", false);
}catch(dst_not_valid& d){
check(std::string("Caught expected exception (dst_not_valid) ") + d.what(), true);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -