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

TimeOffset.h
1 /*
2 * MIT License
3 * Copyright (c) 2018 Brian T. Park
4 */
5
6 #ifndef ACE_TIME_TIME_OFFSET_H
7 #define ACE_TIME_TIME_OFFSET_H
8
9 #include <stdint.h>
10
11 class Print;
12
13 namespace ace_time {
14
15 // These functions need to set the mSeconds and it seemed inefficient to
16 // go through the factory method and assignment operator, so I expose
17 // setMinutes() to them for efficiency.
18 class TimeOffset;
19 namespace time_offset_mutation {
20 void incrementHour(TimeOffset& offset);
21 void increment15Minutes(TimeOffset& offset);
22 }
23
56 class TimeOffset {
57 public:
62 static TimeOffset forHours(int8_t hours) {
63 return TimeOffset::forMinutes(hours * int16_t(60));
64 }
65
74 static TimeOffset forHourMinute(int8_t hour, int8_t minute) {
75 int32_t minutes = (hour * int32_t(60) + minute) * 60;
76 return TimeOffset(minutes);
77 }
78
84 static TimeOffset forHourMinuteSecond(
85 int8_t hour, int8_t minute, int8_t second) {
86 int32_t seconds = (hour * int32_t(60) + minute) * 60 + second;
87 return TimeOffset(seconds);
88 }
89
91 static TimeOffset forMinutes(int16_t minutes) {
92 return TimeOffset(minutes * int32_t(60));
93 }
94
96 static TimeOffset forSeconds(int32_t seconds) {
97 return TimeOffset(seconds);
98 }
99
109 static TimeOffset forOffsetString(const char* offsetString);
110
119 static TimeOffset forOffsetStringChainable(const char*& offsetString);
120
122 static TimeOffset forError() { return TimeOffset(kErrorSeconds); }
123
125 explicit TimeOffset() {}
126
128 int16_t toMinutes() const { return mSeconds / 60; }
129
131 int32_t toSeconds() const { return mSeconds; }
132
138 void toHourMinute(int8_t& hour, int8_t& minute) const {
139 int32_t minutes = mSeconds / 60;
140 hour = minutes / 60;
141 minute = minutes % 60;
142 }
143
149 void toHourMinuteSecond(
150 int8_t& hour, int8_t& minute, int8_t& second) const {
151 int32_t minutes = mSeconds / 60;
152 second = mSeconds % 60;
153 hour = minutes / 60;
154 minute = minutes % 60;
155 }
156
162 bool isZero() const { return mSeconds == 0; }
163
165 bool isError() const {
166 return mSeconds == kErrorSeconds;
167 }
168
175 void printTo(Print& printer) const;
176
177 // Use default copy constructor and assignment operator.
178 TimeOffset(const TimeOffset&) = default;
179 TimeOffset& operator=(const TimeOffset&) = default;
180
181 private:
182 friend bool operator==(const TimeOffset& a, const TimeOffset& b);
183
184 // Give access to setMinutes()
185 friend void time_offset_mutation::incrementHour(TimeOffset& offset);
186 friend void time_offset_mutation::increment15Minutes(TimeOffset& offset);
187
189 explicit TimeOffset(int32_t seconds):
190 mSeconds(seconds) {}
191
193 void setMinutes(int16_t minutes) {
194 mSeconds = minutes * int32_t(60);
195 }
196
198 void setSeconds(int32_t seconds) {
199 mSeconds = seconds;
200 }
201
202 private:
204 static const int32_t kErrorSeconds = INT32_MIN;
205
210 int32_t mSeconds = 0;
211 };
212
213 inline bool operator==(const TimeOffset& a, const TimeOffset& b) {
214 return a.mSeconds == b.mSeconds;
215 }
216
217 inline bool operator!=(const TimeOffset& a, const TimeOffset& b) {
218 return ! (a == b);
219 }
220
221 }
222
223 #endif
ace_time::TimeOffset
A thin wrapper that represents a time offset from a reference point, usually 00:00 at UTC,...
Definition: TimeOffset.h:56
ace_time::TimeOffset::toHourMinuteSecond
void toHourMinuteSecond(int8_t &hour, int8_t &minute, int8_t &second) const
Extract hour, minute, second from the offset.
Definition: TimeOffset.h:149
ace_time::TimeOffset::toMinutes
int16_t toMinutes() const
Return the time offset as minutes.
Definition: TimeOffset.h:128
ace_time::TimeOffset::TimeOffset
TimeOffset()
Constructor.
Definition: TimeOffset.h:125
ace_time::TimeOffset::toHourMinute
void toHourMinute(int8_t &hour, int8_t &minute) const
Extract hour and minute representation of the offset.
Definition: TimeOffset.h:138
ace_time::TimeOffset::forOffsetString
static TimeOffset forOffsetString(const char *offsetString)
Create from an offset string (e.g.
Definition: TimeOffset.cpp:38
ace_time::TimeOffset::isZero
bool isZero() const
Returns true if offset is 00:00.
Definition: TimeOffset.h:162
ace_time::TimeOffset::forError
static TimeOffset forError()
Return an error indicator.
Definition: TimeOffset.h:122
ace_time::TimeOffset::toSeconds
int32_t toSeconds() const
Return the time offset as seconds.
Definition: TimeOffset.h:131
ace_time::TimeOffset::forSeconds
static TimeOffset forSeconds(int32_t seconds)
Create TimeOffset from seconds from 00:00.
Definition: TimeOffset.h:96
ace_time::TimeOffset::forHourMinuteSecond
static TimeOffset forHourMinuteSecond(int8_t hour, int8_t minute, int8_t second)
Create a TimeOffset fro (hour, minute, second) offset.
Definition: TimeOffset.h:84
ace_time::TimeOffset::forHours
static TimeOffset forHours(int8_t hours)
Create TimeOffset with the corresponding hour offset.
Definition: TimeOffset.h:62
ace_time::TimeOffset::forHourMinute
static TimeOffset forHourMinute(int8_t hour, int8_t minute)
Create TimeOffset from (hour, minute) offset.
Definition: TimeOffset.h:74
ace_time::TimeOffset::forMinutes
static TimeOffset forMinutes(int16_t minutes)
Create TimeOffset from minutes from 00:00.
Definition: TimeOffset.h:91
ace_time::TimeOffset::printTo
void printTo(Print &printer) const
Print the human readable string, including a "-" or "+" prefix, in the form of "+/-hh:mm" or "+/-hh:m...
Definition: TimeOffset.cpp:15
ace_time::TimeOffset::forOffsetStringChainable
static TimeOffset forOffsetStringChainable(const char *&offsetString)
Variant of forOffsetString() that updates the string pointer to the next unprocessed character.
Definition: TimeOffset.cpp:48
ace_time::TimeOffset::isError
bool isError() const
Return true if this TimeOffset represents an error.
Definition: TimeOffset.h:165
ace_time::offset_date_time_mutation::incrementHour
void incrementHour(OffsetDateTime &dateTime)
Increment the hour by one within the interval [0, 23].
Definition: offset_date_time_mutation.h:63
ace_time::time_offset_mutation::increment15Minutes
void increment15Minutes(TimeOffset &offset)
Increment the TimeOffset by 15 minute interval.
Definition: time_offset_mutation.h:36