代码改变世界

Boost.Date_Time 使用

2012-12-26 11:45  Scott Guthrie Liu  阅读(209)  评论(0编辑  收藏  举报

Date Programming

View Code
#include <boost/date_time/gregorian/gregorian.hpp>

using namespace boost::gregorian;;

int main()
{
    date d1(2002,Feb,1),d3, d4(2002,Feb,1);
    date weekstart(2002,Feb,1);
    date weekend  = weekstart + weeks(1);
    date d2 = d1 + days(5);
    date today = day_clock::local_day();
    if (d2 >= today) {} //date comparison operators 

    date_period thisWeek(d1,d2);
    if (thisWeek.contains(today)) {}//do something 

    //iterate and print the week
    day_iterator itr(weekstart);
    while (itr <= weekend) {
     std::cout << (*itr) << std::endl;
     ++itr;
    }  
    //input streaming 
    std::stringstream ss("2004-Jan-1");
    ss >> d3;

    //date generator functions 
    date d5 = next_weekday(d4, (greg_weekday)Tuesday); //calculate Sunday following d4

    //US labor day is first Monday in Sept 
    typedef nth_day_of_the_week_in_month nth_dow;
    nth_dow labor_day(nth_dow::first,Monday, Sep); 
    //calculate a specific date for 2004 from functor 
    date d6 = labor_day.get_date(2004); 

}

Time Programming

#include "boost/date_time/posix_time/posix_time.hpp"

Local Time Programming

 

boost时间有些感觉并不好用,过度复杂化了。