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