Adolfo Victoria | 94ffe13 | 2018-06-28 16:14:56 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2018 The Android Open Source Project |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
| 16 | #ifndef UPDATE_ENGINE_UPDATE_MANAGER_WEEKLY_TIME_H_ |
| 17 | #define UPDATE_ENGINE_UPDATE_MANAGER_WEEKLY_TIME_H_ |
| 18 | |
| 19 | #include <string> |
| 20 | #include <vector> |
| 21 | |
| 22 | #include <base/time/time.h> |
| 23 | |
| 24 | namespace chromeos_update_manager { |
| 25 | |
| 26 | // Represents a day of the week and the time since it started. |
| 27 | class WeeklyTime { |
| 28 | public: |
| 29 | // Day of week (Sunday = 0 and so on) and time since start of the day (12 AM). |
| 30 | WeeklyTime(const int& day_of_week, const base::TimeDelta& time) |
| 31 | : day_of_week_(day_of_week), time_(time) {} |
| 32 | |
| 33 | // Create a weekly time from a time object. |
| 34 | static WeeklyTime FromTime(const base::Time& time); |
| 35 | |
| 36 | bool operator==(const WeeklyTime& other) const { |
| 37 | return time_ == other.time() && day_of_week_ == other.day_of_week(); |
| 38 | } |
| 39 | |
| 40 | bool operator!=(const WeeklyTime& other) const { return !(*this == other); } |
| 41 | |
| 42 | // Return the duration between WeeklyTime and |other|. |other| is always |
| 43 | // considered to be after WeeklyTime. i.e. calling this function on [Friday |
| 44 | // 12:00, Monday 12:00] would return 3 days. |
| 45 | base::TimeDelta GetDurationTo(const WeeklyTime& other) const; |
| 46 | |
| 47 | // Gets the weekly time represented as a time delta. |
| 48 | base::TimeDelta TimeFromStartOfWeek() const; |
| 49 | |
| 50 | // Adds the given |offset| to the time with proper wraparound (e.g. Sunday + 1 |
| 51 | // day = Monday). |
| 52 | void AddTime(const base::TimeDelta& offset); |
| 53 | |
| 54 | int day_of_week() const { return day_of_week_; } |
| 55 | |
| 56 | base::TimeDelta time() const { return time_; } |
| 57 | |
| 58 | private: |
| 59 | int day_of_week_; |
| 60 | base::TimeDelta time_; |
| 61 | }; |
| 62 | |
| 63 | // Represents an interval of time during a week represented with WeeklyTime |
| 64 | // objects. This interval can span at most 7 days. |end| is always considered to |
| 65 | // be after |start|, this is possible since the times of the week are cyclic. |
| 66 | // For example, the interval [Thursday 12:00, Monday 12:00) will span the time |
| 67 | // between Thursday and Monday. |
| 68 | class WeeklyTimeInterval { |
| 69 | public: |
| 70 | WeeklyTimeInterval(const WeeklyTime& start, const WeeklyTime& end) |
| 71 | : start_(start), end_(end) {} |
| 72 | |
| 73 | // Determines if |time| is in this interval. |
| 74 | bool InRange(const WeeklyTime& time) const; |
| 75 | |
| 76 | WeeklyTime start() const { return start_; } |
| 77 | |
| 78 | WeeklyTime end() const { return end_; } |
| 79 | |
| 80 | bool operator==(const WeeklyTimeInterval& other) const { |
| 81 | return start_ == other.start() && end_ == other.end(); |
| 82 | } |
| 83 | |
| 84 | // Converts the interval to a string. Used for the BoxedValue ToString |
| 85 | // function. |
| 86 | std::string ToString() const; |
| 87 | |
| 88 | private: |
| 89 | WeeklyTime start_; |
| 90 | WeeklyTime end_; |
| 91 | }; |
| 92 | |
| 93 | using WeeklyTimeIntervalVector = std::vector<WeeklyTimeInterval>; |
| 94 | |
| 95 | } // namespace chromeos_update_manager |
| 96 | |
| 97 | #endif // UPDATE_ENGINE_UPDATE_MANAGER_WEEKLY_TIME_H_ |