AceTime  0.6
Date and time classes for Arduino that support timezones from the TZ Database, and a system clock that can synchronize from an NTP server or an RTC chip.
LocalTime.h
1 /*
2  * MIT License
3  * Copyright (c) 2018 Brian T. Park
4  */
5 
6 #ifndef ACE_TIME_LOCAL_TIME_H
7 #define ACE_TIME_LOCAL_TIME_H
8 
9 #include <stdint.h>
10 #include "common/common.h"
11 
12 class Print;
13 
14 namespace ace_time {
15 
26 class LocalTime {
27  public:
29  static const acetime_t kInvalidSeconds = INT32_MIN;
30 
41  static LocalTime forComponents(uint8_t hour, uint8_t minute,
42  uint8_t second) {
43  return LocalTime(hour, minute, second);
44  }
45 
54  static LocalTime forSeconds(acetime_t seconds) {
55  uint8_t second, minute, hour;
56 
57  if (seconds == kInvalidSeconds) {
58  second = minute = hour = kInvalidValue; // causes isError() to be true
59  } else {
60  second = seconds % 60;
61  uint16_t minutes = seconds / 60;
62  minute = minutes % 60;
63  hour = minutes / 60;
64  }
65 
66  // Return a single object to allow return value optimization.
67  return LocalTime(hour, minute, second);
68  }
69 
78  static LocalTime forTimeString(const char* timeString);
79 
87  static LocalTime forTimeStringChainable(const char*& timeString);
88 
93  static LocalTime forError() {
94  return LocalTime(kInvalidValue, kInvalidValue, kInvalidValue);
95  }
96 
98  explicit LocalTime() {}
99 
106  bool isError() const {
107  if (mSecond >= 60) return true;
108  if (mMinute >= 60) return true;
109  if (mHour == 24) {
110  return mSecond != 0 || mMinute != 0;
111  }
112  return mHour > 24;
113  }
114 
116  uint8_t hour() const { return mHour; }
117 
119  void hour(uint8_t hour) { mHour = hour; }
120 
122  uint8_t minute() const { return mMinute; }
123 
125  void minute(uint8_t month) { mMinute = month; }
126 
128  uint8_t second() const { return mSecond; }
129 
131  void second(uint8_t second) { mSecond = second; }
132 
137  acetime_t toSeconds() const {
138  if (isError()) {
139  return kInvalidSeconds;
140  } else {
141  return ((mHour * (int16_t) 60) + mMinute)
142  * (int32_t) 60 + mSecond;
143  }
144  }
145 
151  int8_t compareTo(const LocalTime& that) const {
152  if (mHour < that.mHour) return -1;
153  if (mHour > that.mHour) return 1;
154  if (mMinute < that.mMinute) return -1;
155  if (mMinute > that.mMinute) return 1;
156  if (mSecond < that.mSecond) return -1;
157  if (mSecond > that.mSecond) return 1;
158  return 0;
159  }
160 
166  void printTo(Print& printer) const;
167 
168  // Use default copy constructor and assignment operator.
169  LocalTime(const LocalTime&) = default;
170  LocalTime& operator=(const LocalTime&) = default;
171 
172  private:
173  friend bool operator==(const LocalTime& a, const LocalTime& b);
174 
176  static const uint8_t kTimeStringLength = 8;
177 
179  static const uint8_t kInvalidValue = UINT8_MAX;
180 
182  explicit LocalTime(uint8_t hour, uint8_t minute, uint8_t second):
183  mHour(hour),
184  mMinute(minute),
185  mSecond(second) {}
186 
187  uint8_t mHour; // [0, 23]
188  uint8_t mMinute; // [0, 59]
189  uint8_t mSecond; // [0, 59]
190 };
191 
193 inline bool operator==(const LocalTime& a, const LocalTime& b) {
194  return a.mSecond == b.mSecond
195  && a.mMinute == b.mMinute
196  && a.mHour == b.mHour;
197 }
198 
200 inline bool operator!=(const LocalTime& a, const LocalTime& b) {
201  return ! (a == b);
202 }
203 
204 }
205 
206 #endif
acetime_t toSeconds() const
Return the number of seconds since midnight.
Definition: LocalTime.h:137
void printTo(Print &printer) const
Print LocalTime to &#39;printer&#39; in ISO 8601 format.
Definition: LocalTime.cpp:13
void second(uint8_t second)
Set the second.
Definition: LocalTime.h:131
void minute(uint8_t month)
Set the minute.
Definition: LocalTime.h:125
void hour(uint8_t hour)
Set the hour.
Definition: LocalTime.h:119
static const acetime_t kInvalidSeconds
An invalid seconds marker that indicates isError() true.
Definition: LocalTime.h:29
The time (hour, minute, second) fields representing the time without regards to the day or the time z...
Definition: LocalTime.h:26
static LocalTime forTimeStringChainable(const char *&timeString)
Variant of forTimeString() that updates the pointer to the next unprocessed character.
Definition: LocalTime.cpp:35
int8_t compareTo(const LocalTime &that) const
Compare this LocalTime with that LocalTime, and return (<0, 0, >0) according to whether (this<that...
Definition: LocalTime.h:151
bool isError() const
Return true if any component is outside the normal time range of 00:00:00 to 23:59:59.
Definition: LocalTime.h:106
static LocalTime forError()
Factory method that returns an instance which indicates an error condition.
Definition: LocalTime.h:93
static LocalTime forComponents(uint8_t hour, uint8_t minute, uint8_t second)
Factory method using separated date, time, and time zone fields.
Definition: LocalTime.h:41
uint8_t hour() const
Return the hour.
Definition: LocalTime.h:116
static LocalTime forTimeString(const char *timeString)
Factory method.
Definition: LocalTime.cpp:27
uint8_t minute() const
Return the minute.
Definition: LocalTime.h:122
uint8_t second() const
Return the second.
Definition: LocalTime.h:128
LocalTime()
Default constructor does nothing.
Definition: LocalTime.h:98
friend bool operator==(const LocalTime &a, const LocalTime &b)
Return true if two LocalTime objects are equal.
Definition: LocalTime.h:193
static LocalTime forSeconds(acetime_t seconds)
Factory method.
Definition: LocalTime.h:54