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.
TimeOffset.h
1 #ifndef ACE_TIME_TIME_OFFSET_H
2 #define ACE_TIME_TIME_OFFSET_H
3 
4 #include <stdint.h>
5 
6 class Print;
7 
8 namespace ace_time {
9 
10 // These functions need to set the mOffsetCode and it seemed inefficient to go
11 // through the factory method and assignment operator, so I expose
12 // setOffsetCode() to them for efficiency. If the compiler is smart enough to
13 // optimize away the assignment operator, then we could remove these friend
14 // declarations and just use the forOffsetCode() factory method instead. I
15 // haven't looked into this.
16 class TimeOffset;
17 namespace time_offset_mutation {
18 void incrementHour(TimeOffset& offset);
19 void increment15Minutes(TimeOffset& offset);
20 }
21 
53 class TimeOffset {
54  public:
56  static const int8_t kErrorCode = INT8_MIN;
57 
62  static TimeOffset forHour(int8_t hour) {
63  return TimeOffset(hour * 4);
64  }
65 
74  static TimeOffset forHourMinute(int8_t hour, int8_t minute) {
75  int8_t code = hour * 4 + minute / 15;
76  return TimeOffset(code);
77  }
78 
83  static TimeOffset forMinutes(int16_t minutes) {
84  return TimeOffset(minutes / 15);
85  }
86 
92  static TimeOffset forOffsetString(const char* offsetString);
93 
102  static TimeOffset forOffsetStringChainable(const char*& offsetString);
103 
105  static TimeOffset forError() { return TimeOffset(kErrorCode); }
106 
112  static TimeOffset forOffsetCode(int8_t offsetCode) {
113  return TimeOffset(offsetCode);
114  }
115 
117  explicit TimeOffset() {}
118 
120  int8_t toOffsetCode() const { return mOffsetCode; }
121 
123  int16_t toMinutes() const {
124  return (int16_t) 15 * mOffsetCode;
125  }
126 
128  int32_t toSeconds() const {
129  return (int32_t) 60 * toMinutes();
130  }
131 
137  void toHourMinute(int8_t& hour, int8_t& minute) const {
138  hour = mOffsetCode / 4;
139  minute = (mOffsetCode % 4) * 15;
140  }
141 
147  bool isZero() const { return mOffsetCode == 0; }
148 
150  bool isError() const {
151  return mOffsetCode == kErrorCode;
152  }
153 
155  void printTo(Print& printer) const;
156 
157  // Use default copy constructor and assignment operator.
158  TimeOffset(const TimeOffset&) = default;
159  TimeOffset& operator=(const TimeOffset&) = default;
160 
161  private:
162  friend bool operator==(const TimeOffset& a, const TimeOffset& b);
163  // Give access to setOffsetCode()
164  friend void time_offset_mutation::incrementHour(TimeOffset& offset);
165  friend void time_offset_mutation::increment15Minutes(TimeOffset& offset);
166 
168  static const uint8_t kTimeOffsetStringLength = 6;
169 
171  explicit TimeOffset(int8_t offsetCode):
172  mOffsetCode(offsetCode) {}
173 
175  void setOffsetCode(int8_t offsetCode) { mOffsetCode = offsetCode; }
176 
187  int8_t mOffsetCode = 0;
188 };
189 
190 inline bool operator==(const TimeOffset& a, const TimeOffset& b) {
191  return a.mOffsetCode == b.mOffsetCode;
192 }
193 
194 inline bool operator!=(const TimeOffset& a, const TimeOffset& b) {
195  return ! (a == b);
196 }
197 
198 }
199 
200 #endif
static TimeOffset forError()
Return an error indicator.
Definition: TimeOffset.h:105
TimeOffset()
Constructor.
Definition: TimeOffset.h:117
bool isError() const
Return true if this TimeOffset represents an error.
Definition: TimeOffset.h:150
int32_t toSeconds() const
Return the time offset as seconds.
Definition: TimeOffset.h:128
static TimeOffset forHour(int8_t hour)
Create TimeOffset with the corresponding hour offset.
Definition: TimeOffset.h:62
int16_t toMinutes() const
Return the time offset as minutes.
Definition: TimeOffset.h:123
static TimeOffset forOffsetCode(int8_t offsetCode)
Create TimeOffset from the offset code.
Definition: TimeOffset.h:112
static TimeOffset forHourMinute(int8_t hour, int8_t minute)
Create TimeOffset from (hour, minute) offset.
Definition: TimeOffset.h:74
A thin wrapper that represents a time offset from a reference point, usually 00:00 at UTC...
Definition: TimeOffset.h:53
static TimeOffset forMinutes(int16_t minutes)
Create TimeOffset from minutes from 00:00.
Definition: TimeOffset.h:83
bool isZero() const
Returns true if offset is 00:00.
Definition: TimeOffset.h:147
int8_t toOffsetCode() const
Return the time offset as the number of 15 minute increments.
Definition: TimeOffset.h:120
void toHourMinute(int8_t &hour, int8_t &minute) const
Extract hour and minute representation of the offset.
Definition: TimeOffset.h:137