#include #include #include "boost/date_time/gregorian/gregorian.hpp" #include "boost/date_time/posix_time/posix_time.hpp" #include "boost/date_time/local_time/local_time.hpp" #define Equal 1 #define UNITTEST_ASSERT(_C_, _E_, _A_)\ if((_E_) != (_A_))\ {\ std::cout << "expected=" << (_E_) << std::endl;\ std::cout << "actual =" << (_A_) << std::endl;\ assert((_E_) == (_A_));\ } /** Converts a std::wstring into a std::string with iso8859_1 encoding. * This method uses the fact that iso8859-1 is the same as the * low byte of UFT-16 or UTF-32 */ template < typename StringT > StringT iso8859_1 ( std::wstring const & rc_string ) { StringT result; typename StringT::value_type mask = ~0; result.reserve(rc_string.size()); for(typename StringT::size_type cnt = 0, max = rc_string.size(); cnt < max; ++cnt) { result.push_back(mask & rc_string[cnt]); } return result; } /** Converts a std::String with iso8859_1 encoding into a std::wstring. * This method uses the fact that iso8859-1 is the same as the * low byte of UFT-16 or UTF-32 */ template < typename StringT > StringT iso8859_1 ( std::string const & rc_string ) { StringT result; typename StringT::value_type mask = ~0; result.resize(rc_string.size()); for(typename StringT::size_type cnt = 0, max = rc_string.size(); cnt < max; ++cnt) { result.push_back(mask & rc_string[cnt]); } return result; } /** Nop specialization for std::string. */ template < > inline std::string iso8859_1 ( std::string const & rc_string ) { return rc_string; } /** Nop specialization for std::wstring. */ template < > inline std::wstring iso8859_1 ( std::wstring const & rc_string ) { return rc_string; } void testConstructor() { std::stringstream ss; // switch locale to output ISO format boost::local_time::local_time_facet* output_facet = new boost::local_time::local_time_facet(); output_facet->format( boost::local_time::local_time_facet::iso_time_format_extended_specifier ); ss.imbue(std::locale(std::locale("C"), output_facet)); boost::posix_time::ptime pt( boost::gregorian::date( 2005, 9, 30 ), boost::posix_time::time_duration( 18, 25, 17 ) ); boost::local_time::time_zone_ptr zone( new boost::local_time::posix_time_zone("+01:00") ); boost::local_time::local_date_time ldt( pt, zone ); ss.str(""); ss.clear(); ss << ldt; UNITTEST_ASSERT(Equal, std::string("2005-09-30 19:25:17+01:00"), ss.str()); UNITTEST_ASSERT(Equal, static_cast(2005), ldt.date().year()); UNITTEST_ASSERT(Equal, static_cast(9), ldt.date().month()); UNITTEST_ASSERT(Equal, static_cast(30), ldt.date().day()); UNITTEST_ASSERT(Equal, static_cast(19), ldt.time_of_day().hours()); UNITTEST_ASSERT(Equal, static_cast(25), ldt.time_of_day().minutes()); UNITTEST_ASSERT(Equal, static_cast(17), ldt.time_of_day().seconds()); UNITTEST_ASSERT(Equal, static_cast(2005), ldt.utc_time().date().year()); UNITTEST_ASSERT(Equal, static_cast(9), ldt.utc_time().date().month()); UNITTEST_ASSERT(Equal, static_cast(30), ldt.utc_time().date().day()); UNITTEST_ASSERT(Equal, static_cast(18), ldt.utc_time().time_of_day().hours()); UNITTEST_ASSERT(Equal, static_cast(25), ldt.utc_time().time_of_day().minutes()); UNITTEST_ASSERT(Equal, static_cast(17), ldt.utc_time().time_of_day().seconds()); UNITTEST_ASSERT(Equal, static_cast(1), ldt.zone()->base_utc_offset().hours()); UNITTEST_ASSERT(Equal, static_cast(0), ldt.zone()->base_utc_offset().minutes()); pt = boost::posix_time::ptime( boost::gregorian::date( 2005, 10, 1 ), boost::posix_time::time_duration( 3, 22, 17 ) ); zone = boost::local_time::time_zone_ptr( new boost::local_time::posix_time_zone("-08:03") ); ldt = boost::local_time::local_date_time( pt, zone ); ss.str(""); ss.clear(); ss << ldt; UNITTEST_ASSERT(Equal, std::string("2005-09-30 19:25:17-08:03"), ss.str()); UNITTEST_ASSERT(Equal, static_cast(2005), ldt.date().year()); UNITTEST_ASSERT(Equal, static_cast(9), ldt.date().month()); UNITTEST_ASSERT(Equal, static_cast(30), ldt.date().day()); UNITTEST_ASSERT(Equal, static_cast(19), ldt.time_of_day().hours()); UNITTEST_ASSERT(Equal, static_cast(25), ldt.time_of_day().minutes()); UNITTEST_ASSERT(Equal, static_cast(17), ldt.time_of_day().seconds()); UNITTEST_ASSERT(Equal, static_cast(2005), ldt.utc_time().date().year()); UNITTEST_ASSERT(Equal, static_cast(10), ldt.utc_time().date().month()); UNITTEST_ASSERT(Equal, static_cast(1), ldt.utc_time().date().day()); UNITTEST_ASSERT(Equal, static_cast(11), ldt.utc_time().time_of_day().hours()); UNITTEST_ASSERT(Equal, static_cast(22), ldt.utc_time().time_of_day().minutes()); UNITTEST_ASSERT(Equal, static_cast(17), ldt.utc_time().time_of_day().seconds()); UNITTEST_ASSERT(Equal, static_cast(-8), ldt.zone()->base_utc_offset().hours()); UNITTEST_ASSERT(Equal, static_cast(-3), ldt.zone()->base_utc_offset().minutes()); } void testInputIso() { std::stringstream ss; // switch locale to read ISO format boost::local_time::local_time_input_facet* input_facet = new boost::local_time::local_time_input_facet(); input_facet->set_iso_extended_format(); ss.imbue(std::locale(std::locale("C"), input_facet)); boost::local_time::time_zone_ptr zone; boost::local_time::local_date_time ndt( boost::posix_time::not_a_date_time, zone ); boost::local_time::local_date_time ldt( boost::posix_time::not_a_date_time, zone ); ldt = ndt; ss.str("2005-09-30 19:25:17+01:00"); ss.clear(); ss >> ldt; UNITTEST_ASSERT(Equal, static_cast(2005), ldt.date().year()); UNITTEST_ASSERT(Equal, static_cast(9), ldt.date().month()); UNITTEST_ASSERT(Equal, static_cast(30), ldt.date().day()); UNITTEST_ASSERT(Equal, static_cast(19), ldt.time_of_day().hours()); UNITTEST_ASSERT(Equal, static_cast(25), ldt.time_of_day().minutes()); UNITTEST_ASSERT(Equal, static_cast(17), ldt.time_of_day().seconds()); UNITTEST_ASSERT(Equal, static_cast(18), ldt.utc_time().time_of_day().hours()); UNITTEST_ASSERT(Equal, static_cast(25), ldt.utc_time().time_of_day().minutes()); UNITTEST_ASSERT(Equal, static_cast(17), ldt.utc_time().time_of_day().seconds()); UNITTEST_ASSERT(Equal, true, 0 != ldt.zone().get()); UNITTEST_ASSERT(Equal, static_cast(1), ldt.zone()->base_utc_offset().hours()); UNITTEST_ASSERT(Equal, static_cast(0), ldt.zone()->base_utc_offset().minutes()); ldt = ndt; ss.str("2005-09-30 19:25:17-08:00"); ss.clear(); ss >> ldt; UNITTEST_ASSERT(Equal, static_cast(2005), ldt.date().year()); UNITTEST_ASSERT(Equal, static_cast(9), ldt.date().month()); UNITTEST_ASSERT(Equal, static_cast(30), ldt.date().day()); UNITTEST_ASSERT(Equal, static_cast(19), ldt.time_of_day().hours()); UNITTEST_ASSERT(Equal, static_cast(25), ldt.time_of_day().minutes()); UNITTEST_ASSERT(Equal, static_cast(17), ldt.time_of_day().seconds()); UNITTEST_ASSERT(Equal, static_cast(3), ldt.utc_time().time_of_day().hours()); UNITTEST_ASSERT(Equal, static_cast(25), ldt.utc_time().time_of_day().minutes()); UNITTEST_ASSERT(Equal, static_cast(17), ldt.utc_time().time_of_day().seconds()); UNITTEST_ASSERT(Equal, true, 0 != ldt.zone().get()); UNITTEST_ASSERT(Equal, static_cast(-8), ldt.zone()->base_utc_offset().hours()); UNITTEST_ASSERT(Equal, static_cast(0), ldt.zone()->base_utc_offset().minutes()); } void testInputIsoWide() { std::wstringstream ss; // switch locale to read ISO format boost::local_time::wlocal_time_input_facet* input_facet = new boost::local_time::wlocal_time_input_facet(); input_facet->set_iso_extended_format(); ss.imbue(std::locale(std::locale("C"), input_facet)); boost::local_time::time_zone_ptr zone; boost::local_time::local_date_time ndt( boost::posix_time::not_a_date_time, zone ); boost::local_time::local_date_time ldt( boost::posix_time::not_a_date_time, zone ); ldt = ndt; ss.str(L"2005-09-30 19:25:17+01:00"); ss.clear(); ss >> ldt; UNITTEST_ASSERT(Equal, static_cast(2005), ldt.date().year()); UNITTEST_ASSERT(Equal, static_cast(9), ldt.date().month()); UNITTEST_ASSERT(Equal, static_cast(30), ldt.date().day()); UNITTEST_ASSERT(Equal, static_cast(19), ldt.time_of_day().hours()); UNITTEST_ASSERT(Equal, static_cast(25), ldt.time_of_day().minutes()); UNITTEST_ASSERT(Equal, static_cast(17), ldt.time_of_day().seconds()); UNITTEST_ASSERT(Equal, static_cast(18), ldt.utc_time().time_of_day().hours()); UNITTEST_ASSERT(Equal, static_cast(25), ldt.utc_time().time_of_day().minutes()); UNITTEST_ASSERT(Equal, static_cast(17), ldt.utc_time().time_of_day().seconds()); UNITTEST_ASSERT(Equal, true, 0 != ldt.zone().get()); UNITTEST_ASSERT(Equal, static_cast(1), ldt.zone()->base_utc_offset().hours()); UNITTEST_ASSERT(Equal, static_cast(0), ldt.zone()->base_utc_offset().minutes()); ldt = ndt; ss.str(L"2005-09-30 19:25:17-08:00"); ss.clear(); ss >> ldt; UNITTEST_ASSERT(Equal, static_cast(2005), ldt.date().year()); UNITTEST_ASSERT(Equal, static_cast(9), ldt.date().month()); UNITTEST_ASSERT(Equal, static_cast(30), ldt.date().day()); UNITTEST_ASSERT(Equal, static_cast(19), ldt.time_of_day().hours()); UNITTEST_ASSERT(Equal, static_cast(25), ldt.time_of_day().minutes()); UNITTEST_ASSERT(Equal, static_cast(17), ldt.time_of_day().seconds()); UNITTEST_ASSERT(Equal, static_cast(3), ldt.utc_time().time_of_day().hours()); UNITTEST_ASSERT(Equal, static_cast(25), ldt.utc_time().time_of_day().minutes()); UNITTEST_ASSERT(Equal, static_cast(17), ldt.utc_time().time_of_day().seconds()); UNITTEST_ASSERT(Equal, true, 0 != ldt.zone().get()); UNITTEST_ASSERT(Equal, static_cast(-8), ldt.zone()->base_utc_offset().hours()); UNITTEST_ASSERT(Equal, static_cast(0), ldt.zone()->base_utc_offset().minutes()); } void testOutputIso() { std::stringstream ss; // switch locale to output ISO format boost::local_time::local_time_facet* output_facet = new boost::local_time::local_time_facet(); output_facet->format( boost::local_time::local_time_facet::iso_time_format_extended_specifier ); ss.imbue(std::locale(std::locale("C"), output_facet)); std::string str("2005-09-30 19:25:17+01:00"); boost::local_time::local_date_time ldt( boost::posix_time::ptime( boost::gregorian::date( 2005, 9, 30 ), boost::posix_time::time_duration( 18, 25, 17 ) ), boost::local_time::time_zone_ptr( new boost::local_time::posix_time_zone("CET+01:00") ) ); ss.str(""); ss.clear(); ss << ldt; UNITTEST_ASSERT(Equal, str, ss.str()); str = "2005-09-30 19:25:17-08:00"; ldt = boost::local_time::local_date_time( boost::posix_time::ptime( boost::gregorian::date( 2005, 10, 1 ), boost::posix_time::time_duration( 3, 25, 17 ) ), boost::local_time::time_zone_ptr( new boost::local_time::posix_time_zone("PST-08:00") ) ); ss.str(""); ss.clear(); ss << ldt; UNITTEST_ASSERT(Equal, str, ss.str()); } void testOutputIsoWide() { std::wstringstream ss; // switch locale to output ISO format boost::local_time::wlocal_time_facet* output_facet = new boost::local_time::wlocal_time_facet(); output_facet->format( boost::local_time::wlocal_time_facet::iso_time_format_extended_specifier ); ss.imbue(std::locale(std::locale("C"), output_facet)); std::wstring str(L"2005-09-30 19:25:17+01:00"); boost::local_time::local_date_time ldt( boost::posix_time::ptime( boost::gregorian::date( 2005, 9, 30 ), boost::posix_time::time_duration( 18, 25, 17 ) ), boost::local_time::time_zone_ptr( new boost::local_time::posix_time_zone("CET+01:00") ) ); ss.str(L""); ss.clear(); ss << ldt; UNITTEST_ASSERT(Equal, iso8859_1(str), iso8859_1(ss.str()) ); str = L"2005-09-30 19:25:17-08:00"; ldt = boost::local_time::local_date_time( boost::posix_time::ptime( boost::gregorian::date( 2005, 10, 1 ), boost::posix_time::time_duration( 3, 25, 17 ) ), boost::local_time::time_zone_ptr( new boost::local_time::posix_time_zone("PST-08:00") ) ); ss.str(L""); ss.clear(); ss << ldt; UNITTEST_ASSERT(Equal, iso8859_1(str), iso8859_1(ss.str()) ); } int main (int argc , char **) { switch(argc) { case 1: std::cout << "testing testConstructor ..." << std::endl; testConstructor(); std::cout << "testing testConstructor OK" << std::endl; break; case 2: std::cout << "testing testInputIso ..." << std::endl; testInputIso(); std::cout << "testing testInputIso OK" << std::endl; break; case 3: std::cout << "testing testInputIsoWide ..." << std::endl; testInputIsoWide(); std::cout << "testing testInputIsoWide OK" << std::endl; break; case 4: std::cout << "testing testOutputIso ..." << std::endl; testOutputIso(); std::cout << "testing testOutputIso OK" << std::endl; break; case 5: std::cout << "testing testOutputIsoWide ..." << std::endl; testOutputIsoWide(); std::cout << "testing testOutputIsoWide OK" << std::endl; break; } return 0; }