• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "time_format_utils.h"
17 
18 #include <chrono>
19 #include <ctime>
20 #include <iomanip>
21 #include <iostream>
22 #include <regex>
23 #include <sstream>
24 #include <string>
25 
26 #include "media_log.h"
27 
28 namespace {
29 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "TimeFormatUtils"};
30 }
31 
32 namespace OHOS {
33 namespace Media {
FormatDateTimeByTimeZone(const std::string & iso8601Str)34 std::string TimeFormatUtils::FormatDateTimeByTimeZone(const std::string &iso8601Str)
35 {
36     std::regex pattern(R"((\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(.\d{1,6})?((\+|-\d{4})?)Z?)");
37     std::smatch match;
38     if (!std::regex_match(iso8601Str, match, pattern)) {
39         return iso8601Str;  // not standard ISO8601 type string
40     }
41 
42     std::istringstream iss(iso8601Str);
43     std::tm tm;
44     if (!(iss >> std::get_time(&tm, "%Y-%m-%dT%H:%M:%S"))) {
45         return iso8601Str;  // cant prase time
46     }
47 
48     // time zone
49     time_t tt = mktime(&tm);
50     if (tt == -1) {
51         return iso8601Str;
52     }
53     uint32_t length = iso8601Str.length();
54     long diffTime = 0;
55     if (iso8601Str.substr(length - 1, length).compare("Z") != 0) {
56         int mins = std::stoi(iso8601Str.substr(length - 2, 2));
57         int hours = std::stoi(iso8601Str.substr(length - 4, 2));
58         char symbol = iso8601Str.at(length - 5);
59         long seconds = (hours * 60 + mins) * 60;
60         diffTime = symbol == '+' ? seconds : -seconds;
61     }
62 
63     // convert time to localtime
64     long timezone = 0;
65     std::tm timeWithOffset = *localtime(&tt);
66     if (timeWithOffset.tm_gmtoff != 0) {
67         timezone = timeWithOffset.tm_gmtoff;
68     }
69     auto localTime =
70         std::chrono::system_clock::from_time_t(std::mktime(&tm)) + std::chrono::seconds(timezone - diffTime);
71     std::time_t localTimeT = std::chrono::system_clock::to_time_t(localTime);
72     std::tm localTm = *std::localtime(&localTimeT);
73     std::ostringstream oss;
74     oss << std::put_time(&localTm, "%Y-%m-%d %H:%M:%S");
75     return oss.str();
76 }
77 
FormatDataTimeByString(const std::string & dataTime)78 std::string TimeFormatUtils::FormatDataTimeByString(const std::string &dataTime)
79 {
80     if (dataTime.compare("") == 0) {
81         return dataTime;
82     }
83     std::string::size_type position = dataTime.find(" ");
84     std::string data = "";
85     std::string time = "";
86     if (position == dataTime.npos) {
87         data = dataTime;
88         if (data.find("-") == data.npos) {
89             data += "-01-01";
90         } else if (data.find_first_of("-") == data.find_last_of("-")) {
91             data += "-01";
92         }
93         time += " 00:00:00";
94     } else {
95         data = dataTime.substr(0, position);
96         time = dataTime.substr(position);
97         if (data.find("-") == data.npos) {
98             data += "-01-01";
99         } else if (data.find_first_of("-") == data.find_last_of("-")) {
100             data += "-01";
101         }
102         if (time.find(":") == data.npos) {
103             time += ":00:00";
104         } else if (time.find_first_of(":") == time.find_last_of(":")) {
105             time += ":00";
106         } else {
107             time = time.substr(0, time.find("."));
108         }
109     }
110     MEDIA_LOGD("FormatDataTimeByString is: %{public}s%{public}s", data.c_str(), time.c_str());
111     return data + time;
112 }
113 
ConvertTimestampToDatetime(const std::string & timestamp)114 std::string TimeFormatUtils::ConvertTimestampToDatetime(const std::string &timestamp)
115 {
116     if (timestamp.empty()) {
117         MEDIA_LOGE("datetime is empty, format failed");
118         return "";
119     }
120 
121     time_t ts = stoi(timestamp);
122     tm *pTime;
123     char date[maxDateTimeSize];
124     char time[maxDateTimeSize];
125     pTime = localtime(&ts);
126     size_t sizeDateStr = strftime(date, maxDateTimeSize, "%Y-%m-%d", pTime);
127     size_t sizeTimeStr = strftime(time, maxDateTimeSize, "%H:%M:%S", pTime);
128     if (sizeDateStr != standardDateStrSize || sizeTimeStr != standardTimeStrSize) {
129         MEDIA_LOGE("datetime is invalid, format failed");
130         return "";
131     }
132 
133     return std::string(date) + " " + std::string(time);
134 }
135 }  // namespace Media
136 }  // namespace OHOS