ch2
timer
只有如下三个成员函数,单位是秒
#include <boost/timer.hpp>
#include <iostream>
using namespace boost;
using namespace std;
int main()
{
timer t;
cout << "MAX TIME VALUE " << t.elapsed_max()/3600 << "h" << endl;
cout << "min TIME VALUE " << t.elapsed_min() << "s" << endl;
cout << "now time elaped" << t.elapsed() << "s" << endl;
}
max timespan second 2.14748e+06
min_timespan second 0.001
now time elapsed 0.067s
progress_timer, timer的另一层包装,析构函数时会自动输出progress_timer.elapsed()
#include <boost/progress.hpp>
#include <iostream>
using namespace boost;
int main()
{
boost::progress_timer t;
std::cout << t.elapsed() << std::endl;
}
#include <boost/progress.hpp>
#include <iostream>
using namespace boost;
int main()
{
{
boost::progress_timer t;
std::cout << t.elapsed() << std::endl;
}
{
boost::progress_timer t;
}
}
#include <boost/progress.hpp>
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
using namespace boost;
using namespace std;
int main()
{
vector<string> v(100);
ofstream fs("./test.txt");
progress_display pd(v.size());
for(auto &x : v)
{
fs << x << std::endl;
++pd;
}
}
#include <boost/timer.hpp>
#include <boost/date_time/gregorian/gregorian.hpp>
#include <iostream>
using namespace std;
using namespace boost::gregorian;
int main()
{
date d(2018, 7, 5);
cout << d.year() << endl;
cout << d.month() << endl;
cout << d.day() << endl;
cout << d.day_of_week() << endl;
cout << d.day_of_year() << endl;
cout << d.day_count() << endl;
cout << d.day_number() << endl;
cout << d.end_of_month() << endl;
cout << d.is_infinity() << endl;
cout << d.week_number() << endl;
cout << d.year_month_day().year << " " << d.year_month_day().month << endl;
system("pause");
return 0;
}
2018
Jul
5
Thu
186
2458305
2458305
2018-Jul-31
0
27
2018 Jul
#include <boost/date_time/gregorian/gregorian.hpp>
#include <iostream>
#include <time.h>
using namespace std;
int main()
{
using namespace boost::gregorian;
date d1;
date d2(2017,1,18);
date d3(2018, Jan, 1);
//date d4(6666,sabi,50) 不可行
cout << d1 << endl;
cout << d2 << endl;
cout << d3 << endl;
date d4(d2);
assert(d1 == date(not_a_date_time));
assert(d2 == d4);
assert(d3 > d4);
date dd1 = from_string("2018.3.1");
date dd2 = from_string("2018/3/1");
date dd3 = from_undelimited_string("20180118");
//天级别的时钟
cout << day_clock::local_day() << endl;
cout << day_clock::universal_day() << endl;
//特殊时间概念枚举变量special_value
date ds1(neg_infin);
date ds2(pos_infin);
date ds3(not_a_date_time);
date ds4(max_date_time);
date ds5(min_date_time);
cout << ds1 << endl;
//访问date类的日期
cout << d2.year() << d2.month() << d2.day() << endl;
cout << d2.day_count() << d2.day_number() << endl;
date::ymd_type ymd = d2.year_month_day();
cout << ymd.day << ymd.month << ymd.year << endl;
cout << d2.day_of_week() << endl;
cout << d2.day_of_year() << endl;
cout << d2.end_of_month() << endl;
// 转化成字符串输出
cout << to_simple_string(d2) << endl;
cout << to_iso_string(d2) << endl;
//c tm结构体与date 转换
tm t = to_tm(d2);
assert(t.tm_hour == 0);
assert(t.tm_min == 0);
//assert(t.tm_year == 2018);
cout << "tm since 1990" << t.tm_year << endl;
cout << "tm sin Jan 0-11" << t.tm_mon << endl;
cout << "tm 1-31" << t.tm_mday << endl;
//日期长度,基本类date_duration,其有方法days()返回时间单位(以天为单位),类型别名为days,派生出 weeks months years
days da1(10), da2(-100), da3(255);
assert(da1 > da2 && da1 < da3);
assert(da1 + da2 == days(-90));
weeks w(3);
assert(w.days() == 211);
months m(5);
years y(2);
months m2 = m + y;
assert(m2.number_of_months() == 29);
assert((y * 2).number_of_years() == 4);
return 0;
}
运行结果
C:\Users\咖啡>"C:\Users\咖啡\Documents\Visual Studio 2015\Projects\helloboost\Debug\helloboost.exe"
not-a-date-time
2017-Jan-18
2018-Jan-01
2018-Jan-18
2018-Jan-18
-infinity
2017Jan18
24577722457772
18Jan2017
Wed
18
2017-Jan-31
2017-Jan-18
20170118
tm117
tm0
tm18
cout << day_clock::local_day() << endl;
cout << day_clock::universal_day() << endl;
2018-Jul-05
#include <boost/timer.hpp>
#include <boost/date_time/gregorian/gregorian.hpp>
#include <iostream>
using namespace std;
using namespace boost::gregorian;
int main()
{
date d(2018, 7, 5);
date dd = from_string("2018.7.6");
//typedef date_duration days
days da(10);
days db(12);
cout << da + db << endl;
cout << da.days() << endl;
assert(da.days() != db.days());
weeks w(3);
cout << w.days() << endl;
assert(w.days() == 21);
cout << w.unit() << endl;
months m(1);
cout << m.number_of_months() << endl;
m += years(1);
cout << m.number_of_months() << endl;
system("pause");
return 0;
}
22
10
21
1
1
13