Boost-date_time库学习

  最近开了boost库的学习,就先从日期-时间库开始吧,boost的date_time库是一个很强大的时间库,用起来还是挺方便的。

  以下代码只是入门级的简单学习,更详细的资料参考boost源码。

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
 
/************************************************************************/
/* C++ Library                                                        */
/************************************************************************/
#include <iostream>
#include <vector>

/************************************************************************/
/* timer and date_time                                                  */
/************************************************************************/
#include "boost/timer.hpp"
#include "boost/progress.hpp"
#include "boost/date_time/gregorian/gregorian.hpp"
#include "boost/date_time/posix_time/posix_time.hpp"

using namespace boost;
using namespace std;

int main(void)
{
    
/************************************************************************/
    
/* timer库                                                              */
    
/************************************************************************/
    
// 单位:s
    timer tr;
    cout << 
"Max timespan: " << tr.elapsed_max() << endl;
    cout << 
"Min timespan: " << tr.elapsed_min() << endl;
    cout << 
"Now time elapsed: " << tr.elapsed() << endl;

    {
        cout << 
"progress_timer start" << endl;
        progress_timer p_tr;
        
//cout << "Now progress_time elapsed: " << p_tr.elapsed() << endl;
        cout << "progress_timer end" << endl;
    }

    vector<
int> v;
    
for (int i = 0; i < 100; i++)
    {
        v.push_back(i);
    }
    progress_display p_ds(v.size());
    vector<
int>::iterator pos;
    
for(pos = v.begin(); pos != v.end(); pos++)
    {
        
// do something
        ++p_ds;
    }

    
/************************************************************************/
    
/* date_time库                                                          */
    
/************************************************************************/

    
// date
    using namespace boost::gregorian;
    
// date可通过多种方式来构造,支持加减运算
    date dt1;                       //一个无效的日期
    date dt2(201761);
    date dt3(
2017, Jan, 1);
    date dt4(dt2);
    assert(dt1 == date(not_a_date_time));
    assert(dt4 == dt2);
    assert(dt3 < dt2);
    date dt = from_string(
"2017-03-15");
    date dtt = from_undelimited_string(
"20170523");
    date::ymd_type ymd = dtt.year_month_day();
    assert(ymd.year == 
2017);
    assert(ymd.month == 
5);
    assert(ymd.day = 
23);
    cout << dt2.year() << 
"-"
         << dt2.month() << 
"-"
         << dt2.day() << endl;
    cout << dt.year() << 
"-"
         << dt.month() << 
"-"
         << dt.day() << endl;
    cout << dtt.year() << 
"-"
         << dtt.month() << 
"-"
         << dtt.day() << endl;
    cout << dtt << endl;
    cout << to_simple_string(dtt) << endl;
    cout << to_iso_string(dtt) << endl;
    cout << to_iso_extended_string(dtt) << endl;
    cout << dtt.day_of_week() << endl;


    
// 可以使用special_values来创建一些特殊的日期
    date d1(pos_infin);
    date d2(neg_infin);
    date d3(not_a_date_time);
    date d4(max_date_time);
    date d5(min_date_time);

    
// 如果创建的日期对象使用了非法值,如超出了范围或使用了不正确的日期,就会抛出异常
    try
    {
        date eDt(
1399121);
    }
    
catch (...)
    {
        
//...
    }

    
// day_clock
    // day_clock是一个天级别的时钟,内部使用了C标准库的localtime()和gmtime()函数;
    // 调用静态成员函数local_day()和universal_day()可当天的本地日期和UTC日期对象.
    cout << day_clock::local_day() << endl;
    cout << day_clock::universal_day() << endl;

    
// date 与 tm的转换
    date date1(2017101);
    tm t = to_tm(date1);
    date date2 = date_from_tm(t);
    assert(date2 == date1);

    
// 日期长度date_duration   以天为单位的时长,有别名days
    days dd(10);
    
// 同时提供了另外三个时长类:years  months weeks
    years  y(1);
    assert(y.number_of_years() == 
1);
    months m(
2);
    assert(m.number_of_months() == 
2);
    weeks  w(
3);
    assert(w.days() == 
21);

    
// 日期区间 date_period
    date_period dp1(date(201723), days(20));
    date_period dp2(date(
201751), days(10));
    cout << dp1 << endl;
    cout << dp2 << endl;
    assert(dp1 < dp2);

    
// time
    using namespace boost::posix_time;
    
// 时间长度 time_duration
    time_duration td(110201000);              // hour,min,sec,fs
    cout << td << endl;

    
// 时间点 ptime
    using namespace boost::gregorian;
    ptime pt(date(
2017610), hours(3));          // 2017年6月10日凌晨3时
    ptime pt1 = time_from_string("2017-5-1 10:00:00");
    ptime pt2 = from_iso_string(
"20170501T080000");
    ptime pt3 = second_clock::local_time();
    ptime pt4 = microsec_clock::universal_time();
    cout << pt1 << endl;
    cout << pt2 << endl;
    cout << pt3 << endl;
    cout << pt4 << endl;

    cout << to_simple_string(pt) << endl;
    cout << to_iso_string(pt) << endl;
    cout << to_iso_extended_string(pt) << endl;

    
// 与tm time_t等结构的转换
    tm tt = to_tm(pt);

    
// 时间时区 time_period
    time_period tp(pt, hours(8));                   // 一个8小时的时区

    
// 除此之外,还有日期、时间迭代器
    //date_iterator time_iterator

    cout << (gregorian_calendar::is_leap_year(
2000) ?  "Yes" : "no") << endl;       // 闰年
    assert(gregorian_calendar::end_of_month_day(20132) == 28);                    // 月末天

    
// 格式化时间
    date ddtt(201769);
    date_facet *pfacet = 
new date_facet("%Y年%m月%d日");
    cout.imbue(locale(cout.getloc(), pfacet));
    cout << ddtt << endl;

    cin.get();
    
return 0;
}

原文地址:https://www.cnblogs.com/MakeView660/p/6971670.html