xref: /ide/tools/previewer/util/TimeTool.cpp (revision 7c804472)
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 "TimeTool.h"
17
18#include <chrono>
19#include <iostream>
20#include <sstream>
21
22#include "LocalDate.h"
23
24const int BASE_YEAR = 1900;
25const int MILLISECOND_PERSECOND = 1000;
26
27std::string TimeTool::GetFormatTime()
28{
29    std::string timeNow = FormateTimeNow();
30    std::string formatTime = "[" + timeNow + "]";
31    return formatTime;
32}
33
34std::string TimeTool::GetTraceFormatTime()
35{
36    std::string traceTimeNow = FormateTimeNow();
37    return traceTimeNow;
38}
39
40std::string TimeTool::FormateTimeNow()
41{
42    std::pair<tm, int64_t> timePair = GetCurrentTime();
43    struct tm utcTime = timePair.first;
44    int64_t msTime = timePair.second;
45    const int fixedTimeWidth2 = 2;
46    const int fixedTimeWidth3 = 3;
47    const int fixedTimeWidth4 = 4;
48    std::ostringstream now;
49    now << FixedTime(utcTime.tm_year + BASE_YEAR, fixedTimeWidth4);
50    now << "-";
51    now << FixedTime(utcTime.tm_mon + 1, fixedTimeWidth2);
52    now << "-";
53    now << FixedTime(utcTime.tm_mday, fixedTimeWidth2);
54    now << "T";
55    now << FixedTime(utcTime.tm_hour, fixedTimeWidth2) ;
56    now << ":";
57    now << FixedTime(utcTime.tm_min, fixedTimeWidth2);
58    now << ":";
59    now << FixedTime(utcTime.tm_sec, fixedTimeWidth2);
60    now << ".";
61    now << FixedTime(msTime % MILLISECOND_PERSECOND, fixedTimeWidth3);
62    return now.str();
63}
64
65std::string TimeTool::FixedTime(int32_t time, int32_t width)
66{
67    std::string tm = std::to_string(time);
68    int len = tm.length();
69    if (len < width) {
70        for (int i = 0; i < width - len; i++) {
71            tm = "0" + tm;
72        }
73    }
74    return tm;
75}
76
77std::pair<tm, int64_t> TimeTool::GetCurrentTime()
78{
79    const std::time_t e8zone = 8 * 60 * 60 * 1000; // Time offset of GMT+08:00, in milliseconds, 8h*60m*60s*1000ms
80    std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
81    std::chrono::milliseconds millsec = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
82    std::time_t ms = millsec.count() + e8zone;
83    millsec = std::chrono::milliseconds(ms);
84    now = std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds>(millsec);
85    auto time = std::chrono::system_clock::to_time_t(now);
86    struct tm utcTime;
87    LocalDate::GmTimeSafe(utcTime, time);
88    std::pair<tm, int64_t> timePair = std::make_pair(utcTime, ms);
89    return timePair;
90}
91