AceTime  0.3
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 #ifndef ACE_TIME_LOCAL_TIME_H
2 #define ACE_TIME_LOCAL_TIME_H
3 
4 #include <stdint.h>
5 #include "common/common.h"
6 
7 class Print;
8 
9 namespace ace_time {
10 
20 class LocalTime {
21  public:
23  static const acetime_t kInvalidSeconds = INT32_MIN;
24 
35  static LocalTime forComponents(uint8_t hour, uint8_t minute,
36  uint8_t second) {
37  return LocalTime(hour, minute, second);
38  }
39 
48  static LocalTime forSeconds(acetime_t seconds) {
49  uint8_t second, minute, hour;
50 
51  if (seconds == kInvalidSeconds) {
52  second = minute = hour = kInvalidValue; // causes isError() to be true
53  } else {
54  second = seconds % 60;
55  uint16_t minutes = seconds / 60;
56  minute = minutes % 60;
57  hour = minutes / 60;
58  }
59 
60  // Return a single object to allow return value optimization.
61  return LocalTime(hour, minute, second);
62  }
63 
72  static LocalTime forTimeString(const char* timeString);
73 
81  static LocalTime forTimeStringChainable(const char*& timeString);
82 
87  static LocalTime forError() {
88  return LocalTime(kInvalidValue, kInvalidValue, kInvalidValue);
89  }
90 
92  explicit LocalTime() {}
93 
100  bool isError() const {
101  if (mSecond >= 60) return true;
102  if (mMinute >= 60) return true;
103  if (mHour == 24) {
104  return mSecond != 0 || mMinute != 0;
105  }
106  return mHour > 24;
107  }
108 
110  uint8_t hour() const { return mHour; }
111 
113  void hour(uint8_t hour) { mHour = hour; }
114 
116  uint8_t minute() const { return mMinute; }
117 
119  void minute(uint8_t month) { mMinute = month; }
120 
122  uint8_t second() const { return mSecond; }
123 
125  void second(uint8_t second) { mSecond = second; }
126 
131  acetime_t toSeconds() const {
132  if (isError()) {
133  return kInvalidSeconds;
134  } else {
135  return ((mHour * (int16_t) 60) + mMinute)
136  * (int32_t) 60 + mSecond;
137  }
138  }
139 
145  int8_t compareTo(const LocalTime& that) const {
146  if (mHour < that.mHour) return -1;
147  if (mHour > that.mHour) return 1;
148  if (mMinute < that.mMinute) return -1;
149  if (mMinute > that.mMinute) return 1;
150  if (mSecond < that.mSecond) return -1;
151  if (mSecond > that.mSecond) return 1;
152  return 0;
153  }
154 
160  void printTo(Print& printer) const;
161 
162  // Use default copy constructor and assignment operator.
163  LocalTime(const LocalTime&) = default;
164  LocalTime& operator=(const LocalTime&) = default;
165 
166  private:
167  friend bool operator==(const LocalTime& a, const LocalTime& b);
168 
170  static const uint8_t kTimeStringLength = 8;
171 
173  static const uint8_t kInvalidValue = UINT8_MAX;
174 
176  explicit LocalTime(uint8_t hour, uint8_t minute, uint8_t second):
177  mHour(hour),
178  mMinute(minute),
179  mSecond(second) {}
180 
181  uint8_t mHour; // [0, 23]
182  uint8_t mMinute; // [0, 59]
183  uint8_t mSecond; // [0, 59]
184 };
185 
187 inline bool operator==(const LocalTime& a, const LocalTime& b) {
188  return a.mSecond == b.mSecond
189  && a.mMinute == b.mMinute
190  && a.mHour == b.mHour;
191 }
192 
194 inline bool operator!=(const LocalTime& a, const LocalTime& b) {
195  return ! (a == b);
196 }
197 
198 }
199 
200 #endif
acetime_t toSeconds() const
Return the number of seconds since midnight.
Definition: LocalTime.h:131
void printTo(Print &printer) const
Print LocalTime to &#39;printer&#39; in ISO 8601 format.
Definition: LocalTime.cpp:8
void second(uint8_t second)
Set the second.
Definition: LocalTime.h:125
void minute(uint8_t month)
Set the minute.
Definition: LocalTime.h:119
void hour(uint8_t hour)
Set the hour.
Definition: LocalTime.h:113
static const acetime_t kInvalidSeconds
An invalid seconds marker that indicates isError() true.
Definition: LocalTime.h:23
The time (hour, minute, second) fields representing the time without regards to the day or the time z...
Definition: LocalTime.h:20
static LocalTime forTimeStringChainable(const char *&timeString)
Variant of forTimeString() that updates the pointer to the next unprocessed character.
Definition: LocalTime.cpp:30
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:145
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:100
static LocalTime forError()
Factory method that returns an instance which indicates an error condition.
Definition: LocalTime.h:87
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:35
uint8_t hour() const
Return the hour.
Definition: LocalTime.h:110
static LocalTime forTimeString(const char *timeString)
Factory method.
Definition: LocalTime.cpp:22
uint8_t minute() const
Return the minute.
Definition: LocalTime.h:116
uint8_t second() const
Return the second.
Definition: LocalTime.h:122
LocalTime()
Default constructor does nothing.
Definition: LocalTime.h:92
friend bool operator==(const LocalTime &a, const LocalTime &b)
Return true if two LocalTime objects are equal.
Definition: LocalTime.h:187
static LocalTime forSeconds(acetime_t seconds)
Factory method.
Definition: LocalTime.h:48