C++ | boost库之时间与日期

目录

一、timer库

1.timer (V1)(不推荐使用)

(1) timer

(2) progress_timer

2.cpu_timer(V2)

二、date_time库

1.处理日期的gregorian

(1)创建日期对象

(2)判断日期是否为特殊日期

(3)访问日期

(4) 日期的输出

(5)boost::gregorian::date与C语音的tm互转

(6)日期长度

(7)日期区间

(8)日期迭代器

(9)其他常用函数

2.处理时间的posix_time

(1)时间长度

(2)时间长度的输出

(3)操作时间长度

(4)自定义毫秒单位

(5)创建时间点

(6)操作时间点对象

 (7)时间区间

(8)时间迭代器

(9)日期与时间的格式化

三、相关宏定义


一、timer库

Boost 1 . 48 版以后的 timer 库由两个组件组成:早期的 timer (V1) 和新的 cpu_timer (V2),前者使用的是标准 C / C ++ 库函数,而后者则基于chrono库使用操作系统的 API , 其计时精度更高。V1 版的 timer 组件的计时精度低 , Boost 官方已经不再推荐使用 ( 编译会有告警 )。

1.timer (V1)(不推荐使用)

        timer(V1)库包含两个小组件,分别是timer和progress_timer。

(1) timer

        timer对象一旦声明,它的构造函数就“启动”了计时工作。用elapsed()函数可以简单测量自对象创建之后所用的时间。elapsed_min()用于返回 timer 能够测量的最小时间范围。elapsed_max()用于返回 timer 能够测量的最大时间范围。它们的单位都是秒。

        timer是小型的计时器,不适用于高精度的时间测量任务,它的精度依赖操作系统或编译器,难以做到跨平台。timer也不适用于测量大跨度时间段,如果需要以天、月,甚至年为时间单位则不能使用timer,应使用的cpu_timer组件。

#include <iostream>
#include <Windows.h>
#include "boost/timer.hpp"

void TestTimer()
{
    boost::timer _timer;
    std::cout << _timer.elapsed_min() << std::endl;
    std::cout << _timer.elapsed_max() << std::endl;
    Sleep(1000);
    std::cout << _timer.elapsed() << std::endl;
}

int main()
{
    TestTimer();
}

(2) progress_timer

        progress_timer也是一个计时器,它派生自timer,会在析构时自动输出时间,省去了timer手动调用elapsed()的工作,是一个相当方便的自动计时的小工具。progress_timer 的一个有趣的特点是私有继承了noncopyable类,以防止被无意的拷贝而破坏了正确的行为。 

        progress_timer唯一需要注意的是其构造函数,它允许将析构时的输出定向到指定的输入输出流里,默认是 std::cout。如果有特别的需求,可以用其他标准库输出流(ofstream 、ostringstream) 将其替换,或者用cout.rdbuf ( ) 重定向cout 的输出。

#include <iostream>
#include <Windows.h>
#include "boost/progress.hpp"
#include <sstream>

void TestProcessTimer()
{
    {
        boost::progress_timer _timer;
        std::cout << _timer.elapsed_min() << std::endl;
        std::cout << _timer.elapsed_max() << std::endl;
        Sleep(1000); 
    }
    //将progress_timer的输出转移到stringstream中
    std::stringstream ss;
    {
        boost::progress_timer _timer(ss);
        std::cout << _timer.elapsed_min() << std::endl;
        std::cout << _timer.elapsed_max() << std::endl;
        Sleep(1000);  
    }
    std::cout << ss.str() << std::endl;
}

int main()
{
    TestProcessTimer();
}

2.cpu_timer(V2)

//todo

二、date_time库

        date_time库需要编译才能使用。它是一个非常全面且灵活的日期时间库,基于我们日常使用的公历(格里高利历),可以提供与时间相关的各种所需功能,如精确定义时间点、时间段和时间长度、加减若干天/月/年、日期迭代器等。date_time库还支持无限时间和无效时间这种在实际生活中有用的概念,而且它可以与 C 语言的传统时间结构 tm 相互转换,提供向下支持。

        date_time库中用枚举special_values定义了特殊的时间概念(boost::date_time),并被 using 语句引入其他子名字空间。

  • pos_infin :表示正无限。
  • neg_infin :表示负无限。
  • not_a_date_time :无效时间。
  • min_date_time :可表示的最小日期或时间。
  • max_date_time :可表示的最大日期或时间。

        date_time库包含两个部分,分别是处理日期的gregorian和处理时间的posix_time。

1.处理日期的gregorian

(1)创建日期对象

        a.空构造函数创建一个无效日期。

        b.顺序传入年、月、日值(支持英文)

        c.从字符串转化

        d.使用day_clock的local_day()或者universal_day()创建本地日期或UTC日期

        e.使用特殊的时间概念枚举创建日期对象

#include "boost/date_time/gregorian/gregorian.hpp" 

boost::gregorian::date _date1;//一个无效日期
boost::gregorian::date _date2(2021, 9, 16);//使用数字构造日期
boost::gregorian::date _date3(2021, boost::gregorian::Sep, 16);//使用英文构造日期
boost::gregorian::date _date4(_date2);//拷贝构造
boost::gregorian::date _date5 = boost::gregorian::from_string("2021/09/16");//字符串转化,使用分隔符(-或/)
boost::gregorian::date _date6 = boost::gregorian::from_undelimited_string("20210916");//字符串转化,无分隔符
boost::gregorian::date _date7 = boost::gregorian::day_clock::local_day();//获取当天日期对象
boost::gregorian::date _date8(boost::date_time::min_date_time);//使用特殊时间枚举创建日期

(2)判断日期是否为特殊日期

    if (_date1 == (boost::gregorian::date)boost::date_time::not_a_date_time)
    {
        std::cout << "_date1 是一个无效日期" << std::endl;
    }

    if (_date1.is_not_a_date())
    {
        std::cout << "_date1 是一个无效日期" << std::endl;
    }

(3)访问日期

        a.成员函数year()、month()和day()分别返回日期的年、月、日。

        b.成员函数year_month_day()返回一个boost::gregorian::date::ymd_type结构,可以一次性获取年、月、日数据。

        c.成员函数day_of_week () 返回 date 的星期数,0表示星期天。

        d.成员函数day_of_year () 返回 date 是当年的第几天 ( 最大值是 366)。

        e.成员函数end _ of _ month ( ) 返回当月的最后一天的 date对象。

        f.成员函数 week _ number ( ) 返回 date 所在的周是当年的第几周 , 其范围是0 ~ 53。

std::cout << _date6.year() << _date6.month() << _date6.day() << std::endl;//2021Sep16
std::cout << "2021/09/16是2021年的第" << _date6.day_of_year() << "天" << std::endl;//259
std::cout << "2021/09/16是星期几?" << _date6.day_of_week() << std::endl;//Thu
std::cout << "2021/09/16所在周是2021年的第几周?" << _date6.week_number() << std::endl;//37

boost::gregorian::date::ymd_type _type = _date6.year_month_day();
std::cout << _type.year << _type.month << _type.day << std::endl;//2021Sep16

boost::gregorian::date _date9 = _date6.end_of_month();
std::cout << "2021年9月的最后一天:" << _date9 << std::endl;//2021-Sep-30

(4) 日期的输出

        a.to_simple string():转换为 YYYY-mmm-DD 格式的字符串 , 其中 , mm 而为 3 字符的英文月份名。
        b.to_iso_string ():转换为 YYYYMMDD 格式的数字字符串。
        c.to_iso_extended_string() :转换为 YYYY-MM-DD 格式的数字字符串。
        d.date也支持流输入输出,默认使用 YYYY-mmm-DD 格式。

std::cout << _date5 << std::endl;//2021-Sep-16
std::cout << boost::gregorian::to_simple_string(_date7) << std::endl;//2021-Sep-16
std::cout << boost::gregorian::to_iso_string(_date7) << std::endl;//20210916

(5)boost::gregorian::date与C语音的tm互转

         tm结构:年份是从1900年起至今多少年,而不是直接存储;月份从0开始的,0表示一月;星期也是从0开始的, 0表示星期日,1表示星期一 。

   // boost::gregorian::date转tm
   tm _tm= boost::gregorian::to_tm(_date9);
   std::cout << _tm.tm_year << "-" << _tm.tm_mon << "-" << _tm.tm_mday << std::endl;//121-8-30
   //tm转boost::gregorian::date
   boost::gregorian::date _date10= boost::gregorian::date_from_tm(_tm);
   std::cout << _date10 << std::endl;//2021-Sep-30

(6)日期长度

        日期长度是以天为单位的时长,是度量时间长度的一个标量。它与日期不同,其值可以是任意整数,可正可负。日期长度支持加减运算,基本的日期长度类是date_duration。date_duration 可以使用构造函数创建一个日期长度,如果传入特殊时间枚举值则会构造出一个特殊时长对象。

        a.成员函数 days () 返回时长的天数。

        b.成员函数is_special () 和 is_negative () 可以判断date_duration 对象是否为特殊值,是否为负值。

        c.成员函数unit () 返回时长的最小单位,即date_duration (1)。

        date_duration有一个新的名字叫days;date_time库还提供其他的时间长度week_duration、month_duration、year_duration,并且为他们定义了新的名字:

  typedef date_duration days;
  typedef weeks_duration weeks;
  typedef date_time::months_duration<greg_durations_config> months;
  typedef date_time::years_duration<greg_durations_config> years;

        weeks_duration是date_duration的子类(weeks是days的子类),除构造函数以7为单位外,其他的行为与days完全相同。

    //日期长度
    boost::gregorian::date_duration duration(boost::gregorian::neg_infin);
    if (duration.is_special())
    {
        std::cout << "duration是一个特殊值" << std::endl;
    }
    if (duration.is_negative())
    {
        std::cout << "duration是一个负值" << std::endl;
    }
    std::cout << duration.days() << std::endl;//-2147483648
    std::cout << duration.unit() << std::endl;//1

    //boost::gregorian::days 
    boost::gregorian::days _day1(10);//10天
    boost::gregorian::days _day2(20);//20天
    std::cout << "两个天时长相加" << _day1 + _day2 << std::endl;//30

    //boost::gregorian::weeks
    boost::gregorian::weeks _weeks1(5);//5周
    boost::gregorian::weeks _weeks2(6);//6周
    std::cout << "两个周时长相加" << _weeks1 + _weeks2 << std::endl;//77
    
    //boost::gregorian::years
    boost::gregorian::years _year1(2);//2年
    boost::gregorian::years _year2(1);//1年
    std::cout << "两个年份时长相加" << _year1.number_of_years() + _year2.number_of_years() << std::endl;//3年

    //boost::gregorian::months
    boost::gregorian::months _months1(2);//2个月
    boost::gregorian::months _months2(1);//1个月
    std::cout << "两个月份时长相加" << _months1.number_of_months() + _months2.number_of_months() << std::endl;//3月
    std::cout << "月份时长与年份时长相加:" << (_months1 + _year1).number_of_months() << std::endl;//26

    //时间点相减、时间点与时间段相加
    boost::gregorian::date today= boost::gregorian::day_clock().local_day();
    boost::gregorian::date midAutumnDay(2021, 9, 21);
    std::cout << "距离中秋节还有:" << midAutumnDay - today<< "天" << std::endl;//4
    boost::gregorian::days fiveDay(5);
    std::cout << "五天之后是" << today+ fiveDay << std::endl;//2021-Sep-22

(7)日期区间

        date_time 库使用 date_period 来表示日期区间的概念,它是时间轴上的一个左闭右开的区间,其端点是两个 date 对象。日期区间的左边界必须小于右边界,否则 date_period 将表示一个无效的日期区间。

        构造日期区间有两种方法:指定区间的两个端点构造区间、指定左端点再加上时长构造区间。通常后一种方法比较常用,这相当于在生活中从某天开始的一个周期。

a. 成员函数 begin( )和 last( )返回日期区间的两个端点。

b. 成员函数end ( ) 返回 last( )后的第一天,与标准容器中的end ( )含义相同,这是一个 " 逾尾的位置 "。

c. 成员函数length ( ) 返回日期区间的长度,以天为单位。

d. 成员函数is_nulI ( )如果在构造日期区间时使用了左大右小的端点或日期长度是 0,那么 is_nulI ( ) 函数将返回 true。

e. 成员函数shift ( ) 将日期区间平移 n 天而长度不变。

f. 成员函数expand ( ) 将日期区间向两端延伸 n 天 , 相当于区间长度增加 2n 天。

g. 成员函数is_before ( ) / is_after ( ) :日期区间是否在日期前或后。

h. 成员函数contains ( ) :日期区间是否包含另一个区间或日期。

i. 成员函数intersects ( ) :两个日期区间是否存在交集。

j. 成员函数intersection ( ) :返回两个区间的交集,如果无交集,则返回一个无效区间。

k. 成员函数is_adjacent ( ) :两个日期区间是否相邻。

l. 成员函数merge ( ) ︰返回两个日期区间的并集,如果日期区间无交集或不相邻,则返回无效区间。

m. 成员函数span ( ) ︰合并两个日期区间及两者间的间隔,相当于广义的 merge()。

    boost::gregorian::date today= boost::gregorian::day_clock().local_day();
    boost::gregorian::days fiveDay(5);
    boost::gregorian::date midAutumnDay(2021, 9, 21);

    //日期区间
    boost::gregorian::date_period _period1(today, fiveDay);
    std::cout << _period1.last() << std::endl;//2021-Sep-21
    std::cout << _period1.end() << std::endl;//2021-Sep-22

    _period1.expand(boost::gregorian::days(2)); //向两端延申两天
    std::cout << _period1.begin() << std::endl;//2021-Sep-15
    std::cout << _period1.end() << std::endl;//2021-Sep-24

    _period1.shift(boost::gregorian::days(2)); //区间向后平移两天
    std::cout << _period1.begin() << std::endl;//2021-Sep-17
    std::cout << _period1.end() << std::endl;//2021-Sep-26

    boost::gregorian::date_period _period2(today, midAutumnDay);
    std::cout << _period2.length() << std::endl;//4
    std::cout << _period2 << std::endl;//[2021-Sep-17/2021-Sep-20]

    if (_period2.is_before(midAutumnDay))
    {
        std::cout << "[2021-Sep-17/2021-Sep-20]在中秋节之前" << std::endl;
    }
    std::cout << _period1 << "与" << _period2 << "是否存在交集?" << _period1.intersects(_period2) << std::endl;
    std::cout << _period1 << "与" << _period2 << "的并集:" << _period1.merge(_period2) << std::endl;
    std::cout << _period1 << "与" << _period2 << "合并:" << _period1.span(_period2) << std::endl;

(8)日期迭代器

         日期迭代器包括 day_iterator、week_iterator、month_iterator 和year_iterator , 它们分别以天、周、月和年为单位。
        日期迭代器的用法基本类似,都需要在构造时传入一个起始日期和增减步长 (默认是1个单位 ) , 然后就可以用operator ++ 、 operator -- 变化日期(考虑到运算效率,日期迭代器只提供前置式
的 ++ 、 -- 操作符,不提供后置式操作符)
。迭代器相当于一个 date 对象的指针,可以用解引用操作符*获得日期迭代器当前的日期对象,也可以用 ->直接调用日期对象的成员函数。

    boost::gregorian::date today = boost::gregorian::day_clock::local_day();
    std::cout << today << std::endl;//2021-Sep-17
    boost::gregorian::day_iterator _iterator(today, 7);
    ++_iterator;
    std::cout << *_iterator << std::endl;//2021-Sep-24
    std::cout << today << std::endl;//注意today并没有变:2021-Sep-17

(9)其他常用函数

        boost::gregorian::gregorian_calendar类有两个常用的函数。

a. 成员函数is_leap_year ( )可以判断年份是否是闰年。

b. 成员函数end_of_month_day ( ) 通过给定年份和月份返回该月的最后一天。

boost::gregorian::gregorian_calendar _calendar;
std::cout << "2021是闰年吗?" << _calendar.is_leap_year(2021) << std::endl;//0
std::cout << "2021年2月最后一天:" << _calendar.end_of_month_day(2021, 2) << std::endl;//28

2.处理时间的posix_time

        时间对象是日期的进一步细化,在日期的基础上增加时间长度即可得到时间对象。date_time 库在格里高利历的基础上提供了微秒级别的时间系统,如果在头文件之前定义了宏#define BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG,它可以达到纳秒级别的精确度。

(1)时间长度

a.空构造函数创建一个0时0分0秒的时间长度。

b.顺序传入时、分、秒、微秒(纳秒)

c.从字符串转化(用冒号隔开)

d.使用特殊的时间概念枚举创建时间长度

e.通过加减乘除创建时间长度。

#include "boost/date_time/posix_time/posix_time.hpp"

boost::posix_time::time_duration _duration1;
boost::posix_time::time_duration _duration2(1, 1, 1, 100);
boost::posix_time::time_duration _duration3(boost::posix_time::duration_from_string("1:60:60:000001"));
boost::posix_time::time_duration _duration4(boost::posix_time::pos_infin);
boost::posix_time::time_duration _duration5(boost::posix_time::hours(5) + boost::posix_time::minutes(10));

(2)时间长度的输出

std::cout << "_duration1:" << _duration1 << std::endl;
std::cout << "_duration2:" << _duration2 << std::endl;
std::cout << "_duration3:" << _duration3 << "   " << _duration3.hours() << "h" << _duration3.minutes() << "m" << _duration3.seconds() << "s" << std::endl;
std::cout << "_duration4:" << _duration4 << std::endl;
std::cout << "_duration5:" << _duration5 << std::endl;
std::cout << "_duration2转字符串:"<<boost::posix_time::to_simple_string(_duration2) << std::endl;

//没有添加#define BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG宏定义的结果如下:
_duration1:00:00:00
_duration2:01:01:01.000100
_duration3:02:01:00.000001   2h1m0s
_duration4:+infinity
_duration5:05:10:00
_duration2转字符串:01:01:01.000100

//添加了#define BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG宏定义的结果如下:
_duration1:00:00:00
_duration2:01:01:01.000000100
_duration3:02:01:00.000001000   2h1m0s
_duration4:+infinity
_duration5:05:10:00
_duration2转字符串:01:01:01.000000100

(3)操作时间长度

1.成员函 数hours( )、minutes( )、seconds( )可以访问时、分、秒。

2.成员函数total_seconds( )、total_milliseconds( )、total _ microseconds( )分别返回时间长度的总秒数、总毫秒数和总微秒数。

3.成员函数fractional_seconds( )以Iong返回微秒数 。

4.成员函数is_positive( )、is_negative( )判断是否为正、负时间长度。

5.成员函数 resolution ( )返回―个枚举值time_resolutions,表示时间长度的分辨率。

  enum time_resolutions {
    sec,
    tenth,
    hundreth, // deprecated misspelled version of hundredth
    hundredth = hundreth,
    milli,
    ten_thousandth,
    micro,
    nano,
    NumResolutions
  };

6.成员函数 num_fractional_digits( ) 返回秒的小数部分的位数 ( 微秒为6位,纳秒为9位)。

7.自由函数 to_simple_string ( ) 和 to_iso_string( ) 分别返回HH:MM:SS.ffffffffff 和HHMMSS,fffffffff 格式的字符串。

8.通过自由函数to_tm( )函数可以将time_duration转换到tm结构,但不能进行反向转换。

std::cout << "_duration2总秒数:" << _duration2.total_seconds() << std::endl;
std::cout << _duration2.fractional_seconds() << std::endl;
if (_duration4.is_positive())
{
    std::cout << "_duration4 是个正数" << std::endl;
}
std::cout << "时间精度,枚举:" << _duration2.resolution() << std::endl;
std::cout << "时间精度,小数部分的位数:" << _duration2.num_fractional_digits() << std::endl;

std::cout << "_duration2转字符串:" << boost::posix_time::to_simple_string(_duration2) << std::endl;
tm _tm = boost::posix_time::to_tm(_duration3);
std::cout << _tm.tm_hour << _tm.tm_min << _tm.tm_sec << std::endl;


//输出结果:默认微秒
_duration2总秒数:3661
100
_duration4 是个正数
时间精度,枚举:5
时间精度,小数部分的位数:6
_duration2转字符串:01:01:01.000100
210

(4)自定义毫秒单位

//自定义毫秒单位
boost::posix_time::time_duration::tick_type my_millisec = boost::posix_time::time_duration::ticks_per_second() / 1000;
boost::posix_time::time_duration _my_duration(1, 60, 60, 10 * my_millisec);//10毫秒
std::cout << _my_duration << std::endl;

//输出结果
02:01:00.010000

(5)创建时间点

a.空构造函数创建当天零点的时间点。

b.在构造函数中指定date和time_duration对象,时间点即日期加时间长度的偏移量。

c.从字符串构造。

d.使用特殊的时间概念枚举创建时间点。

e.使用boost::posix_time::second_clock/microsec_clock获取当前时间。

    boost::posix_time::ptime _ptime0;

    boost::gregorian::date today = boost::gregorian::day_clock::local_day();
    boost::posix_time::time_duration duration(14, 26, 22);
    boost::posix_time::ptime _ptime1(today, duration);

    boost::posix_time::ptime _ptime2 = boost::posix_time::time_from_string("2021-9-22 14:26:50");
    boost::posix_time::ptime _ptime3 = boost::posix_time::from_iso_string("20210922T142730");

    boost::posix_time::ptime _ptime4 = boost::posix_time::second_clock::local_time();
    boost::posix_time::ptime _ptime4_1 = boost::posix_time::second_clock::universal_time();
    boost::posix_time::ptime _ptime5 = boost::posix_time::microsec_clock::local_time();

    boost::posix_time::ptime _ptime6(boost::posix_time::pos_infin);

(6)操作时间点对象

        由于 ptime 相当于 date + time _ duration ,所以对它的操作可以分解为对这两个组成部分的操作。

1.使用成员函数date()和 time_of_day()获得时间点中的日期长度和时间长度,然后就可
以分别对其进行处理。

2.使用自由函数可将ptime转换为字符串(其中的 ffffff 是秒的小数部分,如果为 0 则不显示,T 是日期与时间的分隔符。)
        a.to_simple_string( ) 转换为YYYY-mmm-DD HH:MM:SS.ffffff格式。

        b.to_iso_string( )转换为YYYYMMDDTHHMMSS,ffffff格式。

        c.to_iso_extended_string( )转换为YYYY-MM-DDTHH:MM:SS,ffffff格式。

3.使用自由函数boost::posix_time::to_tm( )与boost::posix_time::ptime_from_tm( )可以进行ptime与tm互转。

    std::cout << boost::gregorian::to_iso_string(_ptime1.date()) << " " << _ptime1.time_of_day() << std::endl;
    std::cout << boost::posix_time::to_iso_string(_ptime5) << std::endl;
    std::cout << boost::posix_time::to_simple_string(_ptime5) << std::endl;
    std::cout << boost::posix_time::to_iso_extended_string(_ptime5) << std::endl;

    tm _tm = boost::posix_time::to_tm(_ptime5);
    std::cout << _tm.tm_year << " " << _tm.tm_mon << " " << _tm.tm_mday << " "<< _tm.tm_hour << " " << _tm.tm_min << " " << _tm.tm_sec << std::endl;
    boost::posix_time::ptime _ptime= boost::posix_time::ptime_from_tm(_tm);
    std::cout << _ptime << std::endl;

 (7)时间区间

        时间区间time_period与日期区间date_period用法基本相同。

boost::posix_time::time_period _time_period1(_ptime1, _ptime4);
std::cout << _time_period1 << std::endl;
_time_period1.shift(boost::posix_time::time_duration(2, 0, 0));
std::cout << _time_period1 << std::endl;
_time_period1.expand(boost::posix_time::time_duration(0, 30, 0));
std::cout << _time_period1 << std::endl;

//输出结果
[2021-Sep-22 14:26:22/2021-Sep-22 20:28:33.999999]
[2021-Sep-22 16:26:22/2021-Sep-22 22:28:33.999999]
[2021-Sep-22 15:56:22/2021-Sep-22 22:58:33.999999]

(8)时间迭代器

        不同于日期迭代器,时间迭代器只有一个time_iterator。它在构造时传入一个起始时间点ptime对象和一个步长 time_duration 对象,然后就同日期迭代器一样使用前置式++ 、-- 来递增或递减时间,使用解引用操作符返回一个 ptime 对象。

        time_iterator也可以直接与 ptime 比较,无须再使用解引用操作符。

boost::posix_time::time_iterator _time_iterator(_ptime1,boost::posix_time::time_duration(2, 0, 0));
std::cout << _ptime1 << std::endl;
++_time_iterator;
std::cout << *_time_iterator << std::endl;
std::cout << _ptime1 << std::endl;

if (_time_iterator > _ptime1)//if(*_time_iterator > _ptime1)也成立
{
    std::cout << "_time_iterator > _ptime1" << std::endl;
}

//输出结果
2021-Sep-22 14:26:22
2021-Sep-22 16:26:22
2021-Sep-22 14:26:22
_time_iterator > _ptime1

(9)日期与时间的格式化

boost::posix_time::time_facet* _facet = new  boost::posix_time::time_facet("%Y年 %m月 %d日 %H时 %M分 %S%F秒");
std::cout.imbue(std::locale( std::cout.getloc(), _facet));
std::cout << _ptime5 << std::endl;

//输出结果
2021年 09月 22日 20时 34分 26.768862秒

三、相关宏定义

1. #define BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG

        将启用 date_time 库更高的时间精确度,由微秒变为纳秒,同时与纳秒相关的一些函数和类也会被启用。在默认情况下此宏是关闭的,因为纳秒精度通常很依赖操作系统,而且在实际生活中很少用到这么高的精确度。

2. #define DATE_TIME_NO_DEFAULT_CONSTRUCTOR

        可以禁止编译器创造出 date 和 ptime 的默认构造函数,强制在构造它们时必须有一个有效的值,从而避免因某些疏忽而导致的错误。

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

烫青菜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值