17c804472Sopenharmony_ci/*
27c804472Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
37c804472Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
47c804472Sopenharmony_ci * you may not use this file except in compliance with the License.
57c804472Sopenharmony_ci * You may obtain a copy of the License at
67c804472Sopenharmony_ci *
77c804472Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
87c804472Sopenharmony_ci *
97c804472Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
107c804472Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
117c804472Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
127c804472Sopenharmony_ci * See the License for the specific language governing permissions and
137c804472Sopenharmony_ci * limitations under the License.
147c804472Sopenharmony_ci */
157c804472Sopenharmony_ci
167c804472Sopenharmony_ci#include "TimeTool.h"
177c804472Sopenharmony_ci
187c804472Sopenharmony_ci#include <chrono>
197c804472Sopenharmony_ci#include <iostream>
207c804472Sopenharmony_ci#include <sstream>
217c804472Sopenharmony_ci
227c804472Sopenharmony_ci#include "LocalDate.h"
237c804472Sopenharmony_ci
247c804472Sopenharmony_ciconst int BASE_YEAR = 1900;
257c804472Sopenharmony_ciconst int MILLISECOND_PERSECOND = 1000;
267c804472Sopenharmony_ci
277c804472Sopenharmony_cistd::string TimeTool::GetFormatTime()
287c804472Sopenharmony_ci{
297c804472Sopenharmony_ci    std::string timeNow = FormateTimeNow();
307c804472Sopenharmony_ci    std::string formatTime = "[" + timeNow + "]";
317c804472Sopenharmony_ci    return formatTime;
327c804472Sopenharmony_ci}
337c804472Sopenharmony_ci
347c804472Sopenharmony_cistd::string TimeTool::GetTraceFormatTime()
357c804472Sopenharmony_ci{
367c804472Sopenharmony_ci    std::string traceTimeNow = FormateTimeNow();
377c804472Sopenharmony_ci    return traceTimeNow;
387c804472Sopenharmony_ci}
397c804472Sopenharmony_ci
407c804472Sopenharmony_cistd::string TimeTool::FormateTimeNow()
417c804472Sopenharmony_ci{
427c804472Sopenharmony_ci    std::pair<tm, int64_t> timePair = GetCurrentTime();
437c804472Sopenharmony_ci    struct tm utcTime = timePair.first;
447c804472Sopenharmony_ci    int64_t msTime = timePair.second;
457c804472Sopenharmony_ci    const int fixedTimeWidth2 = 2;
467c804472Sopenharmony_ci    const int fixedTimeWidth3 = 3;
477c804472Sopenharmony_ci    const int fixedTimeWidth4 = 4;
487c804472Sopenharmony_ci    std::ostringstream now;
497c804472Sopenharmony_ci    now << FixedTime(utcTime.tm_year + BASE_YEAR, fixedTimeWidth4);
507c804472Sopenharmony_ci    now << "-";
517c804472Sopenharmony_ci    now << FixedTime(utcTime.tm_mon + 1, fixedTimeWidth2);
527c804472Sopenharmony_ci    now << "-";
537c804472Sopenharmony_ci    now << FixedTime(utcTime.tm_mday, fixedTimeWidth2);
547c804472Sopenharmony_ci    now << "T";
557c804472Sopenharmony_ci    now << FixedTime(utcTime.tm_hour, fixedTimeWidth2) ;
567c804472Sopenharmony_ci    now << ":";
577c804472Sopenharmony_ci    now << FixedTime(utcTime.tm_min, fixedTimeWidth2);
587c804472Sopenharmony_ci    now << ":";
597c804472Sopenharmony_ci    now << FixedTime(utcTime.tm_sec, fixedTimeWidth2);
607c804472Sopenharmony_ci    now << ".";
617c804472Sopenharmony_ci    now << FixedTime(msTime % MILLISECOND_PERSECOND, fixedTimeWidth3);
627c804472Sopenharmony_ci    return now.str();
637c804472Sopenharmony_ci}
647c804472Sopenharmony_ci
657c804472Sopenharmony_cistd::string TimeTool::FixedTime(int32_t time, int32_t width)
667c804472Sopenharmony_ci{
677c804472Sopenharmony_ci    std::string tm = std::to_string(time);
687c804472Sopenharmony_ci    int len = tm.length();
697c804472Sopenharmony_ci    if (len < width) {
707c804472Sopenharmony_ci        for (int i = 0; i < width - len; i++) {
717c804472Sopenharmony_ci            tm = "0" + tm;
727c804472Sopenharmony_ci        }
737c804472Sopenharmony_ci    }
747c804472Sopenharmony_ci    return tm;
757c804472Sopenharmony_ci}
767c804472Sopenharmony_ci
777c804472Sopenharmony_cistd::pair<tm, int64_t> TimeTool::GetCurrentTime()
787c804472Sopenharmony_ci{
797c804472Sopenharmony_ci    const std::time_t e8zone = 8 * 60 * 60 * 1000; // Time offset of GMT+08:00, in milliseconds, 8h*60m*60s*1000ms
807c804472Sopenharmony_ci    std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
817c804472Sopenharmony_ci    std::chrono::milliseconds millsec = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
827c804472Sopenharmony_ci    std::time_t ms = millsec.count() + e8zone;
837c804472Sopenharmony_ci    millsec = std::chrono::milliseconds(ms);
847c804472Sopenharmony_ci    now = std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds>(millsec);
857c804472Sopenharmony_ci    auto time = std::chrono::system_clock::to_time_t(now);
867c804472Sopenharmony_ci    struct tm utcTime;
877c804472Sopenharmony_ci    LocalDate::GmTimeSafe(utcTime, time);
887c804472Sopenharmony_ci    std::pair<tm, int64_t> timePair = std::make_pair(utcTime, ms);
897c804472Sopenharmony_ci    return timePair;
907c804472Sopenharmony_ci}
91