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 "bundle_active_util.h"
18 
19 namespace OHOS {
20 namespace DeviceUsageStats {
21 const int32_t MILLISECOND_TO_MICROSECOND = 1000;
22 const int32_t MILLISECOND_TO_SECOND = 1000;
23 const int32_t SECOND_TO_MILLISECOND = 1000;
24 const int64_t ONE_DAY_TIME = 24 * 60 * 60 *1000LL;
25 const int64_t WEEK_OFFSET = 6LL;
26 const int64_t DAYS_OF_WEEK = 7LL;
27 const int64_t HOUR_OF_MIDNIGHT = 0;
28 const int64_t MIN_OF_MIDNIGHT = 0;
29 const int64_t SECOND_TO_MIDNIGHT = 0;
30 const int64_t STATR_DAY_OF_MON = 1;
31 const int64_t STATR_MON_OF_YEAR = 0;
32 const int64_t ERROR_TIME = 0;
GetBundleUsageKey(const std::string &bundleName, const int32_t uid)33 std::string BundleActiveUtil::GetBundleUsageKey(const std::string &bundleName, const int32_t uid)
34 {
35     return bundleName + std::to_string(uid);
36 }
37 
GetFFRTDelayTime(const int64_t& delayTime)38 int64_t BundleActiveUtil::GetFFRTDelayTime(const int64_t& delayTime)
39 {
40     return delayTime * MILLISECOND_TO_MICROSECOND;
41 }
42 
GetIntervalTypeStartTime(const int64_t& timeStamp, const int32_t& intervalType)43 int64_t BundleActiveUtil::GetIntervalTypeStartTime(const int64_t& timeStamp, const int32_t& intervalType)
44 {
45     if (timeStamp <= 0) {
46         return ERROR_TIME;
47     }
48     time_t time = timeStamp / MILLISECOND_TO_SECOND;
49     std::tm* tm_time = std::localtime(&time);
50     if (tm_time == nullptr) {
51         return ERROR_TIME;
52     }
53     tm_time->tm_hour = HOUR_OF_MIDNIGHT;
54     tm_time->tm_min = MIN_OF_MIDNIGHT;
55     tm_time->tm_sec = SECOND_TO_MIDNIGHT;
56     if (intervalType == PERIOD_WEEKLY) {
57         int64_t weekday = tm_time->tm_wday;
58         time_t startOfDay = mktime(tm_time) * SECOND_TO_MILLISECOND;
59         time_t weekDayTime = (weekday + WEEK_OFFSET) % DAYS_OF_WEEK * ONE_DAY_TIME;
60         return startOfDay - weekDayTime;
61     }
62     switch (intervalType) {
63         case PERIOD_DAILY:
64             break;
65         case PERIOD_MONTHLY:
66             tm_time->tm_mday = STATR_DAY_OF_MON;
67             break;
68         case PERIOD_YEARLY:
69             tm_time->tm_mon = STATR_MON_OF_YEAR;
70             tm_time->tm_mday = STATR_DAY_OF_MON;
71             break;
72         default:
73             break;
74     }
75     return mktime(tm_time) * SECOND_TO_MILLISECOND;
76 }
77 } // namespace DeviceUsageStats
78 }  // namespace OHOS
79 
80