• 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 #ifndef TIME_UTILS_H
17 #define TIME_UTILS_H
18 
19 #include <chrono>
20 #include <cstdio>
21 #include <stdlib.h>
22 #include <string>
23 #include <time.h>
24 
25 #include "update_log.h"
26 
27 namespace OHOS {
28 namespace UpdateEngine {
29 class TimeUtils {
30 static constexpr int64_t BOOT_COMPLETE_SIMULATE_DURATION = 60L;           // 模拟开机广播:时间判断门限
31 public:
GetTimestamp()32     static int64_t GetTimestamp()
33     {
34         time_t currentTime = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
35         return static_cast<int64_t>(currentTime);
36     }
37 
GetTimestampByMilliseconds()38     static int64_t GetTimestampByMilliseconds()
39     {
40         return std::chrono::duration_cast<std::chrono::milliseconds>
41             (std::chrono::system_clock::now().time_since_epoch()).count();
42     }
43 
GetPrintTimeStr(int64_t time)44     static std::string GetPrintTimeStr(int64_t time)
45     {
46         time_t printTime = static_cast<time_t>(time);
47         tm *tmpTm = localtime(&printTime);
48         if (tmpTm == NULL) {
49             return "";
50         }
51         return asctime(tmpTm);
52     }
53 
GetRandTime(int32_t minTime,int32_t maxTime)54     static int64_t GetRandTime(int32_t minTime, int32_t maxTime)
55     {
56         // 随机 minTime ~ maxTime时间
57         if (maxTime < minTime) {
58             return minTime;
59         }
60         srand((time(NULL)));
61         return minTime + rand() % (maxTime - minTime);
62     }
63 
GetSystemBootDuration()64     static int64_t GetSystemBootDuration()
65     {
66         int64_t bootSeconds =
67             std::chrono::duration_cast<std::chrono::seconds>(std::chrono::steady_clock::now().time_since_epoch())
68                 .count();
69         int64_t currentTime = GetTimestamp();
70         int64_t bootTime = currentTime - bootSeconds;
71         ENGINE_LOGI("GetSystemBootDuration bootSeconeds is %{public}s, bootTime is %{public}s",
72             std::to_string(bootSeconds).c_str(), GetPrintTimeStr(bootTime).c_str());
73         return bootSeconds;
74     }
75 
IsInRebootDuration()76     static bool IsInRebootDuration()
77     {
78         return GetSystemBootDuration() <= BOOT_COMPLETE_SIMULATE_DURATION;
79     }
80 };
81 } // namespace UpdateEngine
82 } // namespace OHOS
83 #endif // TIME_UTILS_H