How to format date time object with format dd/mm/yyyy?(如何使用格式 dd/mm/yyyy 格式化日期时间对象?)
问题描述
如何使用 Boost 库在格式 dd/mm/yyyy H?
How could I print the current date, using Boost libraries, in the format dd/mm/yyyy H?
我有什么:
boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
cout << boost::posix_time::to_simple_string(now).c_str();
2009-Dec-14 23:31:40
但我想要:
2009 年 12 月 14 日 23:31:40
14-Dec-2009 23:31:40
推荐答案
如果您使用的是 Boost.Date_Time,这是使用 IO facet 完成的.
If you're using Boost.Date_Time, this is done using IO facets.
您需要包含 boost/date_time/posix_time/posix_time_io.hpp 以获得正确的 facet 类型定义(wtime_facet、time_facet 等.) 用于 boost::posix_time::ptime.完成后,代码非常简单.您在要输出到的 ostream 上调用 imbue,然后输出您的 ptime:
You need to include boost/date_time/posix_time/posix_time_io.hpp to get the correct facet typedefs (wtime_facet, time_facet, etc.) for boost::posix_time::ptime. Once this is done, the code is pretty simple. You call imbue on the ostream you want to output to, then just output your ptime:
#include <iostream>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/posix_time/posix_time_io.hpp>
using namespace boost::posix_time;
using namespace std;
int main(int argc, char **argv) {
time_facet *facet = new time_facet("%d-%b-%Y %H:%M:%S");
cout.imbue(locale(cout.getloc(), facet));
cout << second_clock::local_time() << endl;
}
输出:
14-Dec-2009 16:13:14
另见格式列表标记在boost文档中,以防你想输出一些更有趣的东西.
See also the list of format flags in the boost docs, in case you want to output something fancier.
这篇关于如何使用格式 dd/mm/yyyy 格式化日期时间对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用格式 dd/mm/yyyy 格式化日期时间对象?
基础教程推荐
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- CString 到 char* 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
