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 // Daylight saving time can cause FromLocalExploded() to fail on the |
| 47 // transition day in the spring when TimeOfDay == 2:30 AM, and the time goes |
| 48 // instantaneously from 1:59 AM 3:00 AM. In this very rare case, it's OK for |
| 49 // this function to fail. |
| 50 return base::Time(); |
| 51 } |
| 52 |
| 53 std::string TimeOfDay::ToString() const { |
| 54 return base::UTF16ToUTF8(base::TimeFormatTimeOfDay(ToTimeToday())); |
| 55 } |
| 56 |
| 57 } // namespace ash |
OLD | NEW |