zoukankan      html  css  js  c++  java
  • boost datetime

    To create a date, use the class boost::gregorian::date

    1. date

    #include <boost/date_time/gregorian/gregorian.hpp>
    #include <iostream>
    
    int main()
    {
      boost::gregorian::date d(2014, 1, 31);
      std::cout << d.year() << std::endl;
      std::cout << d.month() << std::endl;
      std::cout << d.day() << std::endl;
      std::cout << d.day_of_week() << std::endl;
      std::cout << d.end_of_month() << std::endl;

      date d = day_clock::universal_day();
      std::cout << d.year() << std::endl;
      std::cout << d.month() << std::endl;
      std::cout << d.day() << std::endl';
    
      d = date_from_iso_string("20140131");
      std::cout << d.year() << std::endl;
      std::cout << d.month() << std::endl;
      std::cout << d.day() << std::endl;
      return 0;
    }

    boost::gregorian::date provides several constructors to create dates. The most basic constructor takes a year, a month, and a day as parameters.

    boost::gregorian::date returns the current date. The member function universal_day() returns a UTC date, which is independent of time zones and daylight savings. boost::gregorian::day_clock also provides a member function called local_day(), which takes local settings into account. To retrieve the current date within the local time zone, use local_day().

    date_from_iso_string converts a date in the ISO 8601 format. Other functions include: boost::gregorian::from_simple_string() and boost::gregorian::from_us_string().

    2. date_duration

    #include <boost/date_time/gregorian/gregorian.hpp>
    #include <iostream>
    
    using namespace boost::gregorian;
    
    int main()
    {
      date d1(2014, 1, 31);
      date d2(2014, 2, 28);
      date_duration dd = d2 - d1;
      std::cout << dd.days() << std::endl;
    
    date_duration dd{4};
    std::cout << dd.days() << std::endl;
    weeks ws{4};
    std::cout << ws.days() << std::endl;
    months ms{4};
    std::cout << ms.number_of_months() << std::endl;
    years ys{4};
    std::cout << ys.number_of_years() << std::endl;
    return 0; }

    boost::gregorian::date overloads operator-, two points in time can be subtracted. The return value is of type boost::gregorian::date_duration and marks the duration between the two dates. days() returns the number of days in the duration specified.

    Objects of type boost::gregorian::date_duration can also be created by passing the number of days as a single parameter to the constructor. To create a duration that involves weeks, months, or years, use boost::gregorian::weeks, boost::gregorian::months, or boost::gregorian::years

    3. date_period

    #include <boost/date_time/gregorian/gregorian.hpp>
    #include <iostream>
    
    using namespace boost::gregorian;
    
    int main()
    {
      date d1(2014, 1, 1);
      date d2(2014, 2, 28);
      date_period dp{d1, d2};
      date_duration dd = dp.length();
      std::cout << dd.days() << std::endl;
    
      std::cout.setf(std::ios::boolalpha);
      std::cout << dp.contains(d1) << std::endl;
      std::cout << dp.contains(d2) << std::endl;
      return 0;
    }

    The constructor of boost::gregorian::date_period can accept two kinds of input. You can pass two parameters of type boost::gergorian::date, one for the beginning date and one for the end date. Please note that the day before the end date is actually that last day of the period.

    dp contains d1 while dose not contain d2.

    4. iterator

    #include <boost/date_time/gregorian/gregorian.hpp>
    #include <iostream>
    
    using namespace boost;
    
    int main()
    {
      gregorian::date d(2014, 5, 12);
      gregorian::day_iterator it{d};
      std::cout << *++it << std::endl;
      std::cout << date_time::next_weekday(*it, gregorian::greg_weekday(date_time::Friday)) << std::endl;
    return 0; }

    Use the iterator boost::gregorian::day_iterator to jump forward or backward by a day from a specific date. Use boost::gregorian::week_iterator, boost::gregorian::month_iterator, and boost::gregorian::year_iterator to jump by weeks, months, or years, respectively.

  • 相关阅读:
    Bzoj 2134: [国家集训队2011]单选错位(期望)
    洛谷 P1230 智力大冲浪
    洛谷 P2695 骑士的工作
    洛谷 P1551 亲戚
    洛谷 P1111 修复公路
    洛谷 P1599 结算日
    HDU 1166 敌兵布阵
    洛谷 P2212 [USACO14MAR]浇地Watering the Fields
    洛谷 P1506 拯救oibh总部
    洛谷 P1396 营救
  • 原文地址:https://www.cnblogs.com/sssblog/p/11302360.html
Copyright © 2011-2022 走看看