-3

I'm exploring the timestamp in C++ 20 returned from system_clock::now() and when I print out the returned std::chrono::time_point it prints the date and time in the format YYYY-MM-DD HH:MM:SS.xxxxxxx.

Any idea what the xxxxxxx value is? I assumed microseconds initially but I realised microseconds are to six decimal places whereas this value is always to seven.

I'm running this in VS 2022.

#include <iostream>
#include <chrono>

int main()
{
    using namespace std::chrono;
    auto timeDate = zoned_time{ current_zone(), system_clock::now() };
    std::cout << timeDate << std::endl;  // Output: 2022-07-29 08:55:22.8582577 BST
}
康桓瑋
  • 33,481
  • 5
  • 40
  • 90
Crystal
  • 65
  • 1
  • 6
  • Parts of a second. – Some programmer dude Jul 29 '22 at 10:23
  • 2
    And regarding the part "it returns the date and time in the format..." that's wrong. The internal format of the time-point is implementation defined, the format is part of the output and has nothing to do with the time-point returned by the `now` function. – Some programmer dude Jul 29 '22 at 10:25
  • @Someprogrammerdude Can this be used to represent microseconds? I couldn't find documentation that mentioned what parts of a second represents relative to micro of milli seconds – Crystal Jul 29 '22 at 10:29
  • 1
    `auto timeDateInMicroseconds = std::chrono::time_point_cast(timeDate);`? See e.g. [this](https://godbolt.org/z/ch3qWPYnx) for an example. – Some programmer dude Jul 29 '22 at 10:57

1 Answers1

4

It's the fractional part of a second.

The output you're getting comes from operator<<(zoned_time). This outputs the time in the format "{:L%F %T %Z}" (see cppreference.com). The %T part of the format is equivalent to "%H:%M:%S" (reference) and the %S specifies the seconds as a decimal floating-point number with a precision matching that of the precision of the input (i.e. the precision of system_clock::now() in your case).

Klitos Kyriacou
  • 10,634
  • 2
  • 38
  • 70