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

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

Issue 2887913004: [Night Light] CL4: Automatic schedule backend. (Closed)
Patch Set: Working Created 3 years, 7 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
Index: ash/system/night_light/night_light_time.cc
diff --git a/ash/system/night_light/night_light_time.cc b/ash/system/night_light/night_light_time.cc
new file mode 100644
index 0000000000000000000000000000000000000000..6c61c72a94717e62bf4ba11f5ac207d1c002c026
--- /dev/null
+++ b/ash/system/night_light/night_light_time.cc
@@ -0,0 +1,60 @@
+// 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/night_light_time.h"
+#include <base/i18n/time_formatting.h>
James Cook 2017/05/23 16:34:59 nit: blank line above, and no <>
afakhry 2017/05/24 04:21:10 Done.
+#include <base/strings/utf_string_conversions.h>
James Cook 2017/05/23 16:34:59 no <>
afakhry 2017/05/24 04:21:10 Done.
+
+namespace ash {
+
+namespace {
+
+constexpr int kMaxOffsetMinutes = 24 * 60;
+
+} // namespace
+
+NightLightTime::NightLightTime(int offset_minutes)
+ : offset_minutes_from_zero_hour_(
+ offset_minutes == kMaxOffsetMinutes ? 0 : offset_minutes) {
+ DCHECK_LE(offset_minutes_from_zero_hour_, kMaxOffsetMinutes);
+}
+
+NightLightTime::NightLightTime(const NightLightTime& other)
+ : offset_minutes_from_zero_hour_(other.offset_minutes_from_zero_hour_) {}
+
+// static
+NightLightTime NightLightTime::FromTime(const base::Time& time) {
+ base::Time::Exploded exploded;
+ time.LocalExplode(&exploded);
+ return NightLightTime(exploded.hour * 60 + exploded.minute);
+}
+
+NightLightTime& NightLightTime::operator=(const NightLightTime& rhs) {
+ if (this != &rhs)
+ offset_minutes_from_zero_hour_ = rhs.offset_minutes_from_zero_hour_;
+
+ return *this;
+}
+
+bool NightLightTime::operator==(const NightLightTime& rhs) const {
+ return offset_minutes_from_zero_hour_ == rhs.offset_minutes_from_zero_hour_;
+}
+
+base::Time NightLightTime::ToTime() 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;
+ CHECK(base::Time::FromLocalExploded(now, &result));
James Cook 2017/05/23 16:34:59 Will this CHECK-fail and crash at 12:30 AM during
afakhry 2017/05/24 04:21:11 It seems that we have code to handle this case: ht
+ return result;
+}
+
+std::string NightLightTime::ToString() const {
+ return base::UTF16ToUTF8(base::TimeFormatTimeOfDay(ToTime()));
+}
+
+} // namespace ash

Powered by Google App Engine
This is Rietveld 408576698