Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "ash/system/night_light/time_of_day.h" | |
| 6 | |
| 7 #include "base/i18n/time_formatting.h" | |
| 8 #include "base/strings/utf_string_conversions.h" | |
| 9 | |
| 10 namespace ash { | |
| 11 | |
| 12 namespace { | |
| 13 | |
| 14 constexpr int kMaxOffsetMinutes = 24 * 60; | |
| 15 | |
| 16 } // namespace | |
| 17 | |
| 18 TimeOfDay::TimeOfDay(int offset_minutes) | |
| 19 : offset_minutes_from_zero_hour_( | |
| 20 offset_minutes == kMaxOffsetMinutes ? 0 : offset_minutes) { | |
| 21 DCHECK_LE(offset_minutes_from_zero_hour_, kMaxOffsetMinutes); | |
| 22 } | |
| 23 | |
| 24 // static | |
| 25 TimeOfDay TimeOfDay::FromTime(const base::Time& time) { | |
| 26 base::Time::Exploded exploded; | |
| 27 time.LocalExplode(&exploded); | |
| 28 return TimeOfDay(exploded.hour * 60 + exploded.minute); | |
| 29 } | |
| 30 | |
| 31 bool TimeOfDay::operator==(const TimeOfDay& rhs) const { | |
| 32 return offset_minutes_from_zero_hour_ == rhs.offset_minutes_from_zero_hour_; | |
| 33 } | |
| 34 | |
| 35 base::Time TimeOfDay::ToTimeToday() const { | |
| 36 base::Time::Exploded now; | |
| 37 base::Time::Now().LocalExplode(&now); | |
| 38 now.hour = (offset_minutes_from_zero_hour_ / 60) % 24; | |
| 39 now.minute = offset_minutes_from_zero_hour_ % 60; | |
| 40 now.second = 0; | |
| 41 now.millisecond = 0; | |
| 42 base::Time result; | |
| 43 if (base::Time::FromLocalExploded(now, &result)) | |
| 44 return result; | |
| 45 | |
| 46 // This function must not fail. | |
| 47 NOTREACHED(); | |
|
James Cook
2017/05/24 15:42:46
super-nit: I still think FromLocalExploded() can f
afakhry
2017/05/24 16:25:35
Done.
| |
| 48 return base::Time(); | |
| 49 } | |
| 50 | |
| 51 std::string TimeOfDay::ToString() const { | |
| 52 return base::UTF16ToUTF8(base::TimeFormatTimeOfDay(ToTimeToday())); | |
| 53 } | |
| 54 | |
| 55 } // namespace ash | |
| OLD | NEW |