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 #ifndef DATETIME_EX_H
17 #define DATETIME_EX_H
18
19 #include <ctime>
20 #include <cstdint>
21 namespace OHOS {
22
23 constexpr int64_t SEC_TO_NANOSEC = 1000000000;
24 constexpr int64_t SEC_TO_MICROSEC = 1000000;
25 constexpr int64_t SEC_TO_MILLISEC = 1000;
26 constexpr int64_t MILLISEC_TO_NANOSEC = 1000000;
27 constexpr int64_t MICROSEC_TO_NANOSEC = 1000;
28
29 constexpr int SECONDS_PER_HOUR = 3600; // 60 * 60
30 constexpr int SECONDS_PER_DAY = 86400; // 60 * 60 * 24
31
SecToNanosec(int64_t sec)32 static constexpr inline int64_t SecToNanosec(int64_t sec)
33 {
34 return sec * SEC_TO_NANOSEC;
35 }
36
MillisecToNanosec(int64_t millise)37 static constexpr inline int64_t MillisecToNanosec(int64_t millise)
38 {
39 return millise * MILLISEC_TO_NANOSEC;
40 }
41
MicrosecToNanosec(int64_t microsec)42 static constexpr inline int64_t MicrosecToNanosec(int64_t microsec)
43 {
44 return microsec * MICROSEC_TO_NANOSEC;
45 }
46
NanosecToSec(int64_t nanosec)47 static constexpr inline int64_t NanosecToSec(int64_t nanosec)
48 {
49 return nanosec / SEC_TO_NANOSEC;
50 }
51
NanosecToMillisec(int64_t nanosec)52 static constexpr inline int64_t NanosecToMillisec(int64_t nanosec)
53 {
54 return nanosec / MILLISEC_TO_NANOSEC;
55 }
56
NanosecToMicrosec(int64_t nanosec)57 static constexpr inline int64_t NanosecToMicrosec(int64_t nanosec)
58 {
59 return nanosec / MICROSEC_TO_NANOSEC;
60 }
61
62 /**
63 * The GetSecondsSince1970ToNow function get seconds from 1970 to now
64 */
65 int64_t GetSecondsSince1970ToNow();
66
67 /**
68 * The GetSecondsSince1970ToPointTime function get seconds from 1970 to inputtime
69 */
70 int64_t GetSecondsSince1970ToPointTime(struct tm inputTm);
71
72 /**
73 * The GetSecondsBetween function get seconds between inputTm1 and inputTm2.
74 */
75 int64_t GetSecondsBetween(struct tm inputTm1, struct tm inputTm2);
76
77 /**
78 * The GetDaysSince1970ToNow function get days from 1970 to now.
79 */
80 int64_t GetDaysSince1970ToNow();
81
82 /**
83 * The GetLocalTimeZone function get current timezone,
84 * return true if get success, else false.
85 */
86 bool GetLocalTimeZone(int& timezone);
87
88 /**
89 * The GetSystemCurrentTime function get current time,
90 * return true if get success, else false.
91 */
92 bool GetSystemCurrentTime(struct tm* curTime);
93
94 /**
95 * The GetTickCount function get current milliseconds since the system was started.
96 */
97 int64_t GetTickCount();
98
99 /**
100 * The GetMicroTickCount function get current microseconds since the system was started.
101 */
102 int64_t GetMicroTickCount();
103 }
104
105 #endif
106