⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testfacet.cpp

📁 boost库提供标准的C++ API 配合dev c++使用,功能更加强大
💻 CPP
字号:
/* Copyright (c) 2002,2003 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 
 */

#include <sstream>
#include <iostream>
#include <fstream>

#include "boost/date_time/gregorian/greg_month.hpp"
#include "boost/date_time/gregorian/greg_facet.hpp"
#include "boost/date_time/date_format_simple.hpp"
#include "boost/date_time/gregorian/gregorian.hpp"
#include "boost/date_time/testfrmwk.hpp"

#ifndef BOOST_DATE_TIME_NO_LOCALE

    const char* const de_short_month_names[]={"Jan","Feb","Mar","Apr","Mai","Jun","Jul","Aug","Sep","Okt","Nov","Dez", "NAM"};

    const char* const de_long_month_names[]={"Januar","Februar","Marz","April","Mai","Juni","Juli","August","September","Oktober","November","Dezember","NichtDerMonat"};
    const char* const de_special_value_names[]={"NichtDatumzeit","-unbegrenztheit", "+unbegrenztheit"};

const char* const de_short_weekday_names[]={"Son", "Mon", "Die","Mit", "Don", "Fre", "Sam"};

    const char* const de_long_weekday_names[]={"Sonntag", "Montag", "Dienstag","Mittwoch", "Donnerstag", "Freitag", "Samstag"};

#endif 

/** Not used for now
    const char* const es_short_month_names[]={"Ene","Feb","Mar","Abr","Pue","Jun","Jul","Ago","Sep","Oct","Nov","Dic", "NAM"};

    const char* const es_long_month_names[]={"Enero","Febrero","Marcha","Abril","Pueda","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre","NoAMes"};
    const char* const es_special_value_names[]={"NoUnRatoDeLaFacha","-infinito", "+infinito"};
**/
int
main()
{
//   std::locale native("");
//   std::cout << "native: " << native.name() << std::endl;
//#ifndef BOOST_NO_STD_LOCALE
#ifndef BOOST_DATE_TIME_NO_LOCALE

  using namespace boost::gregorian;

  typedef greg_facet_config facet_config;
  typedef boost::date_time::all_date_names_put<facet_config> date_facet;
  typedef boost::date_time::date_names_put<facet_config> date_facet_base;
  typedef boost::date_time::ostream_month_formatter<date_facet_base> month_formatter;

  date_facet gdnp(de_short_month_names, de_long_month_names, 
                  de_special_value_names, de_long_weekday_names,
                  de_long_weekday_names, 
                  '.', 
                  boost::date_time::ymd_order_dmy);

  std::stringstream ss;
  std::ostreambuf_iterator<char> coi(ss);
  gdnp.put_month_short(coi, Oct);
  check("check german short month: " + ss.str(), 
        ss.str() == std::string("Okt"));

  ss.str(""); //reset string stream 
  greg_month m(Oct);
  month_formatter::format_month(m, ss, gdnp);
  check("check german short month: " + ss.str(), 
        ss.str() == std::string("Okt"));
  ss.str(""); //reset string stream 
//   month_formatter::format_month(m, ss, gdnp);
//   check("check german long month: " + ss.str(), 
//         ss.str() == std::string("Oktober"));


  greg_year_month_day ymd(2002,Oct,1);
  typedef boost::date_time::ostream_ymd_formatter<greg_year_month_day, date_facet_base> ymd_formatter;
  ss.str(""); //reset string stream 
  ymd_formatter::ymd_put(ymd, ss, gdnp);
  check("check ymd: " + ss.str(), 
        ss.str() == std::string("01.Okt.2002"));


  typedef boost::date_time::ostream_date_formatter<date, date_facet_base> datef;

  std::stringstream os;
  date d1(2002, Oct, 1);
  datef::date_put(d1, os, gdnp);
  check("ostream low level check string:"+os.str(), 
        os.str() == std::string("01.Okt.2002"));

//   //Locale tests
  std::locale global;
  std::cout << "global: " << global.name() << std::endl;

  // put a facet into a locale
  //check for a facet p319
  check("no registered facet here",
        !std::has_facet<date_facet>(global));

  std::locale global2(global, 
                      new date_facet(de_short_month_names, 
                                     de_long_month_names,
                                     de_special_value_names,
                                     de_long_weekday_names,
                                     de_long_weekday_names));

  check("facet registered here",
        std::has_facet<boost::date_time::date_names_put<facet_config> >(global2));

  std::stringstream os2;
  os2.imbue(global2); 
  datef::date_put(d1, os2);
  check("check string imbued ostream: "+os2.str(), 
        os2.str() == std::string("2002-Okt-01"));

  date infin(pos_infin);
  os2.str(""); //clear stream
  datef::date_put(infin, os2);
  check("check string imbued ostream: "+os2.str(), 
        os2.str() == std::string("+unbegrenztheit"));

  os2.str(""); //clear stream
  os2 << infin;
  check("check string imbued ostream: "+os2.str(), 
        os2.str() == std::string("+unbegrenztheit"));


  date nadt(not_a_date_time);
  os2.str(""); //clear stream
  datef::date_put(nadt, os2);
  check("check string imbued ostream: "+os2.str(), 
        os2.str() == std::string("NichtDatumzeit"));
  

  std::stringstream os3;
  os3 << d1;
  check("check any old ostream: "+os3.str(), 
        os3.str() == std::string("2002-Oct-01"));

  std::ofstream f("test_facet_file.out");
  f << d1 << std::endl;
  
//   // date formatter that takes locale and gets facet from locale
  std::locale german_dates1(global, 
                            new date_facet(de_short_month_names, 
                                           de_long_month_names,
                                           de_special_value_names,
                                           de_short_weekday_names,
                                           de_long_weekday_names,
                                           '.',
                                           boost::date_time::ymd_order_dmy,
                                           boost::date_time::month_as_integer));

  os3.imbue(german_dates1); 
  os3.str("");
  os3 << d1;
  check("check date order: "+os3.str(), 
        os3.str() == std::string("01.10.2002"));

  std::locale german_dates2(global, 
                            new date_facet(de_short_month_names, 
                                           de_long_month_names,
                                           de_special_value_names,
                                           de_short_weekday_names,
                                           de_long_weekday_names,
                                           ' ',
                                           boost::date_time::ymd_order_iso,
                                           boost::date_time::month_as_short_string));

  os3.imbue(german_dates2); 
  os3.str("");
  os3 << d1;
  check("check date order: "+os3.str(), 
        os3.str() == std::string("2002 Okt 01"));

  std::locale german_dates3(global, 
                            new date_facet(de_short_month_names, 
                                           de_long_month_names,
                                           de_special_value_names,
                                           de_short_weekday_names,
                                           de_long_weekday_names,
                                           ' ',
                                           boost::date_time::ymd_order_us,
                                           boost::date_time::month_as_long_string));

  os3.imbue(german_dates3); 
  os3.str("");
  os3 << d1;
  check("check date order: "+os3.str(), 
        os3.str() == std::string("Oktober 01 2002"));

  date_period dp(d1, date_duration(3));
  os3.str("");
  os3 << dp;
  check("check date period: "+os3.str(), 
        os3.str() == std::string("[Oktober 01 2002/Oktober 03 2002]"));


  /*******************************************************************/
  /* Streaming operations for date durations                         */
  /*******************************************************************/

  date_duration dur(26);
  std::stringstream ss2;
  ss2 << dur;
  check("date_duration stream out", ss2.str() == std::string("26"));

  dur = date_duration(boost::date_time::pos_infin);
  ss2.str("");
  ss2 << dur;
  check("date_duration stream out", ss2.str() == std::string("+infinity"));

  /*******************************************************************/
  /* Streaming operations for date generator functions               */
  /*******************************************************************/

  partial_date pd(26, Jun);
  //std::stringstream ss2;
  ss2.str("");
  ss2 << pd;
  check("partial date stream out", ss2.str() == std::string("26 Jun"));

  ss2.str("");
  nth_kday_of_month nkm(nth_kday_of_month::second, Friday, Sep);
  ss2 << nkm;
  check("nth kday of month", ss2.str() == std::string("second Fri of Sep"));

  ss2.str("");
  first_kday_of_month fkm(Saturday, May);
  ss2 << fkm;
  check("first kday of month", ss2.str() == std::string("first Sat of May"));

  ss2.str("");
  last_kday_of_month lkm(Monday, Aug);
  ss2 << lkm;
  check("last kday of month", ss2.str() == std::string("last Mon of Aug"));

  ss2.str("");
  first_kday_after  fka(Thursday);//fkb.get_date(d)
  ss2 << fka;
  check("first kday after", ss2.str() == std::string("Thu after"));

  ss2.str("");
  first_kday_before fkb(Tuesday); // same ^
  ss2 << fkb;
  check("first kday after", ss2.str() == std::string("Tue before"));

  std::cout << pd << '\n'
            << nkm << '\n'
            << fkm << '\n'
            << lkm << '\n'
            << fka << '\n'
            << fkb << '\n'
            << std::endl;

#else
  check("All pass, no tests executed - Locales not supported", true);

#endif //BOOST_DATE_TIME_NO_LOCALE

  return printTestStats();

}

⌨️ 快捷键说明

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