如何自定义“时间戳” Boost.Log 的格式
我想得到年-月-日时:分:秒.分数(2位数字),如果我使用“%Y-%m-%d %H:%M:%S.%f”,我几乎得到了什么我想要秒的小数部分(最后一部分)例外,它在我的 Windows XP 上显示 6 位数字,我不知道如何只获取 2 位数字,知道吗?
I want to get year-month-day hour:minute:second.fraction(2 digits), if I use "%Y-%m-%d %H:%M:%S.%f", I got almost what I want exception for the fraction( last part ) of seconds, it's showing 6 digits on my Windows XP, I don't know how to get 2 digits only, any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我们用这个类来解决这个问题:
初始化如下:
并使用如下:
显然,该类允许我们访问或格式化我们希望的日期的任何部分
We addressed it with this class:
Initialized like this:
And used like this:
The class, obviously, lets us access or format any portion of the date we wish
我正在使用这个
初始化为
并用作
I'm using this
initialized as
and used as
Boost.DateTime(Boost.Log 所依赖的)似乎不支持专门的小数秒格式,因此唯一的方法是编写自己的自定义属性格式化程序,或者(更简单但不太好的方法)稍微修改您的格式化代码。
而不是这样的事情:
我自己没有测试过它,但我相信它应该可以工作:第一个
fmt::date_time()
将返回不带小数秒的时间戳,而第二个>fmt::date_time()
将仅返回秒的小数部分,该值将被fmt::format()
削减为两位数。Boost.DateTime (upon which Boost.Log relies) doesn't seem to support specialized fractional seconds formatting, so the only way to do that would be to write your own custom attribute formatter, or (the easier, but less nice way) to slightly modify your formatting code.
Instead of something like this:
I haven't tested it myself, but I believe it should work: the first
fmt::date_time()
will return the timestamp without the fractional seconds, while the secondfmt::date_time()
will return just the fractional seconds, which will be cut to two digits by thefmt::format()
.试试这个。它在 Linux 下对我有用。
Try this one. It works for me under Linux.