122c0b7e4Sopenharmony_ci/*
222c0b7e4Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
322c0b7e4Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
422c0b7e4Sopenharmony_ci * you may not use this file except in compliance with the License.
522c0b7e4Sopenharmony_ci * You may obtain a copy of the License at
622c0b7e4Sopenharmony_ci *
722c0b7e4Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
822c0b7e4Sopenharmony_ci *
922c0b7e4Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1022c0b7e4Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1122c0b7e4Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1222c0b7e4Sopenharmony_ci * See the License for the specific language governing permissions and
1322c0b7e4Sopenharmony_ci * limitations under the License.
1422c0b7e4Sopenharmony_ci */
1522c0b7e4Sopenharmony_ci
1622c0b7e4Sopenharmony_ci#include <ctime>
1722c0b7e4Sopenharmony_ci#include "bundle_active_util.h"
1822c0b7e4Sopenharmony_ci
1922c0b7e4Sopenharmony_cinamespace OHOS {
2022c0b7e4Sopenharmony_cinamespace DeviceUsageStats {
2122c0b7e4Sopenharmony_ciconst int32_t MILLISECOND_TO_MICROSECOND = 1000;
2222c0b7e4Sopenharmony_ciconst int32_t MILLISECOND_TO_SECOND = 1000;
2322c0b7e4Sopenharmony_ciconst int32_t SECOND_TO_MILLISECOND = 1000;
2422c0b7e4Sopenharmony_ciconst int64_t ONE_DAY_TIME = 24 * 60 * 60 *1000LL;
2522c0b7e4Sopenharmony_ciconst int64_t WEEK_OFFSET = 6LL;
2622c0b7e4Sopenharmony_ciconst int64_t DAYS_OF_WEEK = 7LL;
2722c0b7e4Sopenharmony_ciconst int64_t HOUR_OF_MIDNIGHT = 0;
2822c0b7e4Sopenharmony_ciconst int64_t MIN_OF_MIDNIGHT = 0;
2922c0b7e4Sopenharmony_ciconst int64_t SECOND_TO_MIDNIGHT = 0;
3022c0b7e4Sopenharmony_ciconst int64_t STATR_DAY_OF_MON = 1;
3122c0b7e4Sopenharmony_ciconst int64_t STATR_MON_OF_YEAR = 0;
3222c0b7e4Sopenharmony_ciconst int64_t ERROR_TIME = 0;
3322c0b7e4Sopenharmony_cistd::string BundleActiveUtil::GetBundleUsageKey(const std::string &bundleName, const int32_t uid)
3422c0b7e4Sopenharmony_ci{
3522c0b7e4Sopenharmony_ci    return bundleName + std::to_string(uid);
3622c0b7e4Sopenharmony_ci}
3722c0b7e4Sopenharmony_ci
3822c0b7e4Sopenharmony_ciint64_t BundleActiveUtil::GetFFRTDelayTime(const int64_t& delayTime)
3922c0b7e4Sopenharmony_ci{
4022c0b7e4Sopenharmony_ci    return delayTime * MILLISECOND_TO_MICROSECOND;
4122c0b7e4Sopenharmony_ci}
4222c0b7e4Sopenharmony_ci
4322c0b7e4Sopenharmony_ciint64_t BundleActiveUtil::GetIntervalTypeStartTime(const int64_t& timeStamp, const int32_t& intervalType)
4422c0b7e4Sopenharmony_ci{
4522c0b7e4Sopenharmony_ci    if (timeStamp <= 0) {
4622c0b7e4Sopenharmony_ci        return ERROR_TIME;
4722c0b7e4Sopenharmony_ci    }
4822c0b7e4Sopenharmony_ci    time_t time = timeStamp / MILLISECOND_TO_SECOND;
4922c0b7e4Sopenharmony_ci    std::tm* tm_time = std::localtime(&time);
5022c0b7e4Sopenharmony_ci    if (tm_time == nullptr) {
5122c0b7e4Sopenharmony_ci        return ERROR_TIME;
5222c0b7e4Sopenharmony_ci    }
5322c0b7e4Sopenharmony_ci    tm_time->tm_hour = HOUR_OF_MIDNIGHT;
5422c0b7e4Sopenharmony_ci    tm_time->tm_min = MIN_OF_MIDNIGHT;
5522c0b7e4Sopenharmony_ci    tm_time->tm_sec = SECOND_TO_MIDNIGHT;
5622c0b7e4Sopenharmony_ci    if (intervalType == PERIOD_WEEKLY) {
5722c0b7e4Sopenharmony_ci        int64_t weekday = tm_time->tm_wday;
5822c0b7e4Sopenharmony_ci        time_t startOfDay = mktime(tm_time) * SECOND_TO_MILLISECOND;
5922c0b7e4Sopenharmony_ci        time_t weekDayTime = (weekday + WEEK_OFFSET) % DAYS_OF_WEEK * ONE_DAY_TIME;
6022c0b7e4Sopenharmony_ci        return startOfDay - weekDayTime;
6122c0b7e4Sopenharmony_ci    }
6222c0b7e4Sopenharmony_ci    switch (intervalType) {
6322c0b7e4Sopenharmony_ci        case PERIOD_DAILY:
6422c0b7e4Sopenharmony_ci            break;
6522c0b7e4Sopenharmony_ci        case PERIOD_MONTHLY:
6622c0b7e4Sopenharmony_ci            tm_time->tm_mday = STATR_DAY_OF_MON;
6722c0b7e4Sopenharmony_ci            break;
6822c0b7e4Sopenharmony_ci        case PERIOD_YEARLY:
6922c0b7e4Sopenharmony_ci            tm_time->tm_mon = STATR_MON_OF_YEAR;
7022c0b7e4Sopenharmony_ci            tm_time->tm_mday = STATR_DAY_OF_MON;
7122c0b7e4Sopenharmony_ci            break;
7222c0b7e4Sopenharmony_ci        default:
7322c0b7e4Sopenharmony_ci            break;
7422c0b7e4Sopenharmony_ci    }
7522c0b7e4Sopenharmony_ci    return mktime(tm_time) * SECOND_TO_MILLISECOND;
7622c0b7e4Sopenharmony_ci}
7722c0b7e4Sopenharmony_ci} // namespace DeviceUsageStats
7822c0b7e4Sopenharmony_ci}  // namespace OHOS
7922c0b7e4Sopenharmony_ci
80