I have a C++ REST client and a REST server running in Chrome as a JavaScript code. Both the client and server are running on the same Windows machine. However, I’m encountering a strange issue where the request received time on the server is earlier than the request sent time from the client.
Here’s a snippet of my server-side JavaScript code:
const recievedTime = performance.now();
const timestamp = new Date(
performance.timeOrigin + recievedTime
).toISOString();
And here’s a snippet of my client-side C++ code:
// Get the current time point.
auto now = std::chrono::system_clock::now();
// Convert to time_t for formatting.
std::time_t currentTimeT = std::chrono::system_clock::to_time_t(now);
// Get the local time components (single argument to std::localtime).
std::tm localTime = *std::localtime(¤tTimeT); // Dereference the returned pointer.
// Create an ostringstream for formatting.
std::ostringstream formattedTime;
// Use put_time for efficient formatting.
formattedTime << std::put_time(&localTime, "%Y-%m-%d");
formattedTime << 'T';
formattedTime << std::put_time(&localTime, "%H:%M:%S");
// Get fractional seconds with nanosecond precision.
auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(now.time_since_epoch());
int64_t fractionalSeconds = duration.count() % 1000000000;
// Format milliseconds with 3-digit precision.
formattedTime << '.' << std::setfill('0') << std::setw(3) << (fractionalSeconds / 1000000);
// Append 'Z' for UTC time zone.
formattedTime << 'Z';
std::string timeStamp = formattedTime.str();
I’m expecting the server to receive the request with a timestamp that matches or is later than the timestamp generated on the client. However, I’m observing that the server’s received time is earlier.
Client sent time : 2024-03-12T09:38:03.404Z
Server receive time : 2024-03-12T09:38:03.398Z
Could someone help me understand why this discrepancy might be happening? Is there an issue with how I’m generating or handling timestamps in either the C++ client or JavaScript server code?