File: weekly_time.h

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 (121 lines) | stat: -rw-r--r-- 4,617 bytes parent folder | download | duplicates (7)
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
// 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.

#ifndef CHROMEOS_ASH_COMPONENTS_POLICY_WEEKLY_TIME_WEEKLY_TIME_H_
#define CHROMEOS_ASH_COMPONENTS_POLICY_WEEKLY_TIME_WEEKLY_TIME_H_

#include <memory>
#include <optional>

#include "base/component_export.h"
#include "base/time/time.h"
#include "base/values.h"
#include "components/policy/proto/chrome_device_policy.pb.h"

namespace policy {

// WeeklyTime class contains day of week and time. Day of week is number from 1
// to 7 (1 = Monday, 2 = Tuesday, etc.) Time is in milliseconds from the
// beginning of the day.
class COMPONENT_EXPORT(CHROMEOS_ASH_COMPONENTS_POLICY) WeeklyTime {
 public:
  // Dictionary value key constants for testing.
  static const char kDayOfWeek[];
  static const char kTime[];
  static const char kTimezoneOffset[];
  // Dictionary value constants for testing.
  static const std::vector<std::string> kWeekDays;

  WeeklyTime(int day_of_week,
             int milliseconds,
             std::optional<int> timezone_offset);

  WeeklyTime(const WeeklyTime& rhs);

  WeeklyTime& operator=(const WeeklyTime& rhs);

  bool operator==(const WeeklyTime& rhs) const {
    return day_of_week_ == rhs.day_of_week() &&
           milliseconds_ == rhs.milliseconds() &&
           timezone_offset_ == rhs.timezone_offset();
  }

  bool operator!=(const WeeklyTime& rhs) const { return !operator==(rhs); }

  // Return Dictionary type Value in format:
  // { "day_of_week" : int # value is from 1 to 7 (1 = Monday, 2 = Tuesday,
  // etc.)
  //   "time" : int # in milliseconds from the beginning of the day.
  // }
  base::Value ToValue() const;

  int day_of_week() const { return day_of_week_; }

  int milliseconds() const { return milliseconds_; }

  std::optional<int> timezone_offset() const { return timezone_offset_; }

  // Return duration from |start| till |end| week times. |end| time
  // is always after |start| time. It's possible because week time is cyclic.
  // (i.e. [Friday 17:00, Monday 9:00) )
  // Both WeeklyTimes have to have the same kind of timezone (timezone agnostic
  // or set timezone).
  base::TimeDelta GetDurationTo(const WeeklyTime& other) const;

  // Add milliseconds to WeeklyTime.
  WeeklyTime AddMilliseconds(int milliseconds) const;

  // Creates a new WeeklyTime from the current one that has
  // |timezone_offset_| set to |timezone_offset|. Furthermore, the new
  // WeeklyTime is offset based on the difference between the timezones. This
  // function should only take in WeeklyTimes with a defined timezone (i.e. not
  // nullopt).
  WeeklyTime ConvertToTimezone(int timezone_offset) const;

  // Return WeeklyTime structure from WeeklyTimeProto. Return nullptr if
  // WeeklyTime structure isn't correct.
  static std::unique_ptr<WeeklyTime> ExtractFromProto(
      const enterprise_management::WeeklyTimeProto& container,
      std::optional<int> timezone_offset);

  // Return WeeklyTime structure from Value::Dict in format:
  // { "day_of_week" : int # value is from 1 to 7 (1 = Monday, 2 = Tuesday,
  // etc.)
  //   "time" : int # in milliseconds from the beginning of the day.
  // }.
  // Return nullptr if WeeklyTime structure isn't correct.
  static std::unique_ptr<WeeklyTime> ExtractFromDict(
      const base::Value::Dict& dict,
      std::optional<int> timezone_offset);

  // Return the |time| in GMT in WeeklyTime structure.
  static WeeklyTime GetGmtWeeklyTime(base::Time time);

  // Return the system's local |time| in WeeklyTime structure.
  static WeeklyTime GetLocalWeeklyTime(base::Time time);

 private:
  // Number of weekday (1 = Monday, 2 = Tuesday, etc.)
  int day_of_week_;

  // Time of day in milliseconds from the beginning of the day.
  int milliseconds_;

  // Offset from GMT in milliseconds for the timezone that the Weekly Time is
  // in. If |timezone_offset_| is 0 then it the WeeklyTime will be considered to
  // be in GMT. If |timezone_offset_| is non-zero, then the WeeklyTime will be
  // considered to be in the timezone corresponding to that offset. If
  // |timezone_offset_| is |nullopt|, then it will be interpreted to be in the
  // system's local timezone.
  std::optional<int> timezone_offset_;
};

// Constructs a WeeklyTime from an exploded base::Time.
COMPONENT_EXPORT(CHROMEOS_ASH_COMPONENTS_POLICY)
WeeklyTime GetWeeklyTimeFromExploded(const base::Time::Exploded& exploded,
                                     const std::optional<int> timezone_offset);

}  // namespace policy

#endif  // CHROMEOS_ASH_COMPONENTS_POLICY_WEEKLY_TIME_WEEKLY_TIME_H_