testdate_input_facet.cpp

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

CPP
572
字号
  iss >> dp;  check("Closed range period - custom delimiters", dp == date_period(begin,len));    // date_generator tests  // date_generators use formats contained in the   // date_input_facet for weekdays and months  // reset month & weekday formats to defaults  facet->month_format("%b");  facet->weekday_format("%a");  partial_date pd(1,Jan);  nth_kday_of_month nkd(nth_kday_of_month::first, Sunday, Jan);  first_kday_of_month fkd(Sunday, Jan);  last_kday_of_month lkd(Sunday, Jan);  first_kday_before fkb(Sunday);  first_kday_after fka(Sunday);  // using default date_generator_parser "nth_strings"  iss.str("29 Feb");  iss >> pd;   // Feb-29 is a valid date_generator, get_date() will fail in a non-leap year  check("Default strings, partial_date",       pd.get_date(2004) == date(2004,Feb,29));  iss.str("second Mon of Mar");  iss >> nkd;   check("Default strings, nth_day_of_the_week_in_month",       nkd.get_date(2004) == date(2004,Mar,8));  iss.str("first Tue of Apr");  iss >> fkd;   check("Default strings, first_day_of_the_week_in_month",       fkd.get_date(2004) == date(2004,Apr,6));  iss.str("last Wed of May");  iss >> lkd;   check("Default strings, last_day_of_the_week_in_month",       lkd.get_date(2004) == date(2004,May,26));  iss.str("Thu before");  iss >> fkb;   check("Default strings, first_day_of_the_week_before",       fkb.get_date(date(2004,Feb,8)) == date(2004,Feb,5));  iss.str("Fri after");  iss >> fka;   check("Default strings, first_day_of_the_week_after",       fka.get_date(date(2004,Feb,1)) == date(2004,Feb,6));  // failure tests  check("Incorrect elements (date_generator) w/exceptions", // after/before type mixup      failure_test(fkb, "Fri after", e_failure, new date_input_facet()));  check("Incorrect elements (date_generator) no exceptions", // after/before type mixup      failure_test(fkb, "Fri after", new date_input_facet()));  check("Incorrect elements (date_generator) w/exceptions", // first/last type mixup      failure_test(lkd, "first Tue of Apr", e_failure, new date_input_facet()));  check("Incorrect elements (date_generator) no exceptions", // first/last type mixup      failure_test(lkd, "first Tue of Apr", new date_input_facet()));  check("Incorrect elements (date_generator) w/exceptions", // 'in' is wrong       failure_test(nkd, "second Mon in Mar", e_failure, new date_input_facet()));  check("Incorrect elements (date_generator) no exceptions", // 'in' is wrong       failure_test(nkd, "second Mon in Mar", new date_input_facet()));  // date_generators - custom element strings  facet->date_gen_element_strings("1st","2nd","3rd","4th","5th","final","prior to","past","in");  iss.str("3rd Sat in Jul");  iss >> nkd;  check("Custom strings, nth_day_of_the_week_in_month",       nkd.get_date(2004) == date(2004,Jul,17));  iss.str("1st Wed in May");  iss >> fkd;   check("Custom strings, first_day_of_the_week_in_month",       fkd.get_date(2004) == date(2004,May,5));  iss.str("final Tue in Apr");  iss >> lkd;   check("Custom strings, last_day_of_the_week_in_month",       lkd.get_date(2004) == date(2004,Apr,27));  iss.str("Fri prior to");  iss >> fkb;   check("Custom strings, first_day_of_the_week_before",       fkb.get_date(date(2004,Feb,8)) == date(2004,Feb,6));  iss.str("Thu past");  iss >> fka;   check("Custom strings, first_day_of_the_week_after",       fka.get_date(date(2004,Feb,1)) == date(2004,Feb,5));  // date_generators - special case with empty element string  /* Doesn't work. Empty string returns -1 from string_parse_tree    * because it attempts to match the next set of characters in the    * stream to the wrong element. Ex. It attempts to match "Mar" to    * the 'of' element in the test below.   *   facet->date_gen_element_strings("1st","2nd","3rd","4th","5th","final","prior to","past",""); // the 'of' string is an empty string  iss.str("final Mon Mar");  iss >> lkd;   check("Special case, empty element string",       lkd.get_date(2005) == date(2005,Mar,28));      */    // special values tests (date and days only)  iss.str("minimum-date-time +infinity");  iss >> d;  iss >> dd;  check("Special values, default strings, min_date_time date",      d == date(min_date_time));  check("Special values, default strings, pos_infin days",      dd == days(pos_infin));  iss.str("-infinity maximum-date-time");  iss >> d;  iss >> dd;  check("Special values, default strings, neg_infin date",      d == date(neg_infin));  check("Special values, default strings, max_date_time days",      dd == days(max_date_time));  iss.str("not-a-date-time");  iss >> d;  check("Special values, default strings, not_a_date_time date",      d == date(not_a_date_time));  // special values custom, strings  special_values_parser svp("NADT", "MINF", "INF", "MINDT", "MAXDT");  facet->special_values_parser(svp);  iss.str("MINDT INF");  iss >> d;  iss >> dd;  check("Special values, custom strings, min_date_time date",      d == date(min_date_time));  check("Special values, custom strings, pos_infin days",      dd == days(pos_infin));  iss.str("MINF MAXDT");  iss >> d;  iss >> dd;  check("Special values, custom strings, neg_infin date",      d == date(neg_infin));  check("Special values, custom strings, max_date_time days",      dd == days(max_date_time));  iss.str("NADT");  iss >> dd;  check("Special values, custom strings, not_a_date_time days",      dd == days(not_a_date_time));  // failure test  check("Misspelled input, special_value date w/exceptions",       failure_test(d, "NSDT", e_bad_year, new date_input_facet()));  check("Misspelled input, special_value date no exceptions",       failure_test(d, "NSDT", new date_input_facet()));  check("Misspelled input, special_value days w/exceptions",       failure_test(dd, "NSDT", e_failure, new date_input_facet()));  check("Misspelled input, special_value days no exceptions",       failure_test(dd, "NSDT", new date_input_facet()));  {    // German names. Please excuse any errors, I don't speak German and     // had to rely on an on-line translation service.    // These tests check one of each (at least) from all sets of custom strings    // create a custom format_date_parser    std::string m_a[] = {"Jan","Feb","Mar","Apr","Mai",                         "Jun","Jul","Aug","Sep","Okt","Nov","Dez"};    std::string m_f[] = {"Januar","Februar","Marz","April",                         "Mai","Juni","Juli","August",                         "September","Oktober","November","Dezember"};    std::string w_a[] = {"Son", "Mon", "Die","Mit", "Don", "Fre", "Sam"};    std::string w_f[] = {"Sonntag", "Montag", "Dienstag","Mittwoch",                         "Donnerstag", "Freitag", "Samstag"};    typedef boost::date_time::format_date_parser<date, char> date_parser;    date_parser::input_collection_type months_abbrev;    date_parser::input_collection_type months_full;    date_parser::input_collection_type wkdays_abbrev;    date_parser::input_collection_type wkdays_full;    months_abbrev.assign(m_a, m_a+12);    months_full.assign(m_f, m_f+12);    wkdays_abbrev.assign(w_a, w_a+7);    wkdays_full.assign(w_f, w_f+7);    date_parser d_parser("%B %d %Y",                         months_abbrev, months_full,                          wkdays_abbrev, wkdays_full);    // create a special_values parser    special_values_parser sv_parser("NichtDatumzeit",                                    "Negativ Unendlichkeit",                                    "Positiv Unendlichkeit",                                    "Wenigstes Datum",                                    "Maximales Datum");    // create a period_parser    period_parser p_parser; // default will do    // create date_generator_parser    typedef boost::date_time::date_generator_parser<date,char> date_gen_parser;    date_gen_parser dg_parser("Zuerst","Zweitens","Dritt","Viert",                              "F黱ft","Letzt","Vor","Nach","Von");    // create the date_input_facet    date_input_facet* de_facet =       new date_input_facet("%B %d %Y",                           d_parser,                           sv_parser,                           p_parser,                           dg_parser);    std::istringstream iss;    iss.imbue(std::locale(std::locale::classic(), de_facet));    // June 06 2005, Dec, minimum date, Tues    iss.str("Juni 06 2005 Dez Wenigstes Datum Die");    iss >> d;    iss >> m;    check("German names: date", d == date(2005, Jun, 6));    check("German names: month", m == greg_month(Dec));    iss >> d;    iss >> gw;    check("German names: special value date", d == date(min_date_time));    check("German names: short weekday", gw == greg_weekday(Tuesday));    de_facet->weekday_format("%A"); // long weekday    // Tuesday, Second Tuesday of Mar    iss.str("Dienstag Zweitens Dienstag von Mar");    iss >> gw;    iss >> nkd;    check("German names: long weekday", gw == greg_weekday(Tuesday));    check("German names, nth_day_of_the_week_in_month",         nkd.get_date(2005) == date(2005,Mar,8));    // Tuesday after    iss.str("Dienstag Nach");    iss >> fka;     check("German names, first_day_of_the_week_after",         fka.get_date(date(2005,Apr,5)) == date(2005,Apr,12));  }  {    // test name replacement functions    // collections for adding to facet    const char* const month_short_names[]={"*jan*","*feb*","*mar*",                                           "*apr*","*may*","*jun*",                                           "*jul*","*aug*","*sep*",                                           "*oct*","*nov*","*dec*"};    const char* const month_long_names[]={"**January**","**February**","**March**",                                          "**April**","**May**","**June**",                                          "**July**","**August**","**September**",                                          "**October**","**November**","**December**"};    const char* const weekday_short_names[]={"day1", "day2","day3","day4",                                             "day5","day6","day7"};    const char* const weekday_long_names[]= {"Sun-0", "Mon-1", "Tue-2",                                              "Wed-3", "Thu-4",                                              "Fri-5", "Sat-6"};    std::vector<std::basic_string<char> > short_weekday_names;    std::vector<std::basic_string<char> > long_weekday_names;    std::vector<std::basic_string<char> > short_month_names;    std::vector<std::basic_string<char> > long_month_names;        std::copy(&weekday_short_names[0],               &weekday_short_names[7],              std::back_inserter(short_weekday_names));    std::copy(&weekday_long_names[0],               &weekday_long_names[7],              std::back_inserter(long_weekday_names));    std::copy(&month_short_names[0],               &month_short_names[12],              std::back_inserter(short_month_names));    std::copy(&month_long_names[0],               &month_long_names[12],              std::back_inserter(long_month_names));    date d(not_a_date_time);    date_input_facet* facet = new date_input_facet();    std::stringstream ss;    ss.imbue(std::locale(std::locale::classic(), facet));    facet->short_month_names(short_month_names);    facet->short_weekday_names(short_weekday_names);    facet->long_month_names(long_month_names);    facet->long_weekday_names(long_weekday_names);    facet->format("%a %b %d, %Y");    ss.str("day7 *apr* 23, 2005");    ss >> d;    check("Short custom names, set via accessor function", d.day_of_week() == greg_weekday(6));    check("Short custom names, set via accessor function", d.month() == greg_month(4));    ss.str("");    ss.str("Sun-0 **April** 24, 2005");    facet->format("%A %B %d, %Y");    ss >> d;    check("Long custom names, set via accessor function", d.day_of_week() == greg_weekday(0));    check("Long custom names, set via accessor function", d.month() == greg_month(4));  }#else    check("This test is a nop for platforms with USE_DATE_TIME_PRE_1_33_FACET_IO",           true);#endif  return printTestStats();}                      

⌨️ 快捷键说明

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