Closed YvesZHI closed 2 years ago
Do you have a type-o above? getCurrentTimePoint1
and getCurrentTimePoint2
are identical.
In any event, I recommend looking at the implementation of system_clock::now()
. See what it calls, and see if you can replicate your results calling that directly. system_clock::now()
is typically a thin wrapper around clock_gettime
or gettimeofday
.
For example: https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/src/c%2B%2B11/chrono.cc#L51-L74
Also double-check the types of nanoseconds::rep
, milliseconds::rep
and seconds::rep
. (https://stackoverflow.com/a/20170989/576911)
They should all be signed 64 bits. If they aren't chrono is likely overflowing.
Do you have a type-o above?
getCurrentTimePoint1
andgetCurrentTimePoint2
are identical.
You see that's why I said it was so weird. The two functions are exactly the same except their names. But they returns the different values.
I will do what you proposed here and reply immediately when I figure it out.
Well, this is a very stupid mistake.
template <typename T = std::chrono::milliseconds>
inline Clock<T> getCurrentTimePoint(int8_t timeZone = 0) {
return std::chrono::time_point_cast<T>(std::chrono::system_clock::now()) +
std::chrono::hours {timezone}; // typo error! timeZone, instead of timezone
}
The variable timezone
has been defined (man7.org/linux/man-pages/man3/tzset.3.html) and been included by chrono. That's why the compiler didn't generate any error.
I think modules may fix this, not positive. Looking forward to that!
https://stackoverflow.com/questions/70577774/why-does-the-function-about-getting-current-time-return-a-wrong-time-point
I just got a very weird bug.
As mentioned in the link, it seems that
system_clock::now()
gave me a time point of 1200 days ago. But I have no idea why.My machine
What I've done I defined some functions as below:
After testing, I got this:
time(nullptr)
is always correct;std::chrono::time_point_cast<std::chrono::seconds>(std::chrono::system_clock::now());
is always correct;getCurrentTimePoint()
is always wrong;getCurrentTimePoint2()
is always correct;getCurrentTimePoint1()
has been wrong for about one day but after testing many times, it becomes correct.I'm developing a web server, with a coroutine framwork. I don't know all of details of the framework, all I know is that it's similar with grpc and it has some mechanism to do the time synchronization. I developed a simple demo without the framework and this issue is just gone. So for now it seems that the framework caused this issue.