Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(402)

Unified Diff: ash/system/night_light/time_of_day.cc

Issue 2887913004: [Night Light] CL4: Automatic schedule backend. (Closed)
Patch Set: Nits Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ash/system/night_light/time_of_day.h ('k') | ash/system/night_light/time_of_day_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ash/system/night_light/time_of_day.cc
diff --git a/ash/system/night_light/time_of_day.cc b/ash/system/night_light/time_of_day.cc
new file mode 100644
index 0000000000000000000000000000000000000000..b76ca23dc9bc0d7784035b1fbeca4a40ece4c29e
--- /dev/null
+++ b/ash/system/night_light/time_of_day.cc
@@ -0,0 +1,57 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "ash/system/night_light/time_of_day.h"
+
+#include "base/i18n/time_formatting.h"
+#include "base/strings/utf_string_conversions.h"
+
+namespace ash {
+
+namespace {
+
+constexpr int kMaxOffsetMinutes = 24 * 60;
+
+} // namespace
+
+TimeOfDay::TimeOfDay(int offset_minutes)
+ : offset_minutes_from_zero_hour_(
+ offset_minutes == kMaxOffsetMinutes ? 0 : offset_minutes) {
+ DCHECK_LE(offset_minutes_from_zero_hour_, kMaxOffsetMinutes);
+}
+
+// static
+TimeOfDay TimeOfDay::FromTime(const base::Time& time) {
+ base::Time::Exploded exploded;
+ time.LocalExplode(&exploded);
+ return TimeOfDay(exploded.hour * 60 + exploded.minute);
+}
+
+bool TimeOfDay::operator==(const TimeOfDay& rhs) const {
+ return offset_minutes_from_zero_hour_ == rhs.offset_minutes_from_zero_hour_;
+}
+
+base::Time TimeOfDay::ToTimeToday() const {
+ base::Time::Exploded now;
+ base::Time::Now().LocalExplode(&now);
+ now.hour = (offset_minutes_from_zero_hour_ / 60) % 24;
+ now.minute = offset_minutes_from_zero_hour_ % 60;
+ now.second = 0;
+ now.millisecond = 0;
+ base::Time result;
+ if (base::Time::FromLocalExploded(now, &result))
+ return result;
+
+ // Daylight saving time can cause FromLocalExploded() to fail on the
+ // transition day in the spring when TimeOfDay == 2:30 AM, and the time goes
+ // instantaneously from 1:59 AM 3:00 AM. In this very rare case, it's OK for
+ // this function to fail.
+ return base::Time();
+}
+
+std::string TimeOfDay::ToString() const {
+ return base::UTF16ToUTF8(base::TimeFormatTimeOfDay(ToTimeToday()));
+}
+
+} // namespace ash
« no previous file with comments | « ash/system/night_light/time_of_day.h ('k') | ash/system/night_light/time_of_day_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698