• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_util.h"
17 
18 #include <chrono>
19 #include <iomanip>
20 #include <sstream>
21 #include <thread>
22 
23 namespace OHOS {
24 namespace HiviewDFX {
25 namespace TimeUtil {
StrToTimeStamp(const std::string & tmStr,const std::string & format)26 time_t StrToTimeStamp(const std::string& tmStr, const std::string& format)
27 {
28     std::istringstream input(tmStr.c_str());
29     struct tm tmFormat = { 0 };
30     input.imbue(std::locale(setlocale(LC_ALL, nullptr)));
31     input >> std::get_time(&tmFormat, format.c_str());
32     if (input.fail()) {
33         return 0;
34     }
35 
36     return mktime(&tmFormat);
37 }
38 
GetTimeOfDay()39 uint64_t GetTimeOfDay()
40 {
41     struct timespec now = { 0, 0 };
42     int err = timespec_get(&now, TIME_UTC);
43     if (err == 0) {
44         return 0;
45     }
46     return static_cast<uint64_t>(now.tv_sec);
47 }
48 
GenerateTimestamp()49 uint64_t GenerateTimestamp()
50 {
51     auto now = std::chrono::system_clock::now();
52     auto secs = std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch());
53     return static_cast<uint64_t>(secs.count());
54 }
55 
GetMilliseconds()56 uint64_t GetMilliseconds()
57 {
58     auto now = std::chrono::system_clock::now();
59     auto millisecs = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
60     return millisecs.count();
61 }
62 
Sleep(unsigned int seconds)63 void Sleep(unsigned int seconds)
64 {
65     std::this_thread::sleep_for(std::chrono::seconds(seconds));
66 }
67 
GetMillSecOfSec()68 int GetMillSecOfSec()
69 {
70     auto now = std::chrono::system_clock::now();
71     auto millisecs = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
72     return millisecs.count() % SEC_TO_MILLISEC;
73 }
74 
GetTimeZone()75 std::string GetTimeZone()
76 {
77     std::string timeZone = "";
78     return timeZone;
79 }
80 
Get0ClockStampMs()81 int64_t Get0ClockStampMs()
82 {
83     time_t now = std::time(nullptr);
84     int64_t zero = now;
85     struct tm *l = std::localtime(&now);
86     if (l != nullptr) {
87         l->tm_hour = 0;
88         l->tm_min = 0;
89         l->tm_sec = 0;
90         zero = std::mktime(l) * SEC_TO_MILLISEC;  // time is 00:00:00
91     }
92     return zero;
93 }
94 } // namespace TimeUtil
95 } // namespace HiviewDFX
96 } // namespace OHOS
97