Skip to content

Commit 2904fef

Browse files
fix time related issues on mac (alibaba#1122) (alibaba#1125)
fix time related issues on mac Co-authored-by: zhenjiaseu <zhenjiaseu@gmail.com>
1 parent 51de7e8 commit 2904fef

1 file changed

Lines changed: 19 additions & 14 deletions

File tree

ecosystem/oss.cpp

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,14 @@ static SimpleDOM::Node get_xml_node(HTTP_STACK_OP& op) {
7474
}
7575

7676
// convert oss last modified time, e.g. "Fri, 04 Mar 2022 02:46:25 GMT"
77-
static time_t get_lastmodified(const char* s) {
78-
struct tm tm;
79-
memset(&tm, 0, sizeof(struct tm));
80-
strptime(s, "%a, %d %b %Y %H:%M:%S %Z", &tm);
77+
static time_t get_lastmodified(std::string_view sv) {
78+
if (sv.size() != 29) {
79+
LOG_ERROR_RETURN(0, 0, "invalid lastmodified time: ", sv);
80+
}
81+
struct tm tm{};
82+
if (!strptime(sv.data(), "%a, %d %b %Y %H:%M:%S GMT", &tm)) {
83+
LOG_ERROR_RETURN(0, 0, "invalid lastmodified time: ", sv);
84+
}
8185
return timegm(&tm); // GMT
8286
}
8387

@@ -87,9 +91,8 @@ static time_t get_list_lastmodified(std::string_view sv) {
8791
if (sv.size() != 24) {
8892
LOG_ERROR_RETURN(0, 0, "invalid lastmodified time: ", sv);
8993
}
90-
struct tm tm;
91-
memset(&tm, 0, sizeof(struct tm));
92-
if (!strptime(sv.data(), "%Y-%m-%dT%H:%M:%S.000%Z", &tm)) {
94+
struct tm tm{};
95+
if (!strptime(sv.data(), "%Y-%m-%dT%H:%M:%S.000Z", &tm)) {
9396
LOG_ERROR_RETURN(0, 0, "invalid lastmodified time: ", sv);
9497
}
9598
return timegm(&tm); // GMT
@@ -833,7 +836,7 @@ int OssClient::fill_meta(HTTP_STACK_OP& op, ObjectMeta& meta) {
833836
if (it.second().empty())
834837
meta.set_mtime(0);
835838
else
836-
meta.set_mtime(get_lastmodified(it.second().data()));
839+
meta.set_mtime(get_lastmodified(it.second()));
837840
}
838841

839842
it = op.resp.headers.find("ETag");
@@ -1149,7 +1152,7 @@ int OssClient::batch_get_objects(std::vector<GetObjectParameters>& params) {
11491152
param.meta->set_etag(meta_map["ETag"]);
11501153
param.meta->set_storage_class(meta_map["x-oss-storage-class"]);
11511154
param.meta->set_mtime(
1152-
get_lastmodified(meta_map["Last-Modified"].c_str()));
1155+
get_lastmodified(meta_map["Last-Modified"]));
11531156
param.meta->set_crc64(
11541157
estring_view(meta_map["x-oss-hash-crc64ecma"]).to_uint64());
11551158
param.meta->set_size(
@@ -1653,11 +1656,13 @@ class BasicAuthenticator : public Authenticator {
16531656
void update_gmt_date() { // avoid updating GMT Time every time
16541657
time_t t = photon::now / 1000 / 1000;
16551658
if (t - m_last_tim > GMT_UPDATE_INTERVAL || m_last_tim == 0) {
1656-
m_last_tim = t;
1657-
std::time_t tm = std::time(nullptr);
1658-
struct tm* p = gmtime(&tm);
1659-
strftime(m_gmt_date, GMT_DATE_LIMIT, "%a, %d %b %Y %H:%M:%S %Z", p);
1660-
strftime(m_gmt_date_iso8601, GMT_DATE_LIMIT, "%Y%m%dT%H%M%SZ", p);
1659+
std::time_t now = std::time(nullptr);
1660+
struct tm tm{};
1661+
if (gmtime_r(&now, &tm)) {
1662+
m_last_tim = now;
1663+
strftime(m_gmt_date, GMT_DATE_LIMIT, "%a, %d %b %Y %H:%M:%S GMT", &tm);
1664+
strftime(m_gmt_date_iso8601, GMT_DATE_LIMIT, "%Y%m%dT%H%M%SZ", &tm);
1665+
}
16611666
}
16621667
}
16631668

0 commit comments

Comments
 (0)