阅读和写作日期与boost

reading and writing dates with boost

本文关键字:日期 boost      更新时间:2023-10-16

我正在尝试以以下格式读取/写入日期:yyyy-mm-dddThh:mm:ssZ

我在做这个:

boost::posix_time::ptime t = boost::posix_time::time_from_string( "2012-02-20T00:26:39Z" );
std::cout << boost::posix_time::to_iso_extended_string( t ) << std::endl;

如果我删除最后一个Z,它会起作用,但如果我保留它,就会抛出异常(错误的词法转换:源类型值不能被解释为目标)。

有没有更好的方法来处理这些日期,而不需要手动删除/添加Z?根据我在这里和那里读到的内容,我理解Z的意思是GMT,如果它不存在,就意味着时间在当地时区。我想要一些通用且无错误的版本来阅读它。

查看Boost.DateTime IO教程中关于格式化字符串的部分。

http://www.boost.org/doc/libs/1_48_0/doc/html/date_time/date_time_io.html#format_strings

我希望你能在那里找到答案。