如何使用没有前导零的 Boost.Date_Time 进行格式化?
如何格式化 boost::posix_time::ptime 对象而不用零填充数字?
例如,我想显示 6/7/2011 6:30:25 PM
和不 06/07/2011 06:30:25 PM
代码>.
在 .NET 中,格式字符串类似于“m/d/yyyy h:mm:ss tt”。
下面是一些以错误方式执行此操作的代码,只是为了获得一个想法:
boost::gregorian::date baseDate(1970, 1, 1);
boost::posix_time::ptime shiftDate(baseDate);
boost::posix_time::time_facet *facet = new time_facet("%m/%d/%Y");
cout.imbue(locale(cout.getloc(), facet));
cout << shiftDate;
delete facet;
Output: 01/01/1970
How can you format a boost::posix_time::ptime object without padding the numbers with zeros?
For example, I want to display 6/7/2011 6:30:25 PM
and not 06/07/2011 06:30:25 PM
.
In .NET, the format string would be something like "m/d/yyyy h:mm:ss tt".
Here is some code to do it the wrong way, just to get an idea:
boost::gregorian::date baseDate(1970, 1, 1);
boost::posix_time::ptime shiftDate(baseDate);
boost::posix_time::time_facet *facet = new time_facet("%m/%d/%Y");
cout.imbue(locale(cout.getloc(), facet));
cout << shiftDate;
delete facet;
Output: 01/01/1970
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,此功能并未内置于 Boost.DateTime 中,但编写自己的格式化函数非常简单,例如:
To my knowledge this capability is not built into Boost.DateTime, but it's pretty trivial to write your own formatting functions, e.g.:
我完全同意其他回复:似乎没有格式化说明符可以为日期提供单个数字日期。
一般来说,有一种方法可以使用格式化字符串(几乎与常见的
strftime
格式相同)。例如,这些格式说明符类似于:"%b %d, %Y"
。tgamblin 在这里提供了很好的解释 。
I wholly agree with the other response: there does not seem to be a formatter specifier that gives the date with a single digit day-of-the-month.
In general, there is a way to use formatter strings (nearly identical to the common
strftime
formats). These format specifiers look like, for example:"%b %d, %Y"
.tgamblin provided a nice explanation here.