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
|
// Copyright 2024 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/checked_util.h"
#include <algorithm>
#include <optional>
#include <vector>
#include "base/logging.h"
#include "base/time/time.h"
#include "base/values.h"
#include "chromeos/ash/components/policy/weekly_time/weekly_time_interval_checked.h"
namespace policy::weekly_time {
namespace {
std::optional<base::Time> UTCToLocal(base::Time utc) {
base::Time::Exploded utc_exploded_local;
utc.LocalExplode(&utc_exploded_local);
if (!utc_exploded_local.HasValidValues()) {
return std::nullopt;
}
base::Time utc_exploded_local_unexploded_utc;
if (!base::Time::FromUTCExploded(utc_exploded_local,
&utc_exploded_local_unexploded_utc)) {
return std::nullopt;
}
return utc_exploded_local_unexploded_utc;
}
std::optional<base::Time> LocalToUTC(base::Time local) {
base::Time::Exploded local_exploded_utc;
local.UTCExplode(&local_exploded_utc);
if (!local_exploded_utc.HasValidValues()) {
return std::nullopt;
}
base::Time local_exploded_utc_unexploded_local;
if (!base::Time::FromLocalExploded(local_exploded_utc,
&local_exploded_utc_unexploded_local)) {
return std::nullopt;
}
return local_exploded_utc_unexploded_local;
}
} // namespace
std::optional<std::vector<WeeklyTimeIntervalChecked>> ExtractIntervalsFromList(
const base::Value::List& list) {
std::vector<WeeklyTimeIntervalChecked> intervals;
for (const auto& interval_value : list) {
if (!interval_value.is_dict()) {
LOG(ERROR) << "Interval not a dict: " << interval_value.DebugString();
return std::nullopt;
}
const auto& interval_dict = interval_value.GetDict();
auto interval = WeeklyTimeIntervalChecked::FromDict(interval_dict);
if (!interval.has_value()) {
LOG(ERROR) << "Couldn't parse interval: " << interval_dict.DebugString();
return std::nullopt;
}
intervals.push_back(interval.value());
}
return intervals;
}
bool IntervalsContainTime(
const std::vector<WeeklyTimeIntervalChecked>& intervals,
const WeeklyTimeChecked& time) {
for (const auto& interval : intervals) {
if (interval.Contains(time)) {
return true;
}
}
return false;
}
std::optional<WeeklyTimeChecked> GetNextEvent(
const std::vector<WeeklyTimeIntervalChecked>& intervals,
const WeeklyTimeChecked& time) {
std::optional<WeeklyTimeChecked> next_event = std::nullopt;
// Sentinel value set to max which is 1 week.
base::TimeDelta min_duration = base::Days(7);
for (const auto& interval : intervals) {
for (const WeeklyTimeChecked& event : {interval.start(), interval.end()}) {
const base::TimeDelta duration =
WeeklyTimeIntervalChecked(time, event).Duration();
if (duration < min_duration) {
min_duration = duration;
next_event = event;
}
}
}
return next_event;
}
std::optional<base::TimeDelta> GetDurationToNextEvent(
const std::vector<WeeklyTimeIntervalChecked>& intervals,
const WeeklyTimeChecked& time) {
std::optional<WeeklyTimeChecked> next_event = GetNextEvent(intervals, time);
if (!next_event.has_value()) {
return std::nullopt;
}
return WeeklyTimeIntervalChecked(time, next_event.value()).Duration();
}
base::Time AddOffsetInLocalTime(base::Time utc, base::TimeDelta offset) {
std::optional<base::Time> local = UTCToLocal(utc);
if (!local.has_value()) {
return utc + offset;
}
base::Time local_with_offset = local.value() + offset;
std::optional<base::Time> utc_with_offset = LocalToUTC(local_with_offset);
if (!utc_with_offset.has_value()) {
return utc + offset;
}
return utc_with_offset.value();
}
} // namespace policy::weekly_time
|