Simple Application of boost library-processing of time and date (3)

Source: Internet
Author: User
Tags time and date

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.