AceTime  0.8
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.
LocalTime.cpp
1 /*
2  * MIT License
3  * Copyright (c) 2018 Brian T. Park
4  */
5 
6 #include "common/util.h"
7 #include "LocalTime.h"
8 
9 namespace ace_time {
10 
11 using common::printPad2;
12 
13 void LocalTime::printTo(Print& printer) const {
14  if (isError()) {
15  printer.print(F("<Invalid LocalTime>"));
16  return;
17  }
18 
19  // Time
20  printPad2(printer, mHour);
21  printer.print(':');
22  printPad2(printer, mMinute);
23  printer.print(':');
24  printPad2(printer, mSecond);
25 }
26 
27 LocalTime LocalTime::forTimeString(const char* timeString) {
28  if (strlen(timeString) < kTimeStringLength) {
29  return forError();
30  }
31  return forTimeStringChainable(timeString);
32 }
33 
34 // This assumes that the dateString is always long enough.
35 LocalTime LocalTime::forTimeStringChainable(const char*& timeString) {
36  const char* s = timeString;
37 
38  // hour
39  uint8_t hour = (*s++ - '0');
40  hour = 10 * hour + (*s++ - '0');
41 
42  // ':'
43  s++;
44 
45  // minute
46  uint8_t minute = (*s++ - '0');
47  minute = 10 * minute + (*s++ - '0');
48 
49  // ':'
50  s++;
51 
52  // second
53  uint8_t second = (*s++ - '0');
54  second = 10 * second + (*s++ - '0');
55 
56  timeString = s;
57  return LocalTime(hour, minute, second);
58 }
59 
60 }
void printTo(Print &printer) const
Print LocalTime to &#39;printer&#39; in ISO 8601 format.
Definition: LocalTime.cpp:13
The time (hour, minute, second) fields representing the time without regards to the day or the time z...
Definition: LocalTime.h:26
static LocalTime forTimeStringChainable(const char *&timeString)
Variant of forTimeString() that updates the pointer to the next unprocessed character.
Definition: LocalTime.cpp:35
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:106
static LocalTime forError()
Factory method that returns an instance which indicates an error condition.
Definition: LocalTime.h:93
uint8_t hour() const
Return the hour.
Definition: LocalTime.h:116
static LocalTime forTimeString(const char *timeString)
Factory method.
Definition: LocalTime.cpp:27
uint8_t minute() const
Return the minute.
Definition: LocalTime.h:122
uint8_t second() const
Return the second.
Definition: LocalTime.h:128
LocalTime()
Default constructor does nothing.
Definition: LocalTime.h:98