Closed TomGarden closed 1 year ago
You question appears to boil down to the output of the values of the count of nanoseconds. The value of sysTimestemp
is correct at 1667271043906963213
. This is the number of nanoseconds that your call to system_clock::now()
put into timePoint
:
std::chrono::system_clock::time_point timePoint = std::chrono::system_clock::now(); // 1667271043906963213ns
If you run this code again, and compare sysTimestemp == timePoint.time_since_epoch().count()
, you will find that they hold equal values. As you put a sys_time
into a zoned_time
, the sys_time
represents UTC, and that value remains constant as it is put into a zoned_time
, and retrievied via .get_sys_time()
.
The human-readable format of 1667271043906963213ns
is 2022-11-01 02:50:43.906963213
which is a UTC time stamp.
The local time in Shanghai at this UTC time point is 8h greater. When the zoned_time
is asked to compute the local time for Shanghai, all it does is add 8h to the underlying UTC time stamp. This results in:
1667299843906963213 2022-11-01 10:50:43.906963213
OK , now i understand myself wrong point .
I had a misunderstanding before : std::chrono::system_clock::now()
will change with system time zone .
Now i know : std::chrono::system_clock::now()
allways express UTC-0 time , the time zone of the operating system changes, does not affect the value of this api .
https://en.cppreference.com/w/cpp/chrono/system_clock say that :
The system_clock's time value can be internally adjusted at any time by the operating system, for example due to NTP synchronization or the user changing the system's clock. Daylight Savings Time and time zone changes, however, do not affect it since it is based on the UTC time-zone.
Thank you @HowardHinnant .
My Debian time zone setting : Asia/Shanghai
Test code :
In fact if only the timestamp is calculated , the result is this :
1667271043906963213 express 2022-11-01 10:50:43.906963213
1667299843906963213 express 2022-11-01 18:50:43.906963213
My expected output is :