AceTime  0.7
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 mMinutes and it seemed inefficient to
16 // go through the factory method and assignment operator, so I expose
17 // setMinutes() 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 
61 class TimeOffset {
62  public:
64  static const int8_t kErrorCode = INT8_MIN;
65 
67  static const int16_t kErrorMinutes = INT16_MIN;
68 
73  static TimeOffset forHours(int8_t hour) {
74  return TimeOffset::forMinutes(hour * 60);
75  }
76 
85  static TimeOffset forHourMinute(int8_t hour, int8_t minute) {
86  int16_t minutes = hour * 60 + minute;
87  return TimeOffset(minutes);
88  }
89 
91  static TimeOffset forMinutes(int16_t minutes) {
92  return TimeOffset(minutes);
93  }
94 
100  static TimeOffset forOffsetString(const char* offsetString);
101 
110  static TimeOffset forOffsetStringChainable(const char*& offsetString);
111 
113  static TimeOffset forError() { return TimeOffset(kErrorMinutes); }
114 
120  static TimeOffset forOffsetCode(int8_t offsetCode) {
121  return TimeOffset::forMinutes(
122  (offsetCode == kErrorCode) ? kErrorMinutes : offsetCode * 15);
123  }
124 
126  explicit TimeOffset() {}
127 
129  int8_t toOffsetCode() const { return mMinutes / 15; }
130 
132  int16_t toMinutes() const {
133  return mMinutes;
134  }
135 
137  int32_t toSeconds() const {
138  return (int32_t) 60 * toMinutes();
139  }
140 
146  void toHourMinute(int8_t& hour, int8_t& minute) const {
147  hour = mMinutes / 60;
148  minute = mMinutes % 60;
149  }
150 
156  bool isZero() const { return mMinutes == 0; }
157 
159  bool isError() const {
160  return mMinutes == kErrorMinutes;
161  }
162 
164  void printTo(Print& printer) const;
165 
166  // Use default copy constructor and assignment operator.
167  TimeOffset(const TimeOffset&) = default;
168  TimeOffset& operator=(const TimeOffset&) = default;
169 
170  private:
171  friend bool operator==(const TimeOffset& a, const TimeOffset& b);
172 
173  // Give access to setMinutes()
174  friend void time_offset_mutation::incrementHour(TimeOffset& offset);
175  friend void time_offset_mutation::increment15Minutes(TimeOffset& offset);
176 
178  static const uint8_t kTimeOffsetStringLength = 6;
179 
181  explicit TimeOffset(int16_t minutes):
182  mMinutes(minutes) {}
183 
185  void setMinutes(int16_t minutes) {
186  mMinutes = minutes;
187  }
188 
195  int16_t mMinutes = 0;
196 };
197 
198 inline bool operator==(const TimeOffset& a, const TimeOffset& b) {
199  return a.mMinutes == b.mMinutes;
200 }
201 
202 inline bool operator!=(const TimeOffset& a, const TimeOffset& b) {
203  return ! (a == b);
204 }
205 
206 }
207 
208 #endif
static TimeOffset forError()
Return an error indicator.
Definition: TimeOffset.h:113
TimeOffset()
Constructor.
Definition: TimeOffset.h:126
bool isError() const
Return true if this TimeOffset represents an error.
Definition: TimeOffset.h:159
int32_t toSeconds() const
Return the time offset as seconds.
Definition: TimeOffset.h:137
int16_t toMinutes() const
Return the time offset as minutes.
Definition: TimeOffset.h:132
static TimeOffset forOffsetCode(int8_t offsetCode)
Create TimeOffset from the offset code.
Definition: TimeOffset.h:120
static TimeOffset forHourMinute(int8_t hour, int8_t minute)
Create TimeOffset from (hour, minute) offset.
Definition: TimeOffset.h:85
A thin wrapper that represents a time offset from a reference point, usually 00:00 at UTC...
Definition: TimeOffset.h:61
static TimeOffset forMinutes(int16_t minutes)
Create TimeOffset from minutes from 00:00.
Definition: TimeOffset.h:91
bool isZero() const
Returns true if offset is 00:00.
Definition: TimeOffset.h:156
int8_t toOffsetCode() const
Return the time offset as the number of 15 minute increments.
Definition: TimeOffset.h:129
void toHourMinute(int8_t &hour, int8_t &minute) const
Extract hour and minute representation of the offset.
Definition: TimeOffset.h:146
static TimeOffset forHours(int8_t hour)
Create TimeOffset with the corresponding hour offset.
Definition: TimeOffset.h:73