How to convert std::chrono::time_point to calendar datetime string with fractional seconds?

后端 未结 7 2090
生来不讨喜
生来不讨喜 2020-11-27 02:50

How to convert std::chrono::time_point to calendar datetime string with fractional seconds?

For example:

\"10-10-2012 12:38:40.123456\"         


        
7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-27 03:30

    I would have put this in a comment on the accepted answer, since that's where it belongs, but I can't. So, just in case anyone gets unreliable results, this could be why.

    Be careful of the accepted answer, it fails if the time_point is before the epoch.

    This line of code:

    std::size_t fractional_seconds = ms.count() % 1000;
    

    will yield unexpected values if ms.count() is negative (since size_t is not meant to hold negative values).

提交回复
热议问题