Boost.Date TimeのGregorianで日付の処理
ある年がうるう年か調べたり、日付の足し引きをしたり、残り日数を求めたりできる便利機能が一通り揃ってます。
from_stringに文字列を渡してインスタンスを生成できるのもいいですね。
#include <iostream> #include <boost/date_time/gregorian/gregorian.hpp> #include <boost/format.hpp> int main(){ using namespace std; namespace gr = boost::gregorian; { gr::date date(2012, 2, 28); if(gr::gregorian_calendar::is_leap_year(date.year())){ cout << date.year() << "年はうるう年" << endl; } gr::date day_after_tomorrow = date + gr::date_duration(2); cout << gr::to_iso_extended_string(date) << " の2日後は " << gr::to_iso_extended_string(day_after_tomorrow) << endl; } cout << endl; { gr::date today = gr::day_clock::local_day(); cout << boost::format("Today: %s (%s)") % today % today.day_of_week() << endl; gr::date next_year = gr::from_string("2013/01/01"); gr::date_duration dur = next_year - today; cout << boost::format("2013年まであと%d日") % dur.days() << endl; } return 0; }
output:
2012年はうるう年 2012-02-28 の2日後は 2012-03-01 Today: 2012-May-27 (Sun) 2013年まであと219日