AceTime  3.0.0
Date and time classes for Arduino that support timezones from the TZ Database.
LocalDateTime.cpp
1 /*
2  * MIT License
3  * Copyright (c) 2018 Brian T. Park
4  */
5 
6 #include <string.h> // strlen()
7 #include <Arduino.h> // strncpy_P()
8 #include <AceCommon.h>
9 #include "common/DateStrings.h"
10 #include "LocalDateTime.h"
11 
12 using ace_common::printPad2To;
13 
14 namespace ace_time {
15 
16 void LocalDateTime::printTo(Print& printer) const {
17  if (isError()) {
18  printer.print(F("<Invalid LocalDateTime>"));
19  return;
20  }
21 
22  // Date
23  printer.print(mLocalDate.year());
24  printer.print('-');
25  printPad2To(printer, mLocalDate.month(), '0');
26  printer.print('-');
27  printPad2To(printer, mLocalDate.day(), '0');
28 
29  // 'T' separator
30  printer.print('T');
31 
32  // Time
33  printPad2To(printer, mLocalTime.hour(), '0');
34  printer.print(':');
35  printPad2To(printer, mLocalTime.minute(), '0');
36  printer.print(':');
37  printPad2To(printer, mLocalTime.second(), '0');
38 }
39 
41  if (strlen(dateString) < kDateTimeStringLength) {
42  return LocalDateTime::forError();
43  }
44  return forDateStringChainable(dateString);
45 }
46 
48  const __FlashStringHelper* dateString) {
49  // Copy the F() string into a buffer. Use strncpy_P() because ESP32 and
50  // ESP8266 do not have strlcpy_P(). We need +1 for the '\0' character and
51  // another +1 to determine if the dateString is too long to fit.
52  char buffer[kDateTimeStringLength + 2];
53  strncpy_P(buffer, (const char*) dateString, sizeof(buffer));
54  buffer[kDateTimeStringLength + 1] = 0;
55 
56  // check if the original F() was too long
57  size_t len = strlen(buffer);
58  if (len > kDateTimeStringLength) {
59  return forError();
60  }
61 
62  return forDateString(buffer);
63 }
64 
66  const char* s = dateString;
67 
68  // date
70 
71  // 'T'
72  s++;
73 
74  // time
76 
77  dateString = s;
78  return LocalDateTime(ld, lt);
79 }
80 
81 }
Class that holds the date-time as the components (year, month, day, hour, minute, second) without reg...
Definition: LocalDateTime.h:30
void printTo(Print &printer) const
Print LocalDateTime to 'printer' in ISO 8601 format.
LocalDateTime()
Constructor.
bool isError() const
Return true if any component indicates an error condition.
static LocalDateTime forError()
Factory method that returns an instance where isError() returns true.
static LocalDateTime forDateString(const char *dateString)
Factory method.
static LocalDateTime forDateStringChainable(const char *&dateString)
Variant of forDateString() that updates the pointer to the next unprocessed character.
The date (year, month, day) representing the date without regards to time zone.
Definition: LocalDate.h:46
int16_t year() const
Return the year.
Definition: LocalDate.h:301
static LocalDate forDateStringChainable(const char *&dateString)
Variant of forDateString() that updates the pointer to the next unprocessed character.
Definition: LocalDate.h:260
uint8_t month() const
Return the month with January=1, December=12.
Definition: LocalDate.h:307
uint8_t day() const
Return the day of the month.
Definition: LocalDate.h:313
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
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