http://blog.youkuaiyun.com/firebird321/article/details/2559103
日期转换为数字 #include "boost/date_time/gregorian/gregorian.hpp" #include <iostream> #include <string> int main() { using namespace boost::gregorian; try { // The following date is in ISO 8601 extended format (CCYY-MM-DD) std::string s("2001-10-9"); //2001-October-09 date d(from_simple_string(s)); std::cout << to_simple_string(d) << std::endl; //2001-Oct-09 //Read ISO Standard(CCYYMMDD) and output ISO Extended std::string ud("20011009"); //2001-Oct-09 date d1(from_undelimited_string(ud)); //2001-10-09 std::cout << to_iso_extended_string(d1) << std::endl; //Output the parts of the date - Tuesday October 9, 2001 date::ymd_type ymd = d1.year_month_day(); greg_weekday wd = d1.day_of_week(); //Tuesday October 9, 2001 std::cout << wd.as_long_string() << " " << ymd.month.as_long_string() << " " << ymd.day << ", " << ymd.year << std::endl; //Let's send in month 25 by accident and create an exception std::string bad_date("20012509"); //2001-??-09 std::cout << "An expected exception is next: " << std::endl; date wont_construct(from_undelimited_string(bad_date)); //use wont_construct so compiler doesn't complain, but you wont get here! //An expected exception is next:
//exception: Month number is out of range 1..12 std::cout << "oh oh, you shouldn't reach this line: " << to_iso_string(wont_construct) << std::endl; } catch(std::exception& e) { std::cout << " Exception: " << e.what() << std::endl; } return 0; } 计算给定日期与当前日期的差#include "boost/date_time/gregorian/gregorian.hpp" #include <iostream> int main() { using namespace boost::gregorian; std::string s; std::cout << "Enter birth day YYYY-MM-DD (eg: 2002-02-01): "; std::cin >> s; try { date birthday(from_simple_string(s)); date today = day_clock::local_day(); days days_alive = today - birthday; days one_day(1); if (days_alive == one_day) { std::cout << "Born yesterday, very funny" << std::endl; } else if (days_alive < days(0)) { std::cout << "Not born yet, hmm: " << days_alive.days() << " days" <<std::endl; } else { std::cout << "Days alive: " << days_alive.days() << std::endl; } } catch(...) { std::cout << "Bad date entered: " << s << std::endl; } return 0; }
输出结果: (假设当前日期为 2008-6-23) 如果输入: 2008-6-22; 输出结果为Born yesterday, very funny
如果输入: 2008-06-21; 输出结果为Days alive: 2
如果输入: 2008-06-24; 输出结果为Not born yet, hmm: -1 days
获得当前日期是本年的第几天; 计算当前日期离下一年的给定日期距离多少天 #include <iostream> #include "boost/date_time/gregorian/gregorian.hpp" int main() { using namespace boost::gregorian; date today = day_clock::local_day();
//Jan 这个宏表示 1 月
/* 1 - 12 月的宏定义如下
Jan;Feb;Mar;Apr;May;Jun;Jul;Aug;Sep;Oct;Nov;Dec; 这两个宏我没搞清楚是什么定义,暂时放一下
NotAMonth;NumMonths; */ partial_date new_years_day(1,Jan); //Subtract two dates to get a duration days days_since_year_start = today - new_years_day.get_date(today.year()); std::cout << "Days since Jan 1: " << days_since_year_start.days() << std::endl; days days_until_year_start = new_years_day.get_date(today.year()+1) - today; std::cout << "Days until next Jan 1: " << days_until_year_start.days() << std::endl; return 0; }; 输出结果: (假设当前日期为 2008-6-23) Days since Jan 1: 174 (表示距离08年1月1日,相隔 174 天) Days until next Jan 1: 192 (表示距离09年1月1日,相差 192 天) 给定年月,获得剩下年月的天数 int main() { using namespace boost::gregorian; greg_year year(1400); greg_month month(1); // get a month and a year from the user try { int y, m; std::cout << " Enter Year(ex: 2002): "; std::cin >> y; year = greg_year(y); std::cout << " Enter Month(1..12): "; std::cin >> m; month = greg_month(m); } catch(bad_year by) { std::cout << "Invalid Year Entered: " << by.what() << '/n' << "Using minimum values for month and year." << std::endl; } catch(bad_month bm) { std::cout << "Invalid Month Entered" << bm.what() << '/n' << "Using minimum value for month. " << std::endl; } date start_of_next_year(year+1, Jan, 1); date d(year, month, 1); // add another month to d until we enter the next year. while (d < start_of_next_year){ std::cout << to_simple_string(d.end_of_month()) << std::endl; d += months(1); } return 0; } 输入: 2008(回车) 12(回车) 输出结果: 2008-Dec-31
输入: 2008(回车) 1(回车) 输出结果:
2008-Jan-31 2008-Feb-29 2008-Mar-31 2008-Apr-30 2008-May-31 2008-Jun-30 2008-Jul-31 2008-Aug-31 2008-Sep-30 2008-Oct-31 2008-Nov-30 2008-Dec-31