| 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 | #include "update_engine/update_manager/weekly_time.h" | 
|  | 17 |  | 
|  | 18 | #include <base/strings/string_number_conversions.h> | 
|  | 19 | #include <base/strings/stringprintf.h> | 
|  | 20 | #include <base/time/time.h> | 
|  | 21 |  | 
|  | 22 | using base::Time; | 
|  | 23 | using base::TimeDelta; | 
|  | 24 | using std::string; | 
|  | 25 |  | 
|  | 26 | namespace { | 
|  | 27 | const int kDaysInWeek = 7; | 
|  | 28 | } | 
|  | 29 |  | 
|  | 30 | namespace chromeos_update_manager { | 
|  | 31 |  | 
|  | 32 | TimeDelta WeeklyTime::GetDurationTo(const WeeklyTime& other) const { | 
|  | 33 | if (other.TimeFromStartOfWeek() < TimeFromStartOfWeek()) { | 
|  | 34 | return other.TimeFromStartOfWeek() + | 
|  | 35 | (TimeDelta::FromDays(kDaysInWeek) - TimeFromStartOfWeek()); | 
|  | 36 | } | 
|  | 37 | return other.TimeFromStartOfWeek() - TimeFromStartOfWeek(); | 
|  | 38 | } | 
|  | 39 |  | 
|  | 40 | TimeDelta WeeklyTime::TimeFromStartOfWeek() const { | 
|  | 41 | return TimeDelta::FromDays(day_of_week_) + time_; | 
|  | 42 | } | 
|  | 43 |  | 
|  | 44 | void WeeklyTime::AddTime(const TimeDelta& offset) { | 
|  | 45 | time_ += offset; | 
|  | 46 | int days_over = time_.InDays(); | 
|  | 47 | time_ -= TimeDelta::FromDays(days_over); | 
|  | 48 | day_of_week_ = (day_of_week_ + days_over - 1) % kDaysInWeek + 1; | 
|  | 49 | } | 
|  | 50 |  | 
|  | 51 | // static | 
|  | 52 | WeeklyTime WeeklyTime::FromTime(const Time& time) { | 
|  | 53 | Time::Exploded exploded; | 
|  | 54 | time.LocalExplode(&exploded); | 
|  | 55 | return WeeklyTime(exploded.day_of_week, | 
|  | 56 | TimeDelta::FromHours(exploded.hour) + | 
|  | 57 | TimeDelta::FromMinutes(exploded.minute)); | 
|  | 58 | } | 
|  | 59 |  | 
|  | 60 | bool WeeklyTimeInterval::InRange(const WeeklyTime& time) const { | 
|  | 61 | return time == start_ || | 
|  | 62 | (time.GetDurationTo(start_) >= time.GetDurationTo(end_) && | 
|  | 63 | time != end_); | 
|  | 64 | } | 
|  | 65 |  | 
|  | 66 | string WeeklyTimeInterval::ToString() const { | 
|  | 67 | return base::StringPrintf( | 
|  | 68 | "Start: day_of_week=%d time=%d\nEnd: day_of_week=%d time=%d", | 
|  | 69 | start_.day_of_week(), | 
|  | 70 | start_.time().InMinutes(), | 
|  | 71 | end_.day_of_week(), | 
|  | 72 | end_.time().InMinutes()); | 
|  | 73 | } | 
|  | 74 |  | 
|  | 75 | }  // namespace chromeos_update_manager |