Home > Blockchain >  What does system_clock::now() value after seconds represent in C 20?
What does system_clock::now() value after seconds represent in C 20?

Time:07-29

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
}

CodePudding user response:

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).

  • Related