目录: 1,日期 构造date 继续构造date对象 date特别的值 date能访问的函数 boost date_time 与tm转换 日期的加减运算 计算时间段 日期的迭代器 日期生成器 4月的第一个/最后一个星期一 日期生成器: 某月的第几个星期几,某天的前一个/后一个星期一 日期生成器算法 日历类 gregorian_calendar 2,时间 posix 时间的构造 时间的操作 时间段 操作 时间迭代器
1,日期
构造date
[email protected]:~/boost$ cat main.cpp #include <iostream> #include <boost/date_time/gregorian/gregorian.hpp> int main() { using namespace std; using namespace boost::gregorian; //date d(2010,4,8); //也是可以的 date d(2010,Apr,8); cout << d << endl; return 0; } [email protected]:~/boost$ g++ main.cpp && ./a.out 2010-Apr-08 [email protected]:~/boost$
继续构造date对象
[email protected]:~/boost$ cat main.cpp #include <iostream> #include <boost/date_time/gregorian/gregorian.hpp> int main() { using namespace std; using namespace boost::gregorian; date d1 = from_string("2010-12-21"); cout << d1 << endl; date d2 = from_undelimited_string("20101221"); cout << d2 << endl; date d3 = day_clock::local_day(); cout << d3 << endl; date d4 = day_clock::universal_day(); cout << d4 << endl; return 0; } [email protected]:~/boost$ g++ main.cpp -l boost_date_time && ./a.out 2010-Dec-21 2010-Dec-21 2016-Dec-21 2016-Dec-21 [email protected]:~/boost$
date特别的值
[email protected]:~/boost$ cat main.cpp #include <iostream> #include <boost/date_time/gregorian/gregorian.hpp> int main() { using namespace std; using namespace boost::gregorian; date d1(neg_infin); cout << d1 << endl;//创建负无限日期 date d2(pos_infin); cout << d2 << endl;//创建正无限日期 date d3(not_a_date_time); cout << d3 << endl;//创建不是任何日期 date d4(min_date_time); cout << d4 << endl;//创建日期系统中最小的日期 date d5(max_date_time); cout << d5 << endl;//创建日期系统中最大的日期 return 0; } [email protected]:~/boost$ g++ main.cpp && ./a.out -infinity +infinity not-a-date-time 1400-Jan-01 9999-Dec-31 [email protected]:~/boost$
date能访问的函数
[email protected]:~/boost$ cat main.cpp #include <iostream> #include <boost/date_time/gregorian/gregorian.hpp> int main() { using namespace std; using namespace boost::gregorian; date today = day_clock::local_day(); cout << today << endl;//今天是个好日子 cout << today.day_of_week() << endl;//今天是周几 cout << today.end_of_month() << endl;//月末是哪一天 cout << today.day_of_year() << endl;//今天是本年的第多少天 cout << today.year() << endl;//今天是哪一年 cout << today.week_number() << endl;//今天是本年的第几周 return 0; } [email protected]:~/boost$ g++ main.cpp -l boost_date_time && ./a.out 2016-Dec-21 Wed 2016-Dec-31 356 2016 51 [email protected]:~/boost$
boost date_time 与tm转换
[email protected]:~/boost$ cat main.cpp #include <iostream> #include <boost/date_time/gregorian/gregorian.hpp> int main() { using namespace std; using namespace boost::gregorian; date today = day_clock::local_day(); tm time = to_tm(today);//转换为C风格 cout <<time.tm_year + 1900 << endl; cout <<time.tm_mon + 1 << endl;//tm 默认从0开始 return 0; } [email protected]:~/boost$ g++ main.cpp -l boost_date_time && ./a.out 2016 12 [email protected]:~/boost$
日期的加减运算
[email protected]:~/boost$ cat main.cpp #include <iostream> #include <boost/date_time/gregorian/gregorian.hpp> int main() { using namespace std; using namespace boost::gregorian; date today = day_clock::local_day();//今天时间 date date1(2008,8,8); date_duration diff = today - date1; cout <<"北京奥运会已经过去"<<diff<<"天" <<endl; return 0; } [email protected]:~/boost$ g++ main.cpp -l boost_date_time && ./a.out 北京奥运会已经过去3057天 [email protected]:~/boost$
计算时间段
[email protected]:~/boost$ cat main.cpp /* This example demonstrates a simple use of periods for the calculation of date information. The example calculates if a given date is a weekend or holiday given an exclusion set. That is, each weekend or holiday is entered into the set as a time interval. Then if a given date is contained within any of the intervals it is considered to be within the exclusion set and hence is a offtime. Output: Number Excluded Periods: 5 20020202/20020203 20020209/20020210 20020212/20020212 20020216/20020217 In Exclusion Period: 20020216 --> 20020216/20020217 20020223/20020224 */ #include "boost/date_time/gregorian/gregorian.hpp" #include <set> #include <algorithm> #include <iostream> typedef std::set<boost::gregorian::date_period> date_period_set; //Simple population of the exclusion set date_period_set generateExclusion() { using namespace boost::gregorian; date_period periods_array[] = { date_period(date(2002,Feb,2), date(2002,Feb,4)),//weekend of 2nd-3rd date_period(date(2002,Feb,9), date(2002,Feb,11)), date_period(date(2002,Feb,16), date(2002,Feb,18)), date_period(date(2002,Feb,23), date(2002,Feb,25)), date_period(date(2002,Feb,12), date(2002,Feb,13))//a random holiday 2-12 }; const int num_periods = sizeof(periods_array)/sizeof(date_period); date_period_set ps; //insert the periods in the set std::insert_iterator<date_period_set> itr(ps, ps.begin()); std::copy(periods_array, periods_array+num_periods, itr ); return ps; } int main() { using namespace boost::gregorian; date_period_set ps = generateExclusion(); std::cout << "Number Excluded Periods: " << ps.size() << std::endl; date d(2002,Feb,16); date_period_set::const_iterator i = ps.begin(); //print the periods, check for containment for (;i != ps.end(); i++) { std::cout << to_iso_string(*i) << std::endl; //if date is in exclusion period then print it if (i->contains(d)) { std::cout << "In Exclusion Period: " << to_iso_string(d) << " --> " << to_iso_string(*i) << std::endl; } } return 0; } [email protected]:~/boost$ g++ main.cpp -l boost_date_time && ./a.out Number Excluded Periods: 5 20020202/20020203 20020209/20020210 20020212/20020212 20020216/20020217 In Exclusion Period: 20020216 --> 20020216/20020217 20020223/20020224 [email protected]:~/boost$
日期的迭代器
[email protected]:~/boost$ cat main.cpp #include <stdlib.h> #include <iostream> #include "boost/date_time/gregorian/gregorian.hpp" int main(int argc,char** argv) { using namespace std; using namespace boost::gregorian; int year = atoi(argv[1]); int month = atoi(argv[2]); date firstDay(year,month,1); cout << "日 一 二 三 四 五 六"<<endl; date lastDay = firstDay.end_of_month(); int empty = firstDay.day_of_week(); for(int i =0;i<empty;i++) { cout <<setw(3)<<setfill(‘ ‘)<<‘ ‘ ; } day_iterator it(firstDay,1); for(;*it <= lastDay;++it) { cout<<setw(2)<<it->day(); cout << (6 == it->day_of_week()?‘\n‘:‘ ‘); } } [email protected]:~/boost$ g++ main.cpp -l boost_date_time && ./a.out 2016 12 日 一 二 三 四 五 六 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 [email protected]:~/boost$ Linux cal命令来验证 [email protected]:~/boost$ cal 12 2016 十二月 2016 日 一 二 三 四 五 六 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 [email protected]:~/boost$
日期生成器 4月的第一个/最后一个星期一
[email protected]:~/boost$ cat main.cpp #include <stdlib.h> #include <iostream> #include <boost/date_time/gregorian/gregorian.hpp> int main(int argc,char** argv) { using namespace std; using namespace boost::gregorian; first_day_of_the_week_in_month fdm(Monday,Apr);//4月的第一个星期一 date d2 = fdm.get_date(2016);//2016年4月的第一个星期一 cout << d2 << endl; last_day_of_the_week_in_month lwdm(Monday,Apr);//4月的最后一个星期一 date d1 = lwdm.get_date(2016);//2016年4月的最后一个星期一 cout << d1 << endl; } [email protected]:~/boost$ g++ main.cpp && ./a.out 2016-Apr-04 2016-Apr-25 验证: [email protected]:~/boost$ cal 4 2016 四月 2016 日 一 二 三 四 五 六 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 [email protected]:~/boost$
日期生成器: 某月的第几个星期几,某天的前一个/后一个星期一
[email protected]:~/boost$ cat main.cpp #include <stdlib.h> #include <iostream> #include <boost/date_time/gregorian/gregorian.hpp> int main(int argc,char** argv) { using namespace std; using namespace boost::gregorian; //2010年4月的第3个星期一 typedef nth_day_of_the_week_in_month nth_dow; nth_dow ndm(nth_dow::third,Monday,Apr); date d1 =ndm.get_date(2010); cout << d1 << endl; //指定月,日,年 partial_date pd(1, Oct); date d2 = pd.get_date(2010); //2010-Oct-01 cout << d2 << endl; //2010年4月1日之后的第一个星期一是哪天? first_day_of_the_week_after fdaf(Sunday); date d3 = fdaf.get_date(date(2010, Apr, 1)); //2010-Apr-04 cout << d3 << endl; //2010年4月1日之前的第一个星期一是那天? first_day_of_the_week_before fdbf(Monday); date d4 = fdbf.get_date(date(2010, Apr, 1)); //2010-Mar-29 cout << d4 << endl; return 0; } [email protected]:~/boost$ g++ main.cpp && ./a.out 2010-Apr-19 2010-Oct-01 2010-Apr-04 2010-Mar-29 [email protected]:~/boost$
日期生成器算法
[email protected]:~/boost$ cat main.cpp #include <stdlib.h> #include <iostream> #include <boost/date_time/gregorian/gregorian.hpp> int main(int argc,char** argv) { using namespace std; using namespace boost::gregorian; //从2010-4-1到星期五之间有几天? date d7(2010, Apr, 1); // 这天是星期四 greg_weekday gw1(Friday); days ds1 = days_until_weekday(d7, gw1);//开始计算 cout << ds1 << endl; // 计算从给定日期到前一个给定周日的天数。 date d8(2010, Apr, 1); // 星期四 greg_weekday gw2(Friday); days ds2 = days_before_weekday(d8, gw2); // 4天 cout << ds2 << endl; return 0; } [email protected]:~/boost$ g++ main.cpp && ./a.out 1 6 [email protected]:~/boost$
日历类 gregorian_calendar
2,时间
posix 时间的构造
[email protected]:~/boost$ cat main.cpp #include <iostream> #include <boost/date_time/posix_time/posix_time.hpp> #include <boost/date_time/gregorian/gregorian.hpp> int main() { using namespace std; using namespace boost::posix_time; using namespace boost::gregorian; //创建时间 2002-Jan-10 01:02:03 ptime t1(date(2002, Jan, 10), time_duration(1, 2, 3)); cout << t1 << endl; //创建时间 2002-Jan-10 01:00:00.000005 ptime t2(date(2002, Jan, 10), hours(1) + boost::posix_time::microseconds(5)); cout << t2 << endl; // 特殊时间 ptime d1(neg_infin); ptime d2(pos_infin); ptime d3(not_a_date_time); ptime d4(max_date_time); ptime d5(min_date_time); // 从字符串构造 std::string ts("2016-12-22 23:59:59.050"); ptime t3(time_from_string(ts)); cout << t3 << endl; // 从不带分隔的字符串构造。 std::string ts2("20080131T235959"); ptime t4(from_iso_string(ts2)); cout << t4 << endl; //时间 字符串相互转换 cout << to_simple_string(t3) << endl; cout << to_iso_string(t3) << endl; cout << to_iso_extended_string(t3) << endl; } [email protected]:~/boost$ g++ main.cpp -l boost_date_time && ./a.out 2002-Jan-10 01:02:03 2002-Jan-10 01:00:00.000005 2016-Dec-22 23:59:59.050000 2008-Jan-31 23:59:59 2016-Dec-22 23:59:59.050000 20161222T235959.050000 2016-12-22T23:59:59.050000 [email protected]:~/boost$
时间的操作
[email protected]:~/boost$ cat main.cpp #include <iostream> #include <boost/date_time/posix_time/posix_time.hpp> #include <boost/date_time/gregorian/gregorian.hpp> int main() { using namespace std; using namespace boost::posix_time; using namespace boost::gregorian; //时分秒 毫秒的设定 time_duration time1 = hours(12); cout << time1 << endl; time_duration time2 = minutes(13); cout << time2 << endl; time_duration time3 = seconds(14); cout << time3 << endl; time_duration time4 = microseconds(15); cout << time4 << endl; //时间相加 time_duration time5 = time1+time2+time3+time4; cout << time5 << endl; time_duration time6 = hours(12)+minutes(13)+seconds(14)+microseconds(15); cout << time6 << endl; return 0; } [email protected]:~/boost$ g++ main.cpp -l boost_date_time && ./a.out 12:00:00 00:13:00 00:00:14 00:00:00.000015 12:13:14.000015 12:13:14.000015 [email protected]:~/boost$
时间段 操作
[email protected]:~/boost$ cat main.cpp #include <iostream> #include <boost/date_time/posix_time/posix_time.hpp> #include <boost/date_time/gregorian/gregorian.hpp> int main() { using namespace std; using namespace boost::posix_time; using namespace boost::gregorian; //10毫秒的时间段 date d1 = day_clock::local_day(); time_period tp1(ptime(d1,hours(1)),ptime(d1,hours(1)+milliseconds(10))); cout << tp1 << endl; //10秒的时间段 time_period tp2(second_clock::local_time(),seconds(10)); cout << tp2 << endl; //将整个时间段倒退1小时 tp2.shift(hours(1)); cout << tp2 << endl; //将整个时间段前进1小时 tp2.expand(hours(1)); cout << tp2 << endl; cout << to_simple_string(tp1)<< " to_simple_string"<< endl; return 0; } [email protected]:~/boost$ g++ main.cpp -l boost_date_time && ./a.out [2016-Dec-22 01:00:00/2016-Dec-22 01:00:00.009999] [2016-Dec-22 12:53:12/2016-Dec-22 12:53:21.999999] [2016-Dec-22 13:53:12/2016-Dec-22 13:53:21.999999] [2016-Dec-22 12:53:12/2016-Dec-22 14:53:21.999999] [2016-Dec-22 01:00:00/2016-Dec-22 01:00:00.009999] to_simple_string [email protected]:~/boost$
时间迭代器
[email protected]:~/boost$ cat main.cpp #include <iostream> #include <boost/date_time/posix_time/posix_time.hpp> #include <boost/date_time/gregorian/gregorian.hpp> int main() { using namespace std; using namespace boost::posix_time; using namespace boost::gregorian; date d(2016,Dec,22); ptime start(d); ptime end = start + hours(2);//小时后结束 time_iterator titr(start,minutes(15));//每次递增15分钟 //向后 while(titr < end) { cout << to_simple_string(*titr) << endl; ++titr; } //向前 cout << endl; while(titr >start) { cout << to_simple_string(*titr) << endl; --titr; } return 0; } [email protected]:~/boost$ g++ main.cpp -l boost_date_time && ./a.out 2016-Dec-22 00:00:00 2016-Dec-22 00:15:00 2016-Dec-22 00:30:00 2016-Dec-22 00:45:00 2016-Dec-22 01:00:00 2016-Dec-22 01:15:00 2016-Dec-22 01:30:00 2016-Dec-22 01:45:00 2016-Dec-22 02:00:00 2016-Dec-22 01:45:00 2016-Dec-22 01:30:00 2016-Dec-22 01:15:00 2016-Dec-22 01:00:00 2016-Dec-22 00:45:00 2016-Dec-22 00:30:00 2016-Dec-22 00:15:00 [email protected]:~/boost$
时间: 2024-10-25 02:13:03