Boost日期处理
来源:互联网 发布:解压缩软件 安卓 编辑:程序博客网 时间:2024/05/01 02:03
http://blog.csdn.net/firebird321/article/details/2559103
Boost日期处理
分类: C/C++相关2008-06-17 23:38 1695人阅读 评论(2) 收藏 举报
stringdateexceptioniostreamcompileroutput
日期转换为数字 #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>intmain() { 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-312008-Feb-292008-Mar-312008-Apr-302008-May-312008-Jun-302008-Jul-312008-Aug-312008-Sep-302008-Oct-312008-Nov-302008-Dec-31
- Boost日期处理
- boost 时间与日期处理
- boost 时间与日期处理
- boost 时间与日期处理
- boost 时间与日期处理
- boost 时间与日期处理
- Boost之时间与日期处理
- Boost之日期时间处理(date_time库)
- boost处理日期和时间的总结
- boost 时间与日期处理测试
- boost的时间和日期处理1
- [Boost]boost的时间和日期处理日期的操作和时间的操作
- Boost库 对时间和日期的处理 date_timer库
- Boost库 对时间和日期的处理 date_timer库
- boost的时间和日期处理-(1)日期的操作
- boost日期、时间操作
- boost 时间和日期
- boost日期用法
- Firefox下载含空格的文件
- 同步、异步、互斥、信号量、阻塞、非阻塞
- oracle 数据快修复—之使用RMAN 块介质恢复
- maven 自动部署到 tomcat7
- MAVEN pom.xml 解读
- Boost日期处理
- 数据结构——排序(二)
- iOS中 判断字符串 中文字符 字母 数字 以及下划线
- 将Vim改造为强大的IDE—Vim集成Ctags/Taglist/Cscope/Winmanager/NERDTree/OmniCppComplete(有图有真相)
- maven构建项目引用spring找不到org.springframework.transaction
- bat创建cocos2d-x项目
- DAS,NAS和SAN
- Java中的拦截器、过滤器、监听器
- SharePoint 2007 Init.js库没有注册 导致下拉框失效