AceTime 3.0.0 Date and time classes for Arduino that support timezones from the TZ Database.

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
27 class LocalTime {
28 public:
30 static const int32_t kInvalidSeconds = INT32_MIN;
31
43 static LocalTime forComponents(uint8_t hour, uint8_t minute,
44 uint8_t second, uint8_t fold = 0) {
45 return LocalTime(hour, minute, second, fold);
46 }
47
56 static LocalTime forSeconds(acetime_t seconds, uint8_t fold = 0) {
57 uint8_t second, minute, hour;
58
59 if (seconds == kInvalidSeconds) {
60 second = minute = hour = kInvalidValue; // causes isError() to be true
61 } else {
62 second = seconds % 60;
63 uint16_t minutes = seconds / 60;
64 minute = minutes % 60;
65 hour = minutes / 60;
66 }
67
68 // Return a single object to allow return value optimization.
69 return LocalTime(hour, minute, second, fold);
70 }
71
80 static LocalTime forTimeString(const char* timeString);
81
89 static LocalTime forTimeStringChainable(const char*& timeString);
90
95 static LocalTime forError() {
96 return LocalTime(kInvalidValue, kInvalidValue, kInvalidValue);
97 }
98
100 explicit LocalTime() {}
101
108 bool isError() const {
109 if (mSecond >= 60) return true;
110 if (mMinute >= 60) return true;
111 if (mHour == 24) {
112 return mSecond != 0 || mMinute != 0;
113 }
114 return mHour > 24;
115 }
116
118 uint8_t hour() const { return mHour; }
119
121 void hour(uint8_t hour) { mHour = hour; }
122
124 uint8_t minute() const { return mMinute; }
125
127 void minute(uint8_t minute) { mMinute = minute; }
128
130 uint8_t second() const { return mSecond; }
131
133 void second(uint8_t second) { mSecond = second; }
134
136 uint8_t fold() const { return mFold; }
137
139 void fold(uint8_t fold) { mFold = fold; }
140
145 acetime_t toSeconds() const {
146 if (isError()) {
147 return kInvalidSeconds;
148 } else {
149 return ((mHour * (int16_t) 60) + mMinute)
150 * (int32_t) 60 + mSecond;
151 }
152 }
153
162 int8_t compareTo(const LocalTime& that) const {
163 if (mHour < that.mHour) return -1;
164 if (mHour > that.mHour) return 1;
165 if (mMinute < that.mMinute) return -1;
166 if (mMinute > that.mMinute) return 1;
167 if (mSecond < that.mSecond) return -1;
168 if (mSecond > that.mSecond) return 1;
169 return 0;
170 }
171
177 void printTo(Print& printer) const;
178
179 // Use default copy constructor and assignment operator.
180 LocalTime(const LocalTime&) = default;
181 LocalTime& operator=(const LocalTime&) = default;
182
183 private:
184 friend bool operator==(const LocalTime& a, const LocalTime& b);
185
187 static const uint8_t kTimeStringLength = 8;
188
190 static const uint8_t kInvalidValue = UINT8_MAX;
191
193 explicit LocalTime(
194 uint8_t hour,
195 uint8_t minute,
196 uint8_t second,
197 uint8_t fold = 0
198 ):
199 mHour(hour),
200 mMinute(minute),
201 mSecond(second),
202 mFold(fold)
203 {}
204
205 private:
206 uint8_t mHour; // [0, 23]
207 uint8_t mMinute; // [0, 59]
208 uint8_t mSecond; // [0, 59]
209
210 // Use a separate byte for fold. If we implemented this using a C++ bit
211 // field (e.g. the upper bit of 'mHour'), it causes BasicZoneProcessor and
212 // ExtendedZoneProcessor to consume 200 extra bytes of flash due to the bit
213 // masking operations on accesses and mutations. Even on AVR processors, I
214 // think the increase in static memory is better than paying the 200 bytes
215 // of flash memory. Using a separate byte is also faster.
216 uint8_t mFold; // [0, 1]
217 };
218
220 inline bool operator==(const LocalTime& a, const LocalTime& b) {
221 return a.mSecond == b.mSecond
222 && a.mMinute == b.mMinute
223 && a.mHour == b.mHour;
224 }
225
227 inline bool operator!=(const LocalTime& a, const LocalTime& b) {
228 return ! (a == b);
229 }
230
231 }
232
233 #endif
ace_time::LocalTime
The time (hour, minute, second) fields representing the time without regards to the day or the time z...
Definition: LocalTime.h:27
ace_time::LocalTime::kInvalidSeconds
static const int32_t kInvalidSeconds
An invalid seconds marker that indicates isError() true.
Definition: LocalTime.h:30
ace_time::LocalTime::forComponents
static LocalTime forComponents(uint8_t hour, uint8_t minute, uint8_t second, uint8_t fold=0)
Factory method using separated date, time, and time zone fields.
Definition: LocalTime.h:43
ace_time::LocalTime::fold
uint8_t fold() const
Return the fold.
Definition: LocalTime.h:136
ace_time::LocalTime::operator==
friend bool operator==(const LocalTime &a, const LocalTime &b)
Return true if two LocalTime objects are equal.
Definition: LocalTime.h:220
ace_time::LocalTime::toSeconds
acetime_t toSeconds() const
Return the number of seconds since midnight.
Definition: LocalTime.h:145
ace_time::LocalTime::forTimeStringChainable
static LocalTime forTimeStringChainable(const char *&timeString)
Variant of forTimeString() that updates the pointer to the next unprocessed character.
Definition: LocalTime.cpp:36
ace_time::LocalTime::minute
void minute(uint8_t minute)
Set the minute.
Definition: LocalTime.h:127
ace_time::LocalTime::hour
void hour(uint8_t hour)
Set the hour.
Definition: LocalTime.h:121
ace_time::LocalTime::forError
static LocalTime forError()
Factory method that returns an instance which indicates an error condition.
Definition: LocalTime.h:95
ace_time::LocalTime::forTimeString
static LocalTime forTimeString(const char *timeString)
Factory method.
Definition: LocalTime.cpp:28
ace_time::LocalTime::printTo
void printTo(Print &printer) const
Print LocalTime to 'printer' in ISO 8601 format.
Definition: LocalTime.cpp:14
ace_time::LocalTime::LocalTime
LocalTime()
Default constructor does nothing.
Definition: LocalTime.h:100
ace_time::LocalTime::hour
uint8_t hour() const
Return the hour.
Definition: LocalTime.h:118
ace_time::LocalTime::second
void second(uint8_t second)
Set the second.
Definition: LocalTime.h:133
ace_time::LocalTime::minute
uint8_t minute() const
Return the minute.
Definition: LocalTime.h:124
ace_time::LocalTime::forSeconds
static LocalTime forSeconds(acetime_t seconds, uint8_t fold=0)
Factory method.
Definition: LocalTime.h:56
ace_time::LocalTime::second
uint8_t second() const
Return the second.
Definition: LocalTime.h:130
ace_time::LocalTime::isError
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:108
ace_time::LocalTime::fold
void fold(uint8_t fold)
Set the fold.
Definition: LocalTime.h:139
ace_time::LocalTime::compareTo
int8_t compareTo(const LocalTime &that) const
Compare 'this' LocalTime with 'that' LocalTime, and return (<0, 0, >0) according to whether 'this' oc...
Definition: LocalTime.h:162
common.h
Identifiers used by implementation code which need to be publically exported.
ace_time::acetime_t
int32_t acetime_t
Type for the number of seconds from epoch.
Definition: common.h:24