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.
LocalTime.h
1 #ifndef ACE_TIME_LOCAL_TIME_H
2 #define ACE_TIME_LOCAL_TIME_H
3 
4 #include <stdint.h>
5 #include "common/common.h"
6 
7 class Print;
8 
9 namespace ace_time {
10 
20 class LocalTime {
21  public:
23  static const acetime_t kInvalidSeconds = INT32_MIN;
24 
35  static LocalTime forComponents(uint8_t hour, uint8_t minute,
36  uint8_t second) {
37  return LocalTime(hour, minute, second);
38  }
39 
48  static LocalTime forSeconds(acetime_t seconds) {
49  uint8_t second, minute, hour;
50 
51  if (seconds == kInvalidSeconds) {
52  second = minute = hour = kInvalidValue; // causes isError() to be true
53  } else {
54  second = seconds % 60;
55  uint16_t minutes = seconds / 60;
56  minute = minutes % 60;
57  hour = minutes / 60;
58  }
59 
60  // Return a single object to allow return value optimization.
61  return LocalTime(hour, minute, second);
62  }
63 
70  static LocalTime forTimeString(const char* timeString);
71 
76  static LocalTime forError() {
77  return LocalTime(kInvalidValue, kInvalidValue, kInvalidValue);
78  }
79 
81  explicit LocalTime() {}
82 
89  bool isError() const {
90  if (mSecond >= 60) return true;
91  if (mMinute >= 60) return true;
92  if (mHour == 24) {
93  return mSecond != 0 || mMinute != 0;
94  }
95  return mHour > 24;
96  }
97 
99  uint8_t hour() const { return mHour; }
100 
102  void hour(uint8_t hour) { mHour = hour; }
103 
105  uint8_t minute() const { return mMinute; }
106 
108  void minute(uint8_t month) { mMinute = month; }
109 
111  uint8_t second() const { return mSecond; }
112 
114  void second(uint8_t second) { mSecond = second; }
115 
120  acetime_t toSeconds() const {
121  if (isError()) {
122  return kInvalidSeconds;
123  } else {
124  return ((mHour * (int16_t) 60) + mMinute)
125  * (int32_t) 60 + mSecond;
126  }
127  }
128 
134  int8_t compareTo(const LocalTime& that) const {
135  if (mHour < that.mHour) return -1;
136  if (mHour > that.mHour) return 1;
137  if (mMinute < that.mMinute) return -1;
138  if (mMinute > that.mMinute) return 1;
139  if (mSecond < that.mSecond) return -1;
140  if (mSecond > that.mSecond) return 1;
141  return 0;
142  }
143 
148  void printTo(Print& printer) const;
149 
150  // Use default copy constructor and assignment operator.
151  LocalTime(const LocalTime&) = default;
152  LocalTime& operator=(const LocalTime&) = default;
153 
154  private:
155  friend class LocalDateTime;
156  friend class OffsetDateTime;
157  friend bool operator==(const LocalTime& a, const LocalTime& b);
158 
160  static const uint8_t kTimeStringLength = 8;
161 
163  static const uint8_t kInvalidValue = UINT8_MAX;
164 
172  static LocalTime forTimeStringChainable(const char*& timeString);
173 
174  explicit LocalTime(uint8_t hour, uint8_t minute, uint8_t second):
175  mHour(hour),
176  mMinute(minute),
177  mSecond(second) {}
178 
179  uint8_t mHour; // [0, 23]
180  uint8_t mMinute; // [0, 59]
181  uint8_t mSecond; // [0, 59]
182 };
183 
185 inline bool operator==(const LocalTime& a, const LocalTime& b) {
186  return a.mSecond == b.mSecond
187  && a.mMinute == b.mMinute
188  && a.mHour == b.mHour;
189 }
190 
192 inline bool operator!=(const LocalTime& a, const LocalTime& b) {
193  return ! (a == b);
194 }
195 
196 }
197 
198 #endif
void second(uint8_t second)
Set the second.
Definition: LocalTime.h:114
void minute(uint8_t month)
Set the minute.
Definition: LocalTime.h:108
void hour(uint8_t hour)
Set the hour.
Definition: LocalTime.h:102
static const acetime_t kInvalidSeconds
An invalid seconds marker that indicates isError() true.
Definition: LocalTime.h:23
The time (hour, minute, second) fields representing the time without regards to the day or the time z...
Definition: LocalTime.h:20
acetime_t toSeconds() const
Return the number of seconds since midnight.
Definition: LocalTime.h:120
The date (year, month, day) and time (hour, minute, second) fields representing the time with an offs...
void printTo(Print &printer) const
Print LocalTime to &#39;printer&#39; in ISO 8601 format.
Definition: LocalTime.cpp:8
static LocalTime forError()
Factory method that returns an instance which indicates an error condition.
Definition: LocalTime.h:76
static LocalTime forComponents(uint8_t hour, uint8_t minute, uint8_t second)
Factory method using separated date, time, and time zone fields.
Definition: LocalTime.h:35
static LocalTime forTimeString(const char *timeString)
Factory method.
Definition: LocalTime.cpp:22
uint8_t hour() const
Return the hour.
Definition: LocalTime.h:99
uint8_t second() const
Return the second.
Definition: LocalTime.h:111
bool isError() const
Return true if any component is outside the normal time range of 00:00:00 to 23:59:59.
Definition: LocalTime.h:89
uint8_t minute() const
Return the minute.
Definition: LocalTime.h:105
LocalTime()
Default constructor does nothing.
Definition: LocalTime.h:81
friend bool operator==(const LocalTime &a, const LocalTime &b)
Return true if two LocalTime objects are equal.
Definition: LocalTime.h:185
static LocalTime forSeconds(acetime_t seconds)
Factory method.
Definition: LocalTime.h:48
int8_t compareTo(const LocalTime &that) const
Compare this LocalTime with that LocalTime, and return (<0, 0, >0) according to whether (this<that...
Definition: LocalTime.h:134