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 19namespace OHOS { 20namespace DeviceUsageStats { 21const int32_t MILLISECOND_TO_MICROSECOND = 1000; 22const int32_t MILLISECOND_TO_SECOND = 1000; 23const int32_t SECOND_TO_MILLISECOND = 1000; 24const int64_t ONE_DAY_TIME = 24 * 60 * 60 *1000LL; 25const int64_t WEEK_OFFSET = 6LL; 26const int64_t DAYS_OF_WEEK = 7LL; 27const int64_t HOUR_OF_MIDNIGHT = 0; 28const int64_t MIN_OF_MIDNIGHT = 0; 29const int64_t SECOND_TO_MIDNIGHT = 0; 30const int64_t STATR_DAY_OF_MON = 1; 31const int64_t STATR_MON_OF_YEAR = 0; 32const int64_t ERROR_TIME = 0; 33std::string BundleActiveUtil::GetBundleUsageKey(const std::string &bundleName, const int32_t uid) 34{ 35 return bundleName + std::to_string(uid); 36} 37 38int64_t BundleActiveUtil::GetFFRTDelayTime(const int64_t& delayTime) 39{ 40 return delayTime * MILLISECOND_TO_MICROSECOND; 41} 42 43int64_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