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.

  • 相关阅读:
    Android常用命令
    kafka原理和集群配置
    zookeeper原理和集群配置
    python中is和==的区别以及全字段取出key和value
    Android Monkey压力测试介绍
    有一串随机整数列,a1,a2,...an,求数字[0-9]分别出现的次数,比如:[12, 210, 33]输出{'0': 1, '1': 2, '2': 2, '3': 2},时间和空间复杂度
    接口自动化get请求方式的处理
    读excel和openpyxl模块
    linux上安装Docker
    [数据结构]堆的建立和排序
  • 原文地址:https://www.cnblogs.com/sssblog/p/11302360.html
Copyright © 2011-2022 走看看