1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/ash/components/policy/weekly_time/weekly_time.h"
#include <algorithm>
#include "base/logging.h"
#include "base/time/time.h"
namespace em = enterprise_management;
namespace policy {
namespace {
constexpr int64_t kMillisecondsPerWeek = base::Days(7).InMilliseconds();
} // namespace
// static
const char WeeklyTime::kDayOfWeek[] = "day_of_week";
const char WeeklyTime::kTime[] = "time";
const char WeeklyTime::kTimezoneOffset[] = "timezone_offset";
const std::vector<std::string> WeeklyTime::kWeekDays = {
"UNSPECIFIED", "MONDAY", "TUESDAY", "WEDNESDAY",
"THURSDAY", "FRIDAY", "SATURDAY", "SUNDAY"};
WeeklyTime::WeeklyTime(int day_of_week,
int milliseconds,
std::optional<int> timezone_offset)
: day_of_week_(day_of_week),
milliseconds_(milliseconds),
timezone_offset_(timezone_offset) {
DCHECK_GT(day_of_week, 0);
DCHECK_LE(day_of_week, 7);
DCHECK_GE(milliseconds, 0);
DCHECK_LT(milliseconds, base::Time::kMillisecondsPerDay);
}
WeeklyTime::WeeklyTime(const WeeklyTime& rhs) = default;
WeeklyTime& WeeklyTime::operator=(const WeeklyTime& rhs) = default;
base::Value WeeklyTime::ToValue() const {
base::Value weekly_time(base::Value::Type::DICT);
weekly_time.GetDict().Set(kDayOfWeek, day_of_week_);
weekly_time.GetDict().Set(kTime, milliseconds_);
if (timezone_offset_)
weekly_time.GetDict().Set(kTimezoneOffset, timezone_offset_.value());
return weekly_time;
}
base::TimeDelta WeeklyTime::GetDurationTo(const WeeklyTime& other) const {
// Can't compare timezone agnostic intervals and non-timezone agnostic
// intervals.
DCHECK_EQ(timezone_offset_.has_value(), other.timezone_offset().has_value());
WeeklyTime other_converted =
timezone_offset_ ? other.ConvertToTimezone(timezone_offset_.value())
: other;
int duration = base::Days(other_converted.day_of_week() - day_of_week_)
.InMilliseconds() +
other_converted.milliseconds() - milliseconds_;
if (duration < 0) {
duration += kMillisecondsPerWeek;
}
return base::Milliseconds(duration);
}
WeeklyTime WeeklyTime::AddMilliseconds(int milliseconds) const {
milliseconds %= kMillisecondsPerWeek;
// Make |milliseconds| positive number (add number of milliseconds per week)
// for easier evaluation.
milliseconds += kMillisecondsPerWeek;
int shifted_milliseconds = milliseconds_ + milliseconds;
// Get milliseconds from the start of the day.
int result_milliseconds =
shifted_milliseconds % base::Time::kMillisecondsPerDay;
int day_offset = shifted_milliseconds / base::Time::kMillisecondsPerDay;
// Convert day of week considering week is cyclic. +/- 1 is
// because day of week is from 1 to 7.
int result_day_of_week = (day_of_week_ + day_offset - 1) % 7 + 1;
// AddMilliseconds should preserve the timezone.
return WeeklyTime(result_day_of_week, result_milliseconds, timezone_offset_);
}
WeeklyTime WeeklyTime::ConvertToTimezone(int timezone_offset) const {
DCHECK(timezone_offset_);
return WeeklyTime(day_of_week_, milliseconds_, timezone_offset)
.AddMilliseconds(timezone_offset - timezone_offset_.value());
}
// static
WeeklyTime WeeklyTime::GetGmtWeeklyTime(base::Time time) {
base::Time::Exploded exploded;
time.UTCExplode(&exploded);
return GetWeeklyTimeFromExploded(exploded, 0);
}
// static
WeeklyTime WeeklyTime::GetLocalWeeklyTime(base::Time time) {
base::Time::Exploded exploded;
time.LocalExplode(&exploded);
WeeklyTime result = GetWeeklyTimeFromExploded(exploded, std::nullopt);
return result;
}
// static
std::unique_ptr<WeeklyTime> WeeklyTime::ExtractFromProto(
const em::WeeklyTimeProto& container,
std::optional<int> timezone_offset) {
if (!container.has_day_of_week() ||
container.day_of_week() == em::WeeklyTimeProto::DAY_OF_WEEK_UNSPECIFIED) {
LOG(ERROR) << "Day of week is absent or unspecified.";
return nullptr;
}
if (!container.has_time()) {
LOG(ERROR) << "Time is absent.";
return nullptr;
}
int time_of_day = container.time();
if (!(time_of_day >= 0 && time_of_day < base::Time::kMillisecondsPerDay)) {
LOG(ERROR) << "Invalid time value: " << time_of_day
<< ", the value should be in [0; "
<< base::Time::kMillisecondsPerDay << ").";
return nullptr;
}
return std::make_unique<WeeklyTime>(container.day_of_week(), time_of_day,
timezone_offset);
}
// static
std::unique_ptr<WeeklyTime> WeeklyTime::ExtractFromDict(
const base::Value::Dict& dict,
std::optional<int> timezone_offset) {
auto* day_of_week = dict.FindString(kDayOfWeek);
if (!day_of_week) {
LOG(ERROR) << "day_of_week is absent.";
return nullptr;
}
int day_of_week_value =
std::ranges::find(kWeekDays, *day_of_week) - kWeekDays.begin();
if (day_of_week_value <= 0 || day_of_week_value > 7) {
LOG(ERROR) << "Invalid day_of_week: " << day_of_week;
return nullptr;
}
auto time_of_day = dict.FindInt(kTime);
if (!time_of_day.has_value()) {
LOG(ERROR) << "Time is absent";
return nullptr;
}
if (!(time_of_day.value() >= 0 &&
time_of_day.value() < base::Time::kMillisecondsPerDay)) {
LOG(ERROR) << "Invalid time value: " << time_of_day.value()
<< ", the value should be in [0; "
<< base::Time::kMillisecondsPerDay << ").";
return nullptr;
}
return std::make_unique<WeeklyTime>(day_of_week_value, time_of_day.value(),
timezone_offset);
}
WeeklyTime GetWeeklyTimeFromExploded(const base::Time::Exploded& exploded,
const std::optional<int> timezone_offset) {
int day_of_week = exploded.day_of_week == 0 ? 7 : exploded.day_of_week;
int milliseconds = static_cast<int>(
base::Hours(exploded.hour).InMilliseconds() +
base::Minutes(exploded.minute).InMilliseconds() +
base::Seconds(exploded.second).InMilliseconds() + exploded.millisecond);
return WeeklyTime(day_of_week, milliseconds, timezone_offset);
}
} // namespace policy
|