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

📄 testtime_facet.cpp

📁 C++的一个好库。。。现在很流行
💻 CPP
📖 第 1 页 / 共 2 页
字号:

/* Copyright (c) 2004 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/03 06:55:06 $
 */

#include "boost/date_time/posix_time/posix_time.hpp"
#include "boost/date_time/testfrmwk.hpp"
#include <fstream>
#include <iostream>
#include <sstream>

template<class temporal_type, typename charT>
inline
void 
teststreaming(std::string testname, 
              temporal_type value,
              std::basic_string<charT> expected_result,
              const std::locale& locale = std::locale::classic())
{
  std::basic_stringstream<charT> ss;
  ss.imbue(locale);
  ss << value;
  check(testname, ss.str() == expected_result);
}


#if !defined(BOOST_NO_STD_WSTRING) 
static const wchar_t* long_month_names[] =
  {L"Januar",L"Februar",L"Marz",L"April",L"Mai",L"Juni",L"Juli",L"August",
   L"September",L"Oktober",L"November",L"Dezember"};
static const wchar_t* short_month_names[]=
  {L"Jan",L"Feb",L"Mar",L"Apr",L"Mai",L"Jun",L"Jul",L"Aug",
   L"Sep",L"Okt",L"Nov",L"Dez"};

std::vector<std::basic_string<wchar_t> > de_short_month_names;
std::vector<std::basic_string<wchar_t> > de_long_month_names;
#endif // 

int main() {
  using namespace boost::gregorian;
  using namespace boost::posix_time;

  try {
    date d(2004,Oct,13);
    date min_date(min_date_time);
    date max_date(max_date_time);

    date_period dp(d, d + date_duration(7));
    ptime t(d, time_duration(18,01,56));
    ptime tf = t + microseconds(3);
    time_period tp(t, tf + days(7) + time_duration(1,1,1));
    time_duration td = hours(3) + minutes(2) + seconds(1) + milliseconds(9);
    {
      std::stringstream ss;
      ss << t;
      check("Stream and to_string formats match (ptime)", 
          to_simple_string(t) == ss.str());
      std::cout << t << ' ' << td << std::endl;
      ss.str("");
      ss << tf;
      check("Stream and to_string formats match (ptime w/ fracsec)", 
          to_simple_string(tf) == ss.str());
      ss.str("");
      ss << tp;
      check("Stream and to_string formats match (time_period)", 
          to_simple_string(tp) == ss.str());
      ss.str("");
      ss << td;
      check("Stream and to_string formats match (time_duration)", 
          to_simple_string(td) == ss.str());
      std::cout << ss.str() << std::endl;

      time_facet* f = new time_facet();
      ss.imbue(std::locale(ss.getloc(), f));
      ss.str("");

      f->format("%Y-%b-%d %H:%M:%S %%d");
      f->time_duration_format("%H:%M:%S %%S");
      ss << t;
      check("Literal '%' in format", ss.str() == std::string("2004-Oct-13 18:01:56 %d"));
      ss.str("");
      ss << td;
      check("Literal '%' in time_duration format", ss.str() == std::string("03:02:01 %S"));
      ss.str("");
      f->format("%Y-%b-%d %H:%M:%S %%%d");
      f->time_duration_format("%H:%M:%S %%%S");
      ss << t;
      check("Multiple literal '%'s in format", ss.str() == std::string("2004-Oct-13 18:01:56 %13"));
      ss.str("");
      ss << td;
      check("Multiple literal '%'s in time_duration format", ss.str() == std::string("03:02:01 %01"));
      ss.str("");
    }
    { // negative time_duration tests
      std::string result;
      std::stringstream ss;
      time_duration td1(2,0,0);
      time_duration td2(1,0,0);
      ss << td2 - td1;
      result = "-01:00:00";
      check("Negative time_duration", result == ss.str());
      ss.str("");

      time_duration td3(0,2,0);
      time_duration td4(0,1,0);
      ss << td4 - td3;
      result = "-00:01:00";
      check("Negative time_duration", result == ss.str());
      ss.str("");

      time_duration td5(0,0,2);
      time_duration td6(0,0,1);
      ss << td6 - td5;
      result = "-00:00:01";
      check("Negative time_duration", result == ss.str());
      ss.str("");

#if defined(BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG)
      result = "-00:00:00.000000001";
#else
      result = "-00:00:00.000001";
#endif
      time_duration td7(0,0,0,123);
      time_duration td8(0,0,0,122);
      ss << td8 - td7;
      check("Negative time_duration: " + ss.str(), result == ss.str());

      //reset the sign to always print
      time_facet* f = new time_facet();
      ss.imbue(std::locale(ss.getloc(), f));
      f->time_duration_format("%+%H:%M:%S%F");

      ss.str("");
      ss << td4 - td3;
      result = "-00:01:00";
      check("Negative time_duration sign always: " + ss.str(), result == ss.str());

      ss.str("");
      ss << td3 - td4;
      result = "+00:01:00";
      check("time_duration sign always: " + ss.str(), result == ss.str());


#if defined(BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG)
      result = "-00:00:00.000000001";
#else
      result = "-00:00:00.000001";
#endif
      ss.str("");
      ss << td8 - td7;
      check("Negative time_duration: " + ss.str(), result == ss.str());

#if defined(BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG)
      result = "+00:00:00.000000001";
#else
      result = "+00:00:00.000001";
#endif
      ss.str("");
      ss << td7 - td8;
      check("time_duration sign bit always: " + ss.str(), result == ss.str());

      f->time_duration_format("%-%H hours and %-%M minutes");
      ss.str("");
      ss << td4 - td3;
      result = "-00 hours and -01 minutes";
      check("Negative time_duration two sign flags" + ss.str(), result == ss.str());

    }
      
#if !defined(BOOST_NO_STD_WSTRING) 
    std::copy(&short_month_names[0], 
              &short_month_names[12],
              std::back_inserter(de_short_month_names));
    
    std::copy(&long_month_names[0], 
              &long_month_names[12],
              std::back_inserter(de_long_month_names));
    

    {
      wtime_facet *timefacet = new wtime_facet(wtime_facet::standard_format);
      teststreaming("widestream default classic time", t, 
                    //std::wstring(L"Wed Oct 13 18:01:56 2004"),
                    std::wstring(L"10/13/04 18:01:56"),
                    std::locale(std::locale::classic(), timefacet));
    }
    {
      wtime_facet *timefacet = new wtime_facet(wtime_facet::standard_format);
      teststreaming("widestream default classic time with fractional seconds truncated", t, 
                    //std::wstring(L"Wed Oct 13 18:01:56 2004"),
                    std::wstring(L"10/13/04 18:01:56"),
                    std::locale(std::locale::classic(), timefacet));
    }
    {
      wtime_facet *timefacet = new wtime_facet(wtime_facet::standard_format);
      teststreaming("widestream default time period with fractional seconds truncated", tp, 

⌨️ 快捷键说明

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