Today, we will start to boost the date processing part. There are too many dates to be carefully considered. When we encounter a date processing problem, it is obviously extremely time-consuming to write a processing class by ourselves. After learning about today's content, I believe we will be more comfortable with date processing. Today we are learning about date, the core class of boost date processing. Similarly, let's start with the code:
#include <iostream>#include <boost/date_time/gregorian/gregorian.hpp>int main(){ boost::gregorian::date d1(2014, 9, 26); std::cout << d1 << std::endl; std::cout << d1.year() << std::endl; std::cout << d1.month() << std::endl; std::cout << d1.day() << std::endl; boost::gregorian::date::ymd_type ymd = d1.year_month_day(); std::cout << ymd.year << std::endl; std::cout << ymd.month << std::endl; std::cout << ymd.day << std::endl; return 0;}
The date class is a flexible class. initialization can be passed in a format similar to (2014, 9, 26) or (2014, SEP, 26, of course, it can also be initialized with similar objects. You can use the year (), month (), Day () methods to obtain dates, or declare the ymd_type struct supported by date. If the date class is useful, it is estimated that most people will say: "It is better to write one by yourself for such a simple function ."
Of course, date has more functions than that. Next we will use a piece of code to understand its other functions:
#include <iostream>#include <boost/date_time/gregorian/gregorian.hpp>int main(){ boost::gregorian::date d1(2014, 9, 26); std::cout << d1.day_of_week() << std::endl; std::cout << d1.day_of_year() << std::endl; std::cout << d1.end_of_month() << std::endl; std::cout << boost::gregorian::date(2014, 9, 26).week_number() << std::endl; return 0;}
The date class provides the Member method to obtain the day of the week of the initialization, the day of the year and the number of the last day of the month. From the function names in the code above, we can understand who they are. In addition, date can determine the week of the year when the initialization date is used. Note: In boost-1.56.0, day_of_week returns the day of the week in English, but in some old versions, digits are returned. 0 indicates Sunday.
Simple Application of boost library-processing of time and date (3)