📄 testtime_input_facet.cpp
字号:
/* Copyright (c) 2005 CrystalClear Software, Inc.
* Use, modification and distribution is 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/07/17 23:58:33 $
*/
#include "boost/date_time/gregorian/gregorian.hpp"
#include "boost/date_time/posix_time/posix_time.hpp"
#include "boost/date_time/testfrmwk.hpp"
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
// for tests that are expected to fail and throw exceptions
template<class temporal_type, class exception_type>
bool failure_test(temporal_type component,
const std::string& input,
exception_type /*except*/,
boost::posix_time::time_input_facet* facet)
{
using namespace boost::posix_time;
bool result = false;
std::istringstream iss(input);
iss.exceptions(std::ios_base::failbit); // turn on exceptions
iss.imbue(std::locale(std::locale::classic(), facet));
try {
iss >> component;
}
catch(exception_type e) {
std::cout << "Expected exception caught: \""
<< e.what() << "\"" << std::endl;
result = iss.fail(); // failbit must be set to pass test
}
catch(...) {
result = false;
}
return result;
}
// for tests that are expected to fail quietly
template<class temporal_type>
bool failure_test(temporal_type component,
const std::string& input,
boost::posix_time::time_input_facet* facet)
{
using namespace boost::posix_time;
std::istringstream iss(input);
/* leave exceptions turned off
* iss.exceptions(std::ios_base::failbit); */
iss.imbue(std::locale(std::locale::classic(), facet));
try {
iss >> component;
}
catch(...) {
std::cout << "Caught unexpected exception" << std::endl;
return false;
}
return iss.fail(); // failbit must be set to pass test
}
using namespace boost::gregorian;
using namespace boost::posix_time;
void
do_all_tests()
{
#if defined(USE_DATE_TIME_PRE_1_33_FACET_IO) // skip this file
check("No tests run for this compiler. Incompatible IO", true);
#else
// set up initial objects
time_duration td = hours(0);
ptime pt(not_a_date_time);
time_period tp(pt, td);
// exceptions for failure_tests
std::ios_base::failure e_failure("default");
/* test ptime, time_duration, time_period.
* test all formats.
* test for bad input.
* test special values.
* do not test custom names (done in gregorian).
*
* format flags to test H,M,S,s,F,f,j
*/
// default format tests: time_duration, ptime
std::istringstream iss("09:59:01.321987654321 2005-Jan-15 10:15:03.123456789123");
iss >> td;
iss >> pt;
#if defined(BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG)
check("Default format time_duration", td == time_duration(9,59,1,321987654));
check("Default format ptime", pt == ptime(date(2005,01,15),time_duration(10,15,3,123456789)));
#else
check("Default format time_duration", td == time_duration(9,59,1,321987));
check("Default format ptime", pt == ptime(date(2005,01,15),time_duration(10,15,3,123456)));
#endif
// test all flags that appear in time_input_facet
iss.str("12:34:56 2005-Jan-15 12:34:56");
iss >> td;
iss >> pt;
check("Default format time_duration no frac_sec", td == time_duration(12,34,56));
// the following test insures %F parsing stops at the appropriate point
check("Default format ptime", pt == ptime(date(2005,01,15),time_duration(12,34,56)));
iss.str("14:13:12 extra stuff"); // using default %H:%M:%S%F format
iss >> td;
check("Default frac_sec format time_duration", td == time_duration(14,13,12));
time_input_facet* facet = new time_input_facet();
std::locale loc(std::locale::classic(), facet);
facet->time_duration_format("%H:%M:%S%f");
iss.imbue(loc);
iss.str("14:13:12.0 extra stuff");
iss >> td;
check("Required-frac_sec format time_duration", td == time_duration(14,13,12));
iss.str("12");
facet->time_duration_format("%H");
iss >> td;
check("Hours format", td == hours(12));
iss.str("05");
facet->time_duration_format("%M");
iss >> td;
check("Minutes format", td == minutes(5));
iss.str("45");
facet->time_duration_format("%S");
iss >> td;
check("Seconds w/o frac_sec format", td == seconds(45));
iss.str("10.01");
facet->time_duration_format("%s");
iss >> td;
#if defined(BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG)
check("Seconds w/ frac_sec format", td == time_duration(0,0,10,10000000));
#else
check("Seconds w/ frac_sec format", td == time_duration(0,0,10,10000));
#endif
iss.str("2005-105T23:59");
facet->format("%Y-%jT%H:%M"); // extended ordinal format
iss >> pt;
check("Extended Ordinal format", pt == ptime(date(2005,4,15),time_duration(23,59,0)));
/* this is not implemented yet. The flags: %I & %p are not parsed
iss.str("2005-Jun-14 03:15:00 PM");
facet->format("%Y-%b-%d %I:%M:%S %p");
iss >> pt;
check("12 hour time format (AM/PM)", pt == ptime(date(2005,6,14),time_duration(15,15,0)));
*/
iss.str("2005-Jun-14 15:15:00 %d");
facet->format("%Y-%b-%d %H:%M:%S %%d");
iss >> pt;
check("Literal '%' in format", pt == ptime(date(2005,6,14),time_duration(15,15,0)));
iss.str("15:15:00 %d");
facet->time_duration_format("%H:%M:%S %%d");
iss >> td;
check("Literal '%' in time_duration format", td == time_duration(15,15,0));
iss.str("2005-Jun-14 15:15:00 %14");
facet->format("%Y-%b-%d %H:%M:%S %%%d"); // %% => % & %d => day_of_month
iss >> pt;
check("Multiple literal '%'s in format", pt == ptime(date(2005,6,14),time_duration(15,15,0)));
iss.str("15:15:00 %15");
facet->time_duration_format("%H:%M:%S %%%M");
iss >> td;
check("Multiple literal '%'s in time_duration format", td == time_duration(15,15,0));
{ /****** iso format tests (and custom 'scrunched-together formats) ******/
time_input_facet *facet = new time_input_facet();
facet->set_iso_format();
facet->time_duration_format("%H%M%S%F"); // iso format
std::stringstream ss;
ss.imbue(std::locale(std::locale::classic(), facet));
ptime pt(not_a_date_time);
time_duration td(not_a_date_time);
date d(2002,Oct,17);
#if defined(BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG)
time_duration td2(23,12,17,123450000);
#else
time_duration td2(23,12,17,123450);
#endif
ptime result(d, td2);
ss.str("20021017T231217.12345");
ss >> pt;
check("iso_format ptime", pt == result);
ss.str("");
facet->set_iso_extended_format();
ss.str("2002-10-17 23:12:17.12345");
ss >> pt;
check("iso_extended_format ptime", pt == result);
ss.str("");
ss.str("231217.12345");
ss >> td;
check("iso_format time_duration", td == td2);
ss.str("");
ss.str("-infinity");
ss >> td;
check("iso_format time_duration (special_value)",
td == time_duration(neg_infin));
ss.str("");
// the above tests prove correct parsing of time values in these formats.
// these tests show they also handle special_values & exceptions properly
time_duration nadt(not_a_date_time);
ss.exceptions(std::ios_base::failbit); // we need exceptions turned on here
int count = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -