AceTime  0.1
Date and time classes for Arduino that supports the TZ DAtabase, and a system clock synchronized 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:
59  static TimeOffset forHour(int8_t hour) {
60  return TimeOffset(hour * 4);
61  }
62 
69  static TimeOffset forHourMinute(int8_t hour, uint8_t minute) {
70  int8_t uhour = (hour < 0) ? -hour : hour;
71  uint8_t code = uhour * 4 + minute / 15;
72  return (hour < 0) ? TimeOffset(-code) : TimeOffset(code);
73  }
74 
79  static TimeOffset forMinutes(int16_t minutes) {
80  return TimeOffset(minutes / 15);
81  }
82 
87  static TimeOffset forOffsetString(const char* offsetString);
88 
90  static TimeOffset forError() { return TimeOffset(kErrorCode); }
91 
97  static TimeOffset forOffsetCode(int8_t offsetCode) {
98  return TimeOffset(offsetCode);
99  }
100 
102  explicit TimeOffset() {}
103 
109  bool isZero() const { return mOffsetCode == 0; }
110 
112  int8_t toOffsetCode() const { return mOffsetCode; }
113 
115  int16_t toMinutes() const {
116  return (int16_t) 15 * mOffsetCode;
117  }
118 
120  int32_t toSeconds() const {
121  return (int32_t) 60 * toMinutes();
122  }
123 
125  void toHourMinute(int8_t& hour, uint8_t& minute) const {
126  uint8_t code = (mOffsetCode < 0) ? -mOffsetCode : mOffsetCode;
127  hour = code / 4;
128  hour = (mOffsetCode < 0) ? -hour : hour;
129  minute = (code & 0x03) * 15;
130  }
131 
133  bool isError() const {
134  return mOffsetCode == kErrorCode;
135  }
136 
138  void printTo(Print& printer) const;
139 
140  // Use default copy constructor and assignment operator.
141  TimeOffset(const TimeOffset&) = default;
142  TimeOffset& operator=(const TimeOffset&) = default;
143 
144  private:
145  friend class BasicZoneSpecifier;
146  friend class ManualZoneSpecifier;
147  friend class TimeZone;
148  friend class OffsetDateTime; // forOffsetStringChainable()
149  friend class TimeOffsetMutator;
150  friend bool operator==(const TimeOffset& a, const TimeOffset& b);
151  friend void time_offset_mutation::incrementHour(TimeOffset& offset);
152  friend void time_offset_mutation::increment15Minutes(TimeOffset& offset);
153 
155  static const int8_t kErrorCode = -128;
156 
158  static const uint8_t kTimeOffsetStringLength = 6;
159 
167  static TimeOffset forOffsetStringChainable(const char*& offsetString);
168 
170  explicit TimeOffset(int8_t offsetCode):
171  mOffsetCode(offsetCode) {}
172 
174  void setOffsetCode(int8_t offsetCode) { mOffsetCode = offsetCode; }
175 
186  int8_t mOffsetCode = 0;
187 };
188 
189 inline bool operator==(const TimeOffset& a, const TimeOffset& b) {
190  return a.mOffsetCode == b.mOffsetCode;
191 }
192 
193 inline bool operator!=(const TimeOffset& a, const TimeOffset& b) {
194  return ! (a == b);
195 }
196 
197 }
198 
199 #endif
static TimeOffset forError()
Return an error indicator.
Definition: TimeOffset.h:90
TimeOffset()
Constructor.
Definition: TimeOffset.h:102
int8_t toOffsetCode() const
Return the time offset as the number of 15 minute increments.
Definition: TimeOffset.h:112
void toHourMinute(int8_t &hour, uint8_t &minute) const
Extract hour and minute representation of the offset.
Definition: TimeOffset.h:125
bool isError() const
Return true if this TimeOffset represents an error.
Definition: TimeOffset.h:133
static TimeOffset forHour(int8_t hour)
Create TimeOffset with the corresponding hour offset.
Definition: TimeOffset.h:59
An implementation of ZoneSpecifier which allows the user to manually adjust the UTC offset and the DS...
static TimeOffset forHourMinute(int8_t hour, uint8_t minute)
Create TimeOffset from (hour, minute) offset, where the sign of hour determines the sign of the offse...
Definition: TimeOffset.h:69
static TimeOffset forOffsetCode(int8_t offsetCode)
Create TimeOffset from the offset code.
Definition: TimeOffset.h:97
int16_t toMinutes() const
Return the time offset as minutes.
Definition: TimeOffset.h:115
The date (year, month, day) and time (hour, minute, second) fields representing the time with an offs...
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:79
Class that describes a time zone.
Definition: TimeZone.h:50
int32_t toSeconds() const
Return the time offset as seconds.
Definition: TimeOffset.h:120
bool isZero() const
Returns true if offset is 00:00.
Definition: TimeOffset.h:109
An implementation of ZoneSpecifier that supports a subset of the zones containing in the TZ Database...