boost日期时间--输出格式
boost date time -- output format
假设我有以下代码:
#include <boost/date_time.hpp>
#include <iostream>
#include <locale>
#include <sstream>
int main()
{
boost::local_time::local_time_facet* facet = new boost::local_time::local_time_facet("%e %b %Y %T %q");
std::ostringstream date_osstr;
date_osstr.imbue(std::locale(date_osstr.getloc(), facet));
const boost::posix_time::ptime& now = boost::posix_time::second_clock::local_time();
date_osstr << now;
std::cout << date_osstr.str() << 'n';
}
我期望输出应该具有以下格式:
2003年7月1日10:52:37+0200
但是输出具有以下格式:
2014年4月28日12:40:04
为什么?我做错了什么?我该怎么修?
您需要实际使用local_date_time
才能从local_time_facet
:中获益
#include <boost/date_time.hpp>
#include <boost/date_time/local_time/local_time.hpp>
#include <iostream>
#include <locale>
#include <sstream>
using namespace boost;
static local_time::time_zone_ptr const utc_time_zone(new local_time::posix_time_zone("GMT"));
int main()
{
local_time::local_time_facet* facet = new local_time::local_time_facet("%e %b %Y %T %q");
posix_time::ptime my_ptime = posix_time::second_clock::universal_time();
local_time::local_date_time now(my_ptime, utc_time_zone);
std::ostringstream date_osstr;
date_osstr.imbue(std::locale(date_osstr.getloc(), facet));
date_osstr << now;
std::cout << date_osstr.str() << 'n';
}
打印
28 Apr 2014 12:59:01 +0000
查看Coliru直播
您使用的方面用于local_time对象,但您正在打印posix_time。相反,使用这个:
boost::posix_time::time_facet* facet = new boost::posix_time::time_facet("%e %b %Y %T %q");
相关文章:
- 保证最大输出长度为双精度,并设置默认输出格式的精度
- 输出使用 cout 转换为 ASCII 的文本时输出格式不正确,C++
- 如何将 Openh264 解码帧无延迟输出格式转换为 OpenCV 矩阵
- 为什么int8_t的输出格式使用 4 个字节?
- 使用setW和setFill的输出格式
- TensorFlow图像识别输出格式C
- 背诵和提示输出对齐C++,输出格式
- 哪些数据文件从python(或其他)中读取的C 输出格式更有效
- 读取C 和输出格式的文本文件
- 窗口上的输出格式
- 整数输出格式.什么是 printf(%.3x) 的类似物
- C++输出格式错误
- c++输出格式问题
- 以C++格式输出格式
- 设置输出格式以类似于C++中的表
- 使用iomanip将数据输出格式化为具有Qt的文本文件
- 输出格式错误
- cin/ofstream的输出格式不同
- boost日期时间--输出格式
- __int64将输出格式化为十六进制