C++ Boost库:日期时间库 date_time

C++ Boost库:简介和第一个示例程序
C++ Boost库:数值转换 lexical_cast
C++ Boost库:字符串格式化 format
C++ Boost库:字符串string_algo
C++ Boost库:字符串算法string_algo
C++ Boost库:类型推导BOOST_AUTO/BOOST_TYPEOF
C++ Boost库:分词处理库 tokenizer
C++ Boost库:windows下编译Boost库
C++ Boost库:日期时间库 date_time
C++ Boost库:智能指针scoped_ptr
C++ Boost库:数组智能指针 scoped_array
C++ Boost库:共享所有权的智能指针 shared_ptr
C++ Boost库:工厂函数 make_shared
C++ Boost库:共享有权的数组智能指针shared_array
C++ Boost库:弱引用智能指针 weak_ptr
C++ Boost库:禁止拷贝 nocopyable
C++ Boost库:计时器 timer
C++ Boost库:普通数组array
C++ Boost库:散列容器 unordered_set、unordered_multiset
C++ Boost库:散列容器 unordered_map、unordered_multimap
C++ Boost库:双向映射容器 bimap
C++ Boost库:环形缓冲区 circular_buffer
C++ Boost库:动态多维数组 multi_array
C++ Boost库:使用property_tree解析XML和JSON
C++ Boost库:简化循环 BOOST_FOREACH
C++ Boost库:随机数库 Random
C++ Boost库:引用库 ref
C++ Boost库:绑定库 bind
C++ Boost库:线程库 thread 跨平台多线程
C++ Boost库:互斥量 mutex

1. 简介

date_time库提供了时间日期相关的计算、格式化、转换、输入输岀等功能,为C++的日期时间编程提供了极大便利。

头文件:

#include<boost/date_time/gregorian/gregorian.hpp>

名字空间:

using namespace  boost::gregorian;

静态库:

#pragma  comment(lib, "libboost_date_time-vc100-mt-gd-x32-1_67.lib")

date_duration类:

date_duration表示日期长度,是以天为单位的时长,值可以是任意的整数可正可负。date_time库为date_duration定义了一个常用的 typedef:days,此外还提供了 monthsyearsweeks等另外三个时长类;分别用来表示月,年和星期。

2. 基本使用

#include<iostream>
using namespace std;   

#include<boost/date_time/gregorian/gregorian.hpp>
using namespace  boost::gregorian;

#pragma  comment(lib, "libboost_date_time-vc100-mt-gd-x32-1_67.lib")

int  main()
{ 
   //构造日期
   date   d1(2000, 1, 1);
   date   d2 = from_string("2010-02-02");
   date   d3 = from_string("2010/03/03");
   date   d4 = from_undelimited_string("20100404");//无界定的字符串

   date  d5(min_date_time);//特殊时间值1400-01-01
   date  d6(max_date_time);//特殊时间值9999-12-31

   //输出日期
   cout << d1.year() << "年" <<(int) d1.month() << "月" << d1.day() << endl;
   cout << to_simple_string(d2) << endl;//2010-Feb-02
   cout << to_iso_string(d3) << endl;//20100303
   cout << to_iso_extended_string(d4) << endl;//2010-03-03
   cout << to_iso_string(d5) << endl;
   cout << to_iso_string(d6) << endl;

   //获取今天的日期
   date  today = day_clock::local_day();

   cout << "年"<< today.year() << endl;
   cout << "月"<< today.month() << endl;
   cout << "日"<< today.day() << endl;
   cout <<"星期"<< today.day_of_week() << endl; 
   cout << "一年中的第几周" << today.week_number() << endl;
   cout << "一年中的第多少天" << today.day_of_year() << endl;
   cout << "这个月的结束日期" << today.end_of_month() << endl;

   //时间长度(最小单位是 天)
   days  day1(10);
   days  day2(-5);
   date_duration  du1  = day1 + day2;
   cout <<"10天-5天"<< du1.days() << endl;

   weeks  w1(1);
   cout << "1周的天数" << w1.days() << endl;
   date_duration  du2 = w1 + day1;
   cout << "1周+ 10天" << du2.days() << endl;

   months   mon1(1);//一个月无法知道是多少天 ,并且无法加天数
   cout << "1个月" << mon1.number_of_months() << endl;

   years  year1(1);//一年无法知道是多少天,并且无法加天数
   cout << "1年" << year1.number_of_years()<< endl;

   //如何知道一年多少个月
   months mon2= year1 + months(0);
   cout << "1年多少个月" << mon2.number_of_months() << endl;

   //日期+  日期长度 = 新日期 ,很实用
   date   d10(2018, 1, 31);

   cout << "今天往前推10天是几月几日:" ;
   cout <<  to_iso_extended_string(  d10 + days(-10) ) << endl;

   cout << "今天往后加2周是几月几日:";
   cout << to_iso_extended_string(d10 + weeks(2)) << endl;

   cout << "今天往后推一个月是几月几日:";
   cout << to_iso_extended_string(d10 + months(1)) << endl;

   cout << "今天往后推一年月是几月几日:";
   cout << to_iso_extended_string(d10 + years(1)) << endl;

   getchar();
   return 0;
}

执行效果:

image-20210504135351841

3. date_period类

date_period类用来表示日期范围的概念,它是时间轴上一个左闭右开区间,端点是两个date对象。

日期迭代器:

日期迭代器可以用++--操作符连续访问日期,这些迭代器包括:day_iteratorweek_iteratormonth_iteratoryear_iterator注意:它们并不符合标准迭代器的定义,没有difference_typepointerreference等内部类型定义,不能使用std::advance()或者+=来前进或者后退。

示例代码:

#include<iostream>
using namespace std;   
  
#include<boost/date_time/gregorian/gregorian.hpp>
using namespace  boost::gregorian;

#pragma  comment(lib, "libboost_date_time-vc100-mt-gd-x32-1_67.lib")

int  main()
{ 
   date  d1(2000, 1, 1);
   date  d2(2000, 2, 1);

   //构造一个日期范围
   date_period  dp1(d1, d2);//两个日期确定一个范围
   cout << dp1 << endl;//左闭右开,不包含2000/2/1
   cout << dp1.length() << endl;//返回这个范围的天数

   //把日期范围平移
   dp1.shift(date_duration(10)); 
   cout << dp1 << endl;//向后移动10天

   //把日期向两边延伸
   dp1.expand(date_duration(1));//向两边移动一天
   cout << dp1 << endl; 

   //检测某个日期是否在这个日期范围里面
   cout << dp1.contains(  date(2000,2,20)  ) << endl;


   //日期迭代器

   date  d3(2000, 1, 1);
   day_iterator  it1(d3,10);//默认步长是1
   ++it1;//不会影响原日期,只是输出结果
   ++it1;
   cout <<"day_iterator   "<< *it1 << endl;


   week_iterator  it2(d3, 2);
   --it2;//向前移动2周
   cout << "week_iterator   " << *it2 << endl;

   month_iterator  it3(d3);
   --it3;//向前移动1月
   cout << "month_iterator   " << *it3 << endl;
    
   year_iterator  it4(d3, 10);
   ++it4;//向后移动10年
   cout << "month_iterator   " << *it4 << endl;

   return 0;
}

运行结果:

image-20210504140312585

4. ptime类

date_time库它包含两个组件,处理日期的组件 gregorian和处理时间的组件 posix_timedate类用于创建日期,ptime类则用于定义一个时间。

ptimedate_time库处理时间的核心类,它使用64位(微秒)或96位(纳秒)的整数在内部存储时间数据。

头文件:

#include<boost/date_time/posix_time/posix_time.hpp>

名字空间:

using namespace  boost::posix_time;

示例代码:

#include<iostream>
using namespace std;   
  
//日期组件
#include<boost/date_time/gregorian/gregorian.hpp>
using namespace  boost::gregorian;

//时间组件
#include<boost/date_time/posix_time/posix_time.hpp>
using namespace  boost::posix_time;

#pragma  comment(lib, "libboost_date_time-vc100-mt-gd-x32-1_67.lib")

int  main()
{ 
   //时间的构造
   ptime  t1 = time_from_string("2000-01-01 10:00:02");
   cout << t1 << endl;

   ptime  t2 = from_iso_string("20000101T100003");//用字母T分割日期时间
   cout << t2 << endl;
    
   ptime   t3 = second_clock::local_time();//秒精度
   cout << t3 << endl;

   ptime   t4 = microsec_clock::local_time();//微秒精度
   cout << t4 << endl;

   ptime   t5 (min_date_time);//特殊值
   cout << t5 << endl;

   ptime   t6(max_date_time);//特殊值
   cout << t6 << endl;

   //类似日期长度date_duration,也有时间长度

   //1小时10分钟20秒99毫秒
   time_duration  td1(1, 10, 20, 99*1000); //最后一个参数的单位是微秒
   cout << td1 << endl;


   //1小时10分钟21秒 (自动往前进位)
   time_duration  td2(1, 10, 20,1000 * 1000); //最后一个参数的单位是微秒
   cout << td2 << endl;

   //1小时10分钟20秒  1微秒 
   time_duration  td3=  duration_from_string("1:10:20:000001"); //最后一个参数的单位是微秒
   cout << td3 << endl; 

   //hours
   hours  h(1);
   cout << h.total_seconds() << "秒" << h.total_milliseconds() << "毫秒"
   	<< h.total_microseconds() << "微秒" << h.total_nanoseconds() << "纳秒" << endl;

   //利用时间长度构造
   time_duration td4 = hours(1) + minutes(10) + seconds(20) +microsec(30)+ microseconds(10);
   cout << td4 << endl;


   return 0;
}

运行结果:

image-20210504141053284

5. time_period类

类似日期区间类 date_periodtime_period表示时间轴上一个左闭右开区间,端点是两个ptime对象。

日期迭代器:

类似日期迭代器,时间也有迭代器,但是只有一个time_iterator;可以用++--操作符连续访问时间。

示例代码:

#include<iostream>
using namespace std;   

//日期组件
#include<boost/date_time/gregorian/gregorian.hpp>
using namespace  boost::gregorian;

//时间组件
#include<boost/date_time/posix_time/posix_time.hpp>
using namespace  boost::posix_time;

#pragma  comment(lib, "libboost_date_time-vc100-mt-gd-x32-1_67.lib")

int  main()
{ 
   //起始时间
   ptime  p1 = time_from_string("2000-01-01 00:00:01");
   //结束时间
   ptime  p2(date(2000, 1, 2));// 默认是 2000-01-01 23:59:59.999;

   //产生一个时间范围,   类似 日期范围  date_period的用法
   time_period  tp(p1, p2);//  p2>p1才可以
   cout << tp << endl;

   time_duration  td = tp.length();//返回时间长度
   cout << td<< endl;

   //把时间范围向后平移9秒
   tp.shift(seconds(9));
   cout << tp << endl;

   //把时间范围向两边扩大1小时
   tp.expand(hours(1));
   cout << tp << endl;

   //某个时间是否在这个时间范围内
   ptime  p3 = time_from_string("2010-01-01 00:00:01");
   cout << tp.contains(p3) << endl;


   //----------------------------------------------------------

   //类似日期迭代器year_iterator, month_iterator等,时间迭代器time_iterator
   ptime  p4 = time_from_string("2000-01-01 00:00:01");
   time_iterator  it(p4, seconds(10));//以10秒为步长

   for (int i = 0; i < 3; ++i) ++it; //迭代器的移动不会影响p4的值

   cout <<"时间迭代器向后移动30秒" <<*it << endl;


   time_iterator  it2(p4, hours(24));//以24小时为步长
   ++it2;//迭代器的移动不会影响p4的值
   cout << "时间迭代器向后移动24小时" << *it2 << endl;

   //日期时间格式化
   date  d1 = day_clock::local_day();
   date_facet  *pDF = new  date_facet("%Y年  %m月  %d日");
   cout.imbue(locale(cout.getloc(), pDF));//指定输出流的语言环境
   cout << d1 << endl;

   ptime  t1 = microsec_clock::local_time();
   time_facet  *pTF = new  time_facet("%Y年  %m月  %d日   %H:%M:%S  %F");
   cout.imbue(locale(cout.getloc(), pTF));//指定输出流的语言环境
   cout << t1 << endl;

   getchar();
   return 0;
}

云型结果:

image-20210504141530791

  • 2
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Author: Josh Lospinoso Pub Date: 2019 ISBN: 978-1593278885 Pages: 792 Language: English Format: PDF Size: 10 Mb A fast-paced, thorough introduction to modern C++ written for experienced programmers. After reading C++ Crash Course, you’ll be proficient in the core language concepts, the C++ Standard Library, and the Boost Libraries. C++ is one of the most widely used languages for real-world software. In the hands of a knowledgeable programmer, C++ can produce small, efficient, and readable code that any programmer would be proud of. Designed for intermediate to advanced programmers, C++ Crash Course cuts through the weeds to get you straight to the core of C++17, the most modern revision of the ISO standard. Part 1 covers the core of the C++ language, where you’ll learn about everything from types and functions, to the object life cycle and expressions. Part 2 introduces you to the C++ Standard Library and Boost Libraries, where you’ll learn about all of the high-quality, fully-featured facilities available to you. You’ll cover special utility classes, data structures, and algorithms, and learn how to manipulate file systems and build high-performance programs that communicate over networks. You’ll learn all the major features of modern C++, including: Fundamental types, reference types, and user-defined types The object lifecycle including storage duration, memory management, exceptions, call stacks, and the RAII paradigm Compile-time polymorphism with templates and run-time polymorphism with virtual classes Advanced expressions, statements, and functions Smart pointers, data structures, dates and times, numerics, and probability/statistics facilities Containers, iterators, strings, and algorithms Streams and files, concurrency, networking, and application development With well over 500 code samples and nearly 100 exercises, C++ Crash Course is sure to help you build a strong C++ foundation.
THE BOOST C++ LIBRARIES是一份自己编译的chm格式文档,描述了如何使用boost,目录如下: Front page Chapter 1: Introduction 1.1 C++ and Boost 1.2 Development Process 1.3 Installation 1.4 Overview Chapter 2: Smart Pointers 2.1 General 2.2 RAII 2.3 Scoped Pointer 2.4 Scoped Array 2.5 Shared Pointer 2.6 Shared Array 2.7 Weak Pointer 2.8 Intrusive Pointer 2.9 Pointer Container 2.10 Exercises Chapter 3: Function Objects 3.1 General 3.2 Boost.Bind 3.3 Boost.Ref 3.4 Boost.Function 3.5 Boost.Lambda 3.6 Exercises Chapter 4: Event Handling 4.1 General 4.2 Signals 4.3 Connections 4.4 Exercises Chapter 5: String Handling 5.1 General 5.2 Locales 5.3 Boost.StringAlgorithms 5.4 Boost.Regex 5.5 Boost.Tokenizer 5.6 Boost.Format 5.7 Exercises Chapter 6: Multithreading 6.1 General 6.2 Thread Management 6.3 Synchronization 6.4 Thread Local Storage 6.5 Exercises Chapter 7: Asynchronous Input and Output 7.1 General 7.2 I/O Services and I/O Objects 7.3 Scalability and Multithreading 7.4 Network Programming 7.5 Developing Boost.Asio Extensions 7.6 Exercises Chapter 8: Interprocess Communication 8.1 General 8.2 Shared Memory 8.3 Managed Shared Memory 8.4 Synchronization 8.5 Exercises Chapter 9: Filesystem 9.1 General 9.2 Paths 9.3 Files and Directories 9.4 File Streams 9.5 Exercises Chapter 10: Date and Time 10.1 General 10.2 Calendar Dates 10.3 Location-independent Times 10.4 Location-dependent Times 10.5 Formatted Input and Output 10.6 Exercises Chapter 11: Serialization 11.1 General 11.2 Archive 11.3 Pointers and references 11.4 Serialization of class hierarchy objects 11.5 Wrapper functions for optimization 11.6 Exercises Chapter 12: Parser 12.1 General 12.2 Extended Backus-Naur Form 12.3 Grammar 12.4 Actions 12.5 Exercises Chapter 13: Containers 13.1 General 13.2 Boost.Array 13.3 Boost.Unordered 13.4 Boost.MultiIndex 13.5 Boost.Bimap 13.6 Exercises Chapter 14: Data Structures 14.1 General 14.2 Boost.Tuple 14.3 Boost.Any 14.4 Boost.Variant 14.5 Exercises Chapter 15: Error Handling 15.1 General 15.2 Boost.System 15.3 Boost.Exception Chapter 16: Cast Operators

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

超级D洋葱

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

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

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

打赏作者

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

抵扣说明:

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

余额充值