AceTime  0.6.1
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 /*
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 mOffsetCode and it seemed inefficient to go
16 // through the factory method and assignment operator, so I expose
17 // setOffsetCode() to them for efficiency. If the compiler is smart enough to
18 // optimize away the assignment operator, then we could remove these friend
19 // declarations and just use the forOffsetCode() factory method instead. I
20 // haven't looked into this.
21 class TimeOffset;
22 namespace time_offset_mutation {
23 void incrementHour(TimeOffset& offset);
24 void increment15Minutes(TimeOffset& offset);
25 }
26 
58 class TimeOffset {
59  public:
61  static const int8_t kErrorCode = INT8_MIN;
62 
67  static TimeOffset forHour(int8_t hour) {
68  return TimeOffset(hour * 4);
69  }
70 
79  static TimeOffset forHourMinute(int8_t hour, int8_t minute) {
80  int8_t code = hour * 4 + minute / 15;
81  return TimeOffset(code);
82  }
83 
88  static TimeOffset forMinutes(int16_t minutes) {
89  return TimeOffset(minutes / 15);
90  }
91 
97  static TimeOffset forOffsetString(const char* offsetString);
98 
107  static TimeOffset forOffsetStringChainable(const char*& offsetString);
108 
110  static TimeOffset forError() { return TimeOffset(kErrorCode); }
111 
117  static TimeOffset forOffsetCode(int8_t offsetCode) {
118  return TimeOffset(offsetCode);
119  }
120 
122  explicit TimeOffset() {}
123 
125  int8_t toOffsetCode() const { return mOffsetCode; }
126 
128  int16_t toMinutes() const {
129  return (int16_t) 15 * mOffsetCode;
130  }
131 
133  int32_t toSeconds() const {
134  return (int32_t) 60 * toMinutes();
135  }
136 
142  void toHourMinute(int8_t& hour, int8_t& minute) const {
143  hour = mOffsetCode / 4;
144  minute = (mOffsetCode % 4) * 15;
145  }
146 
152  bool isZero() const { return mOffsetCode == 0; }
153 
155  bool isError() const {
156  return mOffsetCode == kErrorCode;
157  }
158 
160  void printTo(Print& printer) const;
161 
162  // Use default copy constructor and assignment operator.
163  TimeOffset(const TimeOffset&) = default;
164  TimeOffset& operator=(const TimeOffset&) = default;
165 
166  private:
167  friend bool operator==(const TimeOffset& a, const TimeOffset& b);
168  // Give access to setOffsetCode()
169  friend void time_offset_mutation::incrementHour(TimeOffset& offset);
170  friend void time_offset_mutation::increment15Minutes(TimeOffset& offset);
171 
173  static const uint8_t kTimeOffsetStringLength = 6;
174 
176  explicit TimeOffset(int8_t offsetCode):
177  mOffsetCode(offsetCode) {}
178 
180  void setOffsetCode(int8_t offsetCode) { mOffsetCode = offsetCode; }
181 
192  int8_t mOffsetCode = 0;
193 };
194 
195 inline bool operator==(const TimeOffset& a, const TimeOffset& b) {
196  return a.mOffsetCode == b.mOffsetCode;
197 }
198 
199 inline bool operator!=(const TimeOffset& a, const TimeOffset& b) {
200  return ! (a == b);
201 }
202 
203 }
204 
205 #endif
static TimeOffset forError()
Return an error indicator.
Definition: TimeOffset.h:110
TimeOffset()
Constructor.
Definition: TimeOffset.h:122
bool isError() const
Return true if this TimeOffset represents an error.
Definition: TimeOffset.h:155
int32_t toSeconds() const
Return the time offset as seconds.
Definition: TimeOffset.h:133
static TimeOffset forHour(int8_t hour)
Create TimeOffset with the corresponding hour offset.
Definition: TimeOffset.h:67
int16_t toMinutes() const
Return the time offset as minutes.
Definition: TimeOffset.h:128
static TimeOffset forOffsetCode(int8_t offsetCode)
Create TimeOffset from the offset code.
Definition: TimeOffset.h:117
static TimeOffset forHourMinute(int8_t hour, int8_t minute)
Create TimeOffset from (hour, minute) offset.
Definition: TimeOffset.h:79
A thin wrapper that represents a time offset from a reference point, usually 00:00 at UTC...
Definition: TimeOffset.h:58
static TimeOffset forMinutes(int16_t minutes)
Create TimeOffset from minutes from 00:00.
Definition: TimeOffset.h:88
bool isZero() const
Returns true if offset is 00:00.
Definition: TimeOffset.h:152
int8_t toOffsetCode() const
Return the time offset as the number of 15 minute increments.
Definition: TimeOffset.h:125
void toHourMinute(int8_t &hour, int8_t &minute) const
Extract hour and minute representation of the offset.
Definition: TimeOffset.h:142