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

TimePeriod.h
1 /*
2 * MIT License
3 * Copyright (c) 2018 Brian T. Park
4 */
5
6 #ifndef ACE_TIME_TIME_PERIOD_H
7 #define ACE_TIME_TIME_PERIOD_H
8
9 #include <stdint.h>
10
11 class Print;
12
13 namespace ace_time {
14
27 class TimePeriod {
28 public:
30 static const int32_t kInvalidPeriodSeconds = INT32_MIN;
31
37 static const int32_t kMaxPeriodSeconds = 921599;
38
46 static TimePeriod forError(int8_t sign = 0) {
47 return TimePeriod(255, 255, 255, sign);
48 }
49
60 explicit TimePeriod(uint8_t hour, uint8_t minute, uint8_t second,
61 int8_t sign = 1):
62 mHour(hour),
63 mMinute(minute),
64 mSecond(second),
65 mSign(sign) {}
66
75 explicit TimePeriod(int32_t seconds = 0) {
76 if (seconds == kInvalidPeriodSeconds) {
77 mHour = mMinute = mSecond = 255;
78 mSign = 0;
79 return;
80 } else if (seconds < -kMaxPeriodSeconds) {
81 mHour = mMinute = mSecond = 255;
82 mSign = -1;
83 return;
84 } else if (seconds > kMaxPeriodSeconds) {
85 mHour = mMinute = mSecond = 255;
86 mSign = 1;
87 return;
88 }
89
90 if (seconds < 0) {
91 mSign = -1;
92 seconds = -seconds;
93 } else {
94 mSign = 1;
95 }
96 mSecond = seconds % 60;
97 seconds /= 60;
98 mMinute = seconds % 60;
99 seconds /= 60;
100 mHour = seconds;
101 }
102
104 uint8_t hour() const { return mHour; }
105
107 void hour(uint8_t hour) { mHour = hour; }
108
110 uint8_t minute() const { return mMinute; }
111
113 void minute(uint8_t minute) { mMinute = minute; }
114
116 uint8_t second() const { return mSecond; }
117
119 void second(uint8_t second) { mSecond = second; }
120
122 int8_t sign() const { return mSign; }
123
129 void sign(int8_t sign) { mSign = sign; }
130
136 int32_t toSeconds() const {
137 if (isError()) {
138 return kInvalidPeriodSeconds;
139 }
140 int32_t seconds = ((mHour * (int16_t) 60) + mMinute) * (int32_t) 60
141 + mSecond;
142 return (mSign > 0) ? seconds : -seconds;
143 }
144
153 bool isError() const {
154 return mSign == 0 || mSecond == 255 || mMinute == 255;
155 }
156
162 int8_t compareTo(const TimePeriod& that) const {
163 int32_t thisSeconds = toSeconds();
164 int32_t thatSeconds = that.toSeconds();
165 if (thisSeconds < thatSeconds) {
166 return -1;
167 } else if (thisSeconds > thatSeconds) {
168 return 1;
169 } else {
170 return 0;
171 }
172 }
173
185 void printTo(Print& printer) const;
186
187 // Use default copy constructor and assignment operator.
188 TimePeriod(const TimePeriod&) = default;
189 TimePeriod& operator=(const TimePeriod&) = default;
190
191 private:
192 friend bool operator==(const TimePeriod& a, const TimePeriod& b);
193
194 uint8_t mHour; // [0, 255], normally hour < 24
195 uint8_t mMinute; // [0, 59], normally minute < 60
196 uint8_t mSecond; // [0, 59], normally second < 60
197
204 int8_t mSign;
205 };
206
211 inline bool operator==(const TimePeriod& a, const TimePeriod& b) {
212 return a.mSecond == b.mSecond
213 && a.mMinute == b.mMinute
214 && a.mHour == b.mHour
215 && a.mSign == b.mSign;
216 }
217
219 inline bool operator!=(const TimePeriod& a, const TimePeriod& b) {
220 return ! (a == b);
221 }
222
223 }
224
225 #endif
ace_time::TimePeriod
Represents a period of time relative to some known point in time, potentially represented by a DateTi...
Definition: TimePeriod.h:27
ace_time::TimePeriod::sign
int8_t sign() const
Return the sign bit.
Definition: TimePeriod.h:122
ace_time::TimePeriod::kInvalidPeriodSeconds
static const int32_t kInvalidPeriodSeconds
An invalid time period seconds.
Definition: TimePeriod.h:30
ace_time::TimePeriod::TimePeriod
TimePeriod(int32_t seconds=0)
Constructor from number of seconds.
Definition: TimePeriod.h:75
ace_time::TimePeriod::forError
static TimePeriod forError(int8_t sign=0)
Factory method that creates a TimePeriod representing a generic error so that isError() returns true.
Definition: TimePeriod.h:46
ace_time::TimePeriod::minute
void minute(uint8_t minute)
Set the minute.
Definition: TimePeriod.h:113
ace_time::TimePeriod::kMaxPeriodSeconds
static const int32_t kMaxPeriodSeconds
The largest period that can be represented by this class, in seconds, corresponding to +/- 255h59m59s...
Definition: TimePeriod.h:37
ace_time::TimePeriod::compareTo
int8_t compareTo(const TimePeriod &that) const
Compare 'this' TimePeriod with 'that' TimePeriod and return (<0, 0, >0) according to (this<that,...
Definition: TimePeriod.h:162
ace_time::TimePeriod::hour
void hour(uint8_t hour)
Set the hour.
Definition: TimePeriod.h:107
ace_time::TimePeriod::TimePeriod
TimePeriod(uint8_t hour, uint8_t minute, uint8_t second, int8_t sign=1)
Constructor.
Definition: TimePeriod.h:60
ace_time::TimePeriod::second
uint8_t second() const
Return the second.
Definition: TimePeriod.h:116
ace_time::TimePeriod::sign
void sign(int8_t sign)
Set the sign bit.
Definition: TimePeriod.h:129
ace_time::TimePeriod::operator==
friend bool operator==(const TimePeriod &a, const TimePeriod &b)
Return true if two TimePeriod objects are equal.
Definition: TimePeriod.h:211
ace_time::TimePeriod::minute
uint8_t minute() const
Return the minute.
Definition: TimePeriod.h:110
ace_time::TimePeriod::printTo
void printTo(Print &printer) const
Print to given printer.
Definition: TimePeriod.cpp:14
ace_time::TimePeriod::second
void second(uint8_t second)
Set the second.
Definition: TimePeriod.h:119
ace_time::TimePeriod::hour
uint8_t hour() const
Return the hour.
Definition: TimePeriod.h:104
ace_time::TimePeriod::toSeconds
int32_t toSeconds() const
Convert to number of seconds.
Definition: TimePeriod.h:136
ace_time::TimePeriod::isError
bool isError() const
Return true if this represents an error.
Definition: TimePeriod.h:153