比较日期c++(使用boost)
Compare dates c++ (using boost)
我需要能够在c++中转换和比较日期。我发现boost库非常适合我的需求,但我无法使其正常工作:
// include headers...
using namespace boost::posix_time;
using namespace boost::gregorian;
ptime p1(second_clock::universal_time());
p1 -= weeks(5); // Subtract 5 weeks from p1 using boost...
std::string old_date("2011-11-19"); // format:YYYY-MM-DD
std:stringstream ss(old_date);
ptime p2;
p2 << ss;
if(p1 <= p2){
// this should not happen in this case (yet it does!) p1 > p2!!
}
基本上,我希望能够减去本地日期的周(或月),然后将结果与YYYY-MM-DD格式的字符串日期进行比较。。。
您的区域设置可能无法识别YYYY-MM-DD格式的日期。请尝试像格式化字符串示例中那样设置输入方面的格式。
下面的例子展示了如何将字符串流的输入和输出格式设置为"ISO扩展":
#include <iostream>
#include <boost/date_time/posix_time/posix_time.hpp>
int main()
{
using namespace boost::gregorian;
using namespace boost::posix_time;
using namespace std;
stringstream ss;
/****** format strings ******/
time_facet* output_facet = new time_facet();
time_input_facet* input_facet = new time_input_facet();
ss.imbue(locale(ss.getloc(), output_facet));
ss.imbue(locale(ss.getloc(), input_facet));
output_facet->format(time_facet::iso_time_format_extended_specifier);
input_facet->format(time_input_facet::iso_time_format_extended_specifier);
ptime t(second_clock::local_time());
ss << t;
cout << ss.str() << endl; // 2012-08-03 23:46:38
ss.str("2000-01-31 12:34:56");
ss >> t;
assert(t.date().year() == 2000);
assert(t.date().month() == 1);
assert(t.date().day() == 31);
assert(t.time_of_day().hours() == 12);
assert(t.time_of_day().minutes() == 34);
assert(t.time_of_day().seconds() == 56);
}
相关文章:
- boost::asio::steady_timer()与sleep()我应该使用哪一个
- 当我尝试使用 sstream 和分面将 Boost Time_duration转换为字符串时,我没有得到所需的格式
- 如何使用boost::具有嵌套结构和最小代码更改的序列化
- 使用Boost Interprocess创建托管共享内存需要很长时间
- 如果C++对象的类在另一个boost模块中声明,如何使用boost将指向该对象的指针返回到python
- 如何在 Boost.Asio 中使用 Zero-copy sendmsg/receive
- 使用 boost 进行标记化会给出相同的输出
- 如何使用 Boost Asio 在 Android 上获取我的本地 udp IP 地址?
- 在成员dynamic_bitset上使用 boost::from_block_range 时出错,但在本地dynamic
- 将 boost::odeint 与向量类一起使用,而无需调整向量的大小
- Growing Boost.使用单个写入器的进程间内存映射文件
- 使用Boost使用自定义环境创建子过程
- Boost ::使用与多个水槽的同一后端使用相同的后端安全性
- boost::使用成员函数绑定回调的帮助
- Boost使用指向对象的指针向量(C++-linux)
- Boost::使用字符数组格式化
- 是否可以使用Boost.使用预分配的缓冲区格式化
- 为什么 Boost 使用全局函数覆盖来实现自定义验证程序"Program Options"
- 链接错误Boost.使用stlport作为标准库的Python
- 通过Python BOOST使用派生类(c++)