Skip to content

Commit ea7c688

Browse files
IOTSDK-24021: Fix parsing of server DateTime
std::get_time does not claim support of time zones in the string parameter, and seems to be subject of locale set on the system. An example is a following result on linux: ParseTime(Thu, 1 Jan 1970 0:00:00 GMT)=-2209075200 ::strptime is also a subject to the time zones of locale, but it also claim support for parsing a time zone from the passed string. Thus, additional logic incorporated to offset parsed DataTime to the local timezone. Signed-off-by: Mykhailo Diachenko <ext-mykhailo.z.diachenko@here.com>
1 parent 6d25648 commit ea7c688

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

olp-cpp-sdk-authentication/src/AuthenticationClientUtils.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "ResponseFromJsonBuilder.h"
3333
#include "olp/core/http/NetworkResponse.h"
3434
#include "olp/core/http/NetworkUtils.h"
35+
#include "olp/core/logging/Log.h"
3536
#include "olp/core/utils/Base64.h"
3637
#include "olp/core/utils/Url.h"
3738

@@ -55,6 +56,7 @@ constexpr auto kOauthTimestamp = "oauth_timestamp";
5556
constexpr auto kOauthSignatureMethod = "oauth_signature_method";
5657
constexpr auto kVersion = "1.0";
5758
constexpr auto kHmac = "HMAC-SHA256";
59+
constexpr auto kLogTag = "AuthenticationClientUtils";
5860

5961
std::string Base64Encode(const Crypto::Sha256Digest& digest) {
6062
std::string ret = olp::utils::Base64Encode(digest.data(), digest.size());
@@ -98,13 +100,28 @@ constexpr auto kDate = "date";
98100
#define timegm _mkgmtime
99101
#endif
100102

101-
std::time_t ParseTime(const std::string& value) {
103+
std::time_t doParseTime(const std::string& value) {
102104
std::tm tm = {};
103105
std::istringstream ss(value);
104-
ss >> std::get_time(&tm, "%a, %d %b %Y %H:%M:%S %z");
106+
const auto format = "%a, %d %b %Y %H:%M:%S %Z";
107+
const auto r = ::strptime(value.c_str(), format, &tm);
108+
if (r != value.c_str() + value.size()) {
109+
OLP_SDK_LOG_WARNING(kLogTag, "Timestamp is not fully parsed" << value);
110+
}
105111
return timegm(&tm);
106112
}
107113

114+
std::time_t gmtEpochOffset() {
115+
const auto epochAsDateTime = "Thu, 1 Jan 1970 0:00:00 GMT";
116+
return doParseTime(epochAsDateTime);
117+
}
118+
119+
std::time_t ParseTime(const std::string& value) {
120+
const auto time = doParseTime(value);
121+
const auto offset = gmtEpochOffset();
122+
return time - offset;
123+
}
124+
108125
boost::optional<std::time_t> GetTimestampFromHeaders(
109126
const olp::http::Headers& headers) {
110127
auto it =

0 commit comments

Comments
 (0)