1 /*
2  * Copyright (c) 2024 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 <ctime>
17 #include <limits>
18 #include "bundle_active_util.h"
19 
20 namespace OHOS {
21 namespace DeviceUsageStats {
22 const int32_t MILLISECOND_TO_MICROSECOND = 1000;
23 const int32_t MILLISECOND_TO_SECOND = 1000;
24 const int32_t SECOND_TO_MILLISECOND = 1000;
25 const int64_t ONE_DAY_TIME = 24 * 60 * 60 *1000LL;
26 const int64_t WEEK_OFFSET = 6LL;
27 const int64_t DAYS_OF_WEEK = 7LL;
28 const int64_t HOUR_OF_MIDNIGHT = 0;
29 const int64_t MIN_OF_MIDNIGHT = 0;
30 const int64_t SECOND_TO_MIDNIGHT = 0;
31 const int64_t STATR_DAY_OF_MON = 1;
32 const int64_t STATR_MON_OF_YEAR = 0;
33 const int64_t ERROR_TIME = 0;
GetBundleUsageKey(const std::string & bundleName,const int32_t uid)34 std::string BundleActiveUtil::GetBundleUsageKey(const std::string &bundleName, const int32_t uid)
35 {
36     return bundleName + std::to_string(uid);
37 }
38 
GetFFRTDelayTime(const int64_t & delayTime)39 int64_t BundleActiveUtil::GetFFRTDelayTime(const int64_t& delayTime)
40 {
41     return delayTime * MILLISECOND_TO_MICROSECOND;
42 }
43 
GetIntervalTypeStartTime(const int64_t & timeStamp,const int32_t & intervalType)44 int64_t BundleActiveUtil::GetIntervalTypeStartTime(const int64_t& timeStamp, const int32_t& intervalType)
45 {
46     if (timeStamp <= 0) {
47         return ERROR_TIME;
48     }
49     time_t time = timeStamp / MILLISECOND_TO_SECOND;
50     std::tm* tm_time = std::localtime(&time);
51     if (tm_time == nullptr) {
52         return ERROR_TIME;
53     }
54     tm_time->tm_hour = HOUR_OF_MIDNIGHT;
55     tm_time->tm_min = MIN_OF_MIDNIGHT;
56     tm_time->tm_sec = SECOND_TO_MIDNIGHT;
57     if (intervalType == PERIOD_WEEKLY) {
58         int64_t weekday = tm_time->tm_wday;
59         time_t startOfDay = mktime(tm_time) * SECOND_TO_MILLISECOND;
60         time_t weekDayTime = (weekday + WEEK_OFFSET) % DAYS_OF_WEEK * ONE_DAY_TIME;
61         return startOfDay - weekDayTime;
62     }
63     switch (intervalType) {
64         case PERIOD_DAILY:
65             break;
66         case PERIOD_MONTHLY:
67             tm_time->tm_mday = STATR_DAY_OF_MON;
68             break;
69         case PERIOD_YEARLY:
70             tm_time->tm_mon = STATR_MON_OF_YEAR;
71             tm_time->tm_mday = STATR_DAY_OF_MON;
72             break;
73         default:
74             break;
75     }
76     return mktime(tm_time) * SECOND_TO_MILLISECOND;
77 }
78 
StringToInt32(const std::string & str)79 int32_t BundleActiveUtil::StringToInt32(const std::string& str)
80 {
81     char* pEnd = nullptr;
82     errno = 0;
83     int64_t res = std::strtol(str.c_str(), &pEnd, 10);
84     if (errno == ERANGE || pEnd == str.c_str() || *pEnd != '\0' ||
85         (res < std::numeric_limits<int32_t>::min()) ||
86         res > std::numeric_limits<int32_t>::max()) {
87         return 0;
88     }
89     return static_cast<int32_t>(res);
90 }
91 
StringToInt64(const std::string & str)92 int64_t BundleActiveUtil::StringToInt64(const std::string& str)
93 {
94     char* pEnd = nullptr;
95     errno = 0;
96     int64_t res = std::strtol(str.c_str(), &pEnd, 10);
97     if (errno == ERANGE || pEnd == str.c_str() || *pEnd != '\0') {
98         return 0;
99     }
100     return res;
101 }
102 } // namespace DeviceUsageStats
103 }  // namespace OHOS
104 
105