1 /* 2 * Copyright (c) 2022 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 "bundle_active_calendar.h" 17 #include "bundle_active_period_stats.h" 18 #include "bundle_active_util.h" 19 20 namespace OHOS { 21 namespace DeviceUsageStats { BundleActiveCalendar(const int64_t timeStamp)22BundleActiveCalendar::BundleActiveCalendar(const int64_t timeStamp) 23 { 24 time_ = timeStamp; 25 dayMilliseconds_ = ONE_DAY_TIME; 26 weekMilliseconds_ = ONE_WEEK_TIME; 27 monthMilliseconds_ = ONE_MONTH_TIME; 28 yearMilliseconds_ = ONE_YEAR_TIME; 29 } 30 TruncateToDay()31void BundleActiveCalendar::TruncateToDay() 32 { 33 if (debug_) { 34 time_ -= time_ % dayMilliseconds_; 35 return; 36 } 37 int64_t dayStartTime = BundleActiveUtil::GetIntervalTypeStartTime(time_, BundleActiveUtil::PERIOD_DAILY); 38 if (dayStartTime == 0) { 39 return; 40 } 41 time_ = dayStartTime; 42 } 43 TruncateToWeek()44void BundleActiveCalendar::TruncateToWeek() 45 { 46 if (debug_) { 47 time_ -= time_ % weekMilliseconds_; 48 return; 49 } 50 int64_t weekStartTime = BundleActiveUtil::GetIntervalTypeStartTime(time_, BundleActiveUtil::PERIOD_WEEKLY); 51 if (weekStartTime == 0) { 52 return; 53 } 54 time_ = weekStartTime; 55 } 56 TruncateToMonth()57void BundleActiveCalendar::TruncateToMonth() 58 { 59 if (debug_) { 60 time_ -= time_ % monthMilliseconds_; 61 return; 62 } 63 int64_t monthStartTime = BundleActiveUtil::GetIntervalTypeStartTime(time_, BundleActiveUtil::PERIOD_MONTHLY); 64 if (monthStartTime == 0) { 65 return; 66 } 67 time_ = monthStartTime; 68 } 69 TruncateToYear()70void BundleActiveCalendar::TruncateToYear() 71 { 72 if (debug_) { 73 time_ -= time_ % yearMilliseconds_; 74 return; 75 } 76 int64_t yearStartTime = BundleActiveUtil::GetIntervalTypeStartTime(time_, BundleActiveUtil::PERIOD_YEARLY); 77 if (yearStartTime == 0) { 78 return; 79 } 80 time_ = yearStartTime; 81 } 82 IncreaseDays(const int64_t val)83void BundleActiveCalendar::IncreaseDays(const int64_t val) 84 { 85 time_ += val * dayMilliseconds_; 86 } 87 IncreaseWeeks(const int64_t val)88void BundleActiveCalendar::IncreaseWeeks(const int64_t val) 89 { 90 time_ += val* weekMilliseconds_; 91 } 92 IncreaseMonths(const int64_t val)93void BundleActiveCalendar::IncreaseMonths(const int64_t val) 94 { 95 time_ += val * monthMilliseconds_; 96 } 97 IncreaseYears(const int64_t val)98void BundleActiveCalendar::IncreaseYears(const int64_t val) 99 { 100 time_ += val * yearMilliseconds_; 101 } 102 SetMilliseconds(const int64_t timeStamp)103void BundleActiveCalendar::SetMilliseconds(const int64_t timeStamp) 104 { 105 time_ = timeStamp; 106 } 107 GetMilliseconds()108int64_t BundleActiveCalendar::GetMilliseconds() 109 { 110 return time_; 111 } 112 ChangeToDebug()113void BundleActiveCalendar::ChangeToDebug() 114 { 115 debug_ = true; 116 dayMilliseconds_ = ONE_DAY_TIME_DEBUG; 117 weekMilliseconds_ = ONE_WEEK_TIME_DEBUG; 118 monthMilliseconds_ = ONE_MONTH_TIME_DEBUG; 119 yearMilliseconds_ = ONE_YEAR_TIME_DEBUG; 120 } 121 TruncateTo(int32_t intervalType)122void BundleActiveCalendar::TruncateTo(int32_t intervalType) 123 { 124 switch (intervalType) { 125 case BundleActivePeriodStats::PERIOD_DAILY: 126 TruncateToDay(); 127 break; 128 case BundleActivePeriodStats::PERIOD_WEEKLY: 129 TruncateToWeek(); 130 break; 131 case BundleActivePeriodStats::PERIOD_MONTHLY: 132 TruncateToMonth(); 133 break; 134 case BundleActivePeriodStats::PERIOD_YEARLY: 135 TruncateToYear(); 136 break; 137 default: 138 break; 139 } 140 } 141 } // namespace DeviceUsageStats 142 } // namespace OHOS 143