AceTime  1.11.7
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 <AceCommon.h>
7 #include "LocalTime.h"
8 
9 using ace_common::printPad2To;
10 
11 namespace ace_time {
12 
13 void LocalTime::printTo(Print& printer) const {
14  if (isError()) {
15  printer.print(F("<Invalid LocalTime>"));
16  return;
17  }
18 
19  // Time
20  printPad2To(printer, mHour, '0');
21  printer.print(':');
22  printPad2To(printer, mMinute, '0');
23  printer.print(':');
24  printPad2To(printer, mSecond, '0');
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 }
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:35
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:27
void printTo(Print &printer) const
Print LocalTime to 'printer' in ISO 8601 format.
Definition: LocalTime.cpp:13
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