<Start>
The boost. datetime Library provides time-and date-related computing, formatting, conversion, input/output, and other functions to facilitate C ++ programming. However, it has the following features:
1. Boost. datetime only supports any Gregorian calendar date after January 1, 1400. If you need to calculate another date, you need to seek support from other libraries.
Date and time are common operations in programming. In the C standard library, <time. h> provides time and date functions of the time_t type and the TM structure type. Windows API also provides filetime-related functions. As boost is introduced here, we will not compare the advantages and disadvantages of these two types. If you are interested, you can go into it and believe that you will make your choice based on your own needs. Next, I will introduce the main functions of boost. datetime based on different situations. If you need strict time precision calculation, see boost. datetime stability and accuracy.
* Basic concepts:
Time point, such as eleven o'clock A.M.
Time period (such as 1 hour)-a period of time.
Time Window (time interval, time period)-a period of time, from a certain time point to another time point, such as two o'clock P.M. to 3.
Time System-a specific time representation and computing rule system.
Calendar system-a one-day-based time system. Gregorian calender is a standard calendar system.
UTC time (Coordinated Universal Time), supports the Standard Time System of Leap seconds. Similar GMT is used.
DST time (daylight savings time-Daylight Saving winter time) Many countries and regions adjust their local time in the summer.
Time zone-the region or country that uses the Time deviation rules between a specific DST rule and the standard.
EPOCH-start point of the date system and clock system. The start point of a time system is different. Generally, the UTC start point is on January 1, January 1, 1970.
Tai time-a high-precision Time System, which is too esoteric when using atomic power, has not been studied in detail.
Note: For more information about boost. datetime input and output operations, see boost. datetime Io operations.
Header file:
#include <boost/date_time/posix_time/posix_time.hpp>
Example 1. Date calculation: print the date of today; the date of this year's Thanksgiving Day (November Thursday of 4th); if it is not until this year's Thanksgiving Day, print the number of days from today to Thanksgiving Day.
using namespace boost::gregorian;
typedef nth_day_of_the_week_in_month nth_dow;
date today = day_clock::local_day(); //today
std::cout << "today is: " << today << std::endl;
nth_dow fourth_thur_in_nov(nth_dow::fourth,Thursday,Nov); // 4th thursday in Nov
date thanksgiving = fourth_thur_in_nov.get_date(today.year()); // get the date this year
std::cout << "Thanksgiving day this year is: " << thanksgiving << std::endl;
if(today < thanksgiving)
{
date_duration dd = thanksgiving - today; //date duration
std::cout << "has " << dd.days() << " days to thanksgiving."<< std::endl;
}
A. For date objects
> To obtain a date object, you can use four constructors of Date:
1: date d(2013,Jan,10);//constructor from grep y/m/d
2: date d1(d);//copy constructor
3: /*special date values*/
4: date d2(neg_infin);
5: date d3(pos_infin);
6: date d4(not_a_date_time);
7: date d5(max_date_time);
8: date d6(min_date_time);
9: date d7; //default constructor
> You can also use a string to obtain:
1: using namespace boost::gregorian;
2: date d(from_string("2002/1/25"));
3: date d1(from_string("2002-1-25"));
4: date d2(from_undelimited_string("20020125"));
5:
> You can also get the following information from the clock:
1: using namespace boost::gregorian;
2: Date D (day_clock: local_day (); // obtain the local date.
3: Date D1 (day_clock: universal_day (); // obtain the GMT date
4:
> To access the date object or make some date judgments, refer to the following code:
1: using namespace boost::gregorian;
2: date today = day_clock::local_day();
3: STD: cout <today. Year () <STD: Endl; // print year
4: STD: cout <today. Month () <STD: Endl; // print the month
5: STD: cout <today. Day () <STD: Endl; // print the day
6: STD: cout <today. week_number () <STD: Endl; // week of the year
7: STD: cout <today. day_of_week () <STD: Endl; // print the day of the week
8: STD: cout <today. day_of_year () <STD: Endl; // print the day of the year
9: STD: cout <today. end_of_month () <STD: Endl; // print the last day of the month. For the month of the month, May 1, February.
10: std::cout << today.modjulian_day() << std::endl;
11: std::cout << today.julian_day() << std::endl;
12:
13: if(today.is_infinity())
14: // today.is_neg_infinity();today.is_pos_infinity();today.is_not_a_date();
15: // today.is_special();
16: // today.as_special();
17: {
18: std::cout << "True" << std::endl;
19: }
20: else
21: {
22: std::cout << "False" << std::endl;
23: }
24:
> We often need to convert the time to the string type. See:
1: using namespace boost::gregorian;
2: date today = day_clock::local_day();
3: std::string str1(to_simple_string(today));//YYYY-MMM-DD, 2013-Jan-11
4: std::string str2(to_iso_string(today));//YYYYMMDD, 20130111
5: std::string str3(to_iso_extended_string(today));//YYYY-MM-DD, 2013-01-11
6: std::cout << str1 << std::endl << str2 << std::endl << str3 << std::endl;
> The date object can also be converted to TM:
1: using namespace boost::gregorian;
2: date today = day_clock::local_day();
3: tm d_tm = to_tm(today); // tm struct, year start from 1900, and Jan is 0, Sunday is 0
4: std::cout << d_tm.tm_year << std::endl << d_tm.tm_mon << std::endl << d_tm.tm_mday << std::endl;
5: std::cout << d_tm.tm_wday << std::endl;
B. For date_duration objects
Date_duration can be constructed in multiple ways. The operator +/-can calculate date, date_duration, and so on. The details are as follows.
1: using namespace boost::gregorian;
2: date leap_year_date(2004,Jan,31);
3: date norm_year_date(2005,Jan,31);
4: date_duration dd(3);
5: days dd1(10);
6: weeks ww(2);
7: months mm(1);
8: years yy(2);
9:
10: std::cout << (leap_year_date + dd) << std::endl;
11: std::cout << (leap_year_date + dd1) << std::endl;
12: std::cout << (leap_year_date + ww) << std::endl;
13: std::cout << (leap_year_date + mm) << std::endl;
14: std::cout << (leap_year_date + yy) << std::endl;
15:
16: std::cout << (norm_year_date + dd) << std::endl;
17: std::cout << (norm_year_date + dd1) << std::endl;
18: std::cout << (norm_year_date + ww) << std::endl;
19: std::cout << (norm_year_date + mm) << std::endl;
20: std::cout << (norm_year_date + yy) << std::endl;
21:
In addition, the iterator day_iterator, month_iterator, and year_iterator can also be regarded as a fixed-length date_duration tool.
C. For the date_period object
Date_peroid is used as a date window to determine whether a date falls between the window.
Date_period can be created by adding two dates or one date plus one day. To change a date_period, you can use Shift translation or expand widening to merge. You can also use a minimum period containing the two period periods. The method is span. But when using it, you must pay attention to the constraints. For example, when merge is used, the two period periods must be intersection. Otherwise, how can we combine them?
1: using namespace boost::gregorian;
2: date d_begin(2013,Jan,22);
3: date d_end(2013,Apr,1);
4: date_period dp(d_begin,d_end);//constructor
5: date_period dp1(d_begin,days(20));//constructor 2
6:
7: std::cout << dp << std::endl;
8: dp.shift(days(10));
9: std::cout << dp << std::endl;
10: dp.expand(days(10));
11: std::cout << dp << std::endl;
Here, we have an introduction to the basic boost date methods. However, we may find that the problem has not been elaborated on the 4th Thursday of June. In fact, there are multiple methods in boost: date_time to get the answer. In this example, nth_day_of_the_week_in_month is used. You may try to use next_weekday to find November 1 Thursday and add 3 more weeks from January 1, 1st.
The processing of time is very similar to the date. I will also introduce it in the subsequent article, and paste it here.
<CONCLUSION>
[Boost] boost time and date Processing-(1) date operations