最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c++ - What is the highest resolution which can be printed or formatted for a std::chrono::time_point? - Stack Overflow

programmeradmin2浏览0评论

I am writing some logic to serialize a std::chrono::time_point object to a JSON string.

Here is the code I have so far:

std::string get_json(const std::chrono::time_point<std::chrono::system_clock> timestamp) {

    std::string result = std::format(
        "{{\r\n\"timestamp\": \"{%FT%T%z}\"\r\n}}",
        timestamp
    );

    return result;
}

\r\n is used because this data will be sent to a HTTP interface.

I found this page which describes documentation for std::formatter for std::chrono::sys_time.

I have not found anything which appears to be more relevant to std::chrono::time_point, so I am guessing the format specifiers are the same.

Format specifiers appear to be missing for sub-second precision.

Currently, the code I have written will write a timestamp in the format

2025-03-08T19:54:00+0000

rather than something which higher resolution such as

2025-03-08T19:54:00.123456+0000

Is there a way to achieve formatting with higher resolution? Ideally microseconds or nanoseconds?

(Disclaimer: std::chrono::time_point may not support such high resolution. I haven't yet figured out its maximum precision.)

Note: Please be aware I am not asking what the highest resolution of any particular clock is. I am asking about printing or formatting a clock value, not the shortest tick size supported by any particular clock type.

I am writing some logic to serialize a std::chrono::time_point object to a JSON string.

Here is the code I have so far:

std::string get_json(const std::chrono::time_point<std::chrono::system_clock> timestamp) {

    std::string result = std::format(
        "{{\r\n\"timestamp\": \"{%FT%T%z}\"\r\n}}",
        timestamp
    );

    return result;
}

\r\n is used because this data will be sent to a HTTP interface.

I found this page which describes documentation for std::formatter for std::chrono::sys_time.

I have not found anything which appears to be more relevant to std::chrono::time_point, so I am guessing the format specifiers are the same.

Format specifiers appear to be missing for sub-second precision.

Currently, the code I have written will write a timestamp in the format

2025-03-08T19:54:00+0000

rather than something which higher resolution such as

2025-03-08T19:54:00.123456+0000

Is there a way to achieve formatting with higher resolution? Ideally microseconds or nanoseconds?

(Disclaimer: std::chrono::time_point may not support such high resolution. I haven't yet figured out its maximum precision.)

Note: Please be aware I am not asking what the highest resolution of any particular clock is. I am asking about printing or formatting a clock value, not the shortest tick size supported by any particular clock type.

Share Improve this question edited Mar 8 at 20:09 user2138149 asked Mar 8 at 19:56 user2138149user2138149 17.7k30 gold badges150 silver badges296 bronze badges 8
  • The title does not match the question in the body. 1. What is the highest resolution? 2. How to format timestamp with microseconds? – 3CxEZiVlQ Commented Mar 8 at 19:59
  • Please see this Stackoverflow answer: https://stackoverflow/a/8386373/27900156. – Sergey A Kryukov Commented Mar 8 at 20:05
  • It does match. I am asking about the maximum printable resolution, not the actual resolution supported by the underlying clock choice. – user2138149 Commented Mar 8 at 20:05
  • This question is similar to: How to get the precision of high_resolution_clock?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. – Sergey A Kryukov Commented Mar 8 at 20:05
  • No, that is not what I am asking about. The underlying resolution of the clock is irrelevant. – user2138149 Commented Mar 8 at 20:06
 |  Show 3 more comments

1 Answer 1

Reset to default 8

Slip a : into the right place and you've already got subsecond precision:

"{{\r\n\"timestamp\": \"{:%FT%T%z}\"\r\n}}",

https://gcc.godbolt./z/3jajKG7Tc

The %T will output whatever precision the time_point has. That precision varies with platform but is always subsecond. You can control the precision by changing the precision of the time_point using either implicit conversion (for finer) or floor, ceil or round (for coarser). E.g.:

std::chrono::floor<std::chrono::milliseconds>(timestamp)

You can also used the named conversions (floor, ceil or round) to convert to a finer precision if that is more convenient in your code. In this case all of the conversions are identical to the implicit conversion -- value preserving.

发布评论

评论列表(0)

  1. 暂无评论