File: weekly_time_checked.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (113 lines) | stat: -rw-r--r-- 3,700 bytes parent folder | download | duplicates (8)
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
// 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/weekly_time_checked.h"

#include <algorithm>
#include <array>
#include <optional>

#include "base/logging.h"
#include "base/time/time.h"
#include "base/values.h"

namespace policy {

// static
const char WeeklyTimeChecked::kDayOfWeek[] = "day_of_week";
const char WeeklyTimeChecked::kMillisecondsSinceMidnight[] =
    "milliseconds_since_midnight";
std::array<const char*, 7> WeeklyTimeChecked::kWeekDays = {
    "MONDAY", "TUESDAY",  "WEDNESDAY", "THURSDAY",
    "FRIDAY", "SATURDAY", "SUNDAY"};

// static
WeeklyTimeChecked::Day WeeklyTimeChecked::NextDay(Day day) {
  int next_value = static_cast<int>(day) % 7 + 1;
  return static_cast<Day>(next_value);
}

WeeklyTimeChecked::WeeklyTimeChecked(Day day_of_week,
                                     int milliseconds_since_midnight)
    : day_of_week_(day_of_week),
      milliseconds_since_midnight_(milliseconds_since_midnight) {}

WeeklyTimeChecked::WeeklyTimeChecked(const WeeklyTimeChecked&) = default;

WeeklyTimeChecked& WeeklyTimeChecked::operator=(const WeeklyTimeChecked&) =
    default;

// static
std::optional<WeeklyTimeChecked> WeeklyTimeChecked::FromDict(
    const base::Value::Dict& dict) {
  auto* day_of_week_str = dict.FindString(kDayOfWeek);
  if (!day_of_week_str) {
    LOG(ERROR) << "Missing day_of_week.";
    return std::nullopt;
  }

  int day_of_week_value =
      std::ranges::find(kWeekDays, *day_of_week_str) - kWeekDays.begin() + 1;
  if (day_of_week_value < 1 || day_of_week_value > 7) {
    LOG(ERROR) << "Invalid day_of_week: " << *day_of_week_str;
    return std::nullopt;
  }
  Day day_of_week = static_cast<Day>(day_of_week_value);

  auto milliseconds = dict.FindInt(kMillisecondsSinceMidnight);
  if (!milliseconds.has_value()) {
    LOG(ERROR) << "Missing milliseconds_since_midnight.";
    return std::nullopt;
  }

  if (milliseconds.value() < 0 ||
      milliseconds.value() >= base::Time::kMillisecondsPerDay) {
    LOG(ERROR) << "Invalid milliseconds_since_midnight value: "
               << milliseconds.value() << ", the value should be in range [0, "
               << base::Time::kMillisecondsPerDay << ").";
    return std::nullopt;
  }

  return WeeklyTimeChecked(day_of_week, milliseconds.value());
}

// static
WeeklyTimeChecked WeeklyTimeChecked::FromExploded(
    const base::Time::Exploded& exploded) {
  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 WeeklyTimeChecked(static_cast<Day>(day_of_week), milliseconds);
}

// static
WeeklyTimeChecked WeeklyTimeChecked::FromTimeAsLocalTime(base::Time time) {
  base::Time::Exploded exploded;
  time.LocalExplode(&exploded);
  return FromExploded(exploded);
}

// static
WeeklyTimeChecked WeeklyTimeChecked::FromTimeDelta(base::TimeDelta time_delta) {
  time_delta %= base::Days(7);
  if (time_delta.is_negative()) {
    time_delta += base::Days(7);
  }

  int day = time_delta.InDays();
  time_delta -= base::Days(day);
  int millis = time_delta.InMilliseconds();

  // Add one to day since Monday starts at 1, not at 0.
  return WeeklyTimeChecked(static_cast<Day>(day + 1), millis);
}

base::TimeDelta WeeklyTimeChecked::ToTimeDelta() const {
  return base::Days(static_cast<int>(day_of_week_) - 1) +
         base::Milliseconds(milliseconds_since_midnight_);
}

}  // namespace policy