AceTime 2.4.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
40 LocalDateTime LocalDateTime::forDateString(const char* dateString) {
41 if (strlen(dateString) < kDateTimeStringLength) {
42 return LocalDateTime::forError();
43 }
44 return forDateStringChainable(dateString);
45 }
46
47 LocalDateTime LocalDateTime::forDateString(
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
65 LocalDateTime LocalDateTime::forDateStringChainable(const char*& dateString) {
66 const char* s = dateString;
67
68 // date
69 LocalDate ld = LocalDate::forDateStringChainable(s);
70
71 // 'T'
72 s++;
73
74 // time
75 LocalTime lt = LocalTime::forTimeStringChainable(s);
76
77 dateString = s;
78 return LocalDateTime(ld, lt);
79 }
80
81 }
ace_time::LocalDateTime
Class that holds the date-time as the components (year, month, day, hour, minute, second) without reg...
Definition: LocalDateTime.h:30
ace_time::LocalDateTime::printTo
void printTo(Print &printer) const
Print LocalDateTime to 'printer' in ISO 8601 format.
Definition: LocalDateTime.cpp:16
ace_time::LocalDateTime::LocalDateTime
LocalDateTime()
Constructor.
Definition: LocalDateTime.h:148
ace_time::LocalDateTime::isError
bool isError() const
Return true if any component indicates an error condition.
Definition: LocalDateTime.h:151
ace_time::LocalDateTime::forError
static LocalDateTime forError()
Factory method that returns an instance where isError() returns true.
Definition: LocalDateTime.h:143
ace_time::LocalDateTime::forDateString
static LocalDateTime forDateString(const char *dateString)
Factory method.
Definition: LocalDateTime.cpp:40
ace_time::LocalDateTime::forDateStringChainable
static LocalDateTime forDateStringChainable(const char *&dateString)
Variant of forDateString() that updates the pointer to the next unprocessed character.
Definition: LocalDateTime.cpp:65
ace_time::LocalDate
The date (year, month, day) representing the date without regards to time zone.
Definition: LocalDate.h:46
ace_time::LocalDate::year
int16_t year() const
Return the year.
Definition: LocalDate.h:301
ace_time::LocalDate::forDateStringChainable
static LocalDate forDateStringChainable(const char *&dateString)
Variant of forDateString() that updates the pointer to the next unprocessed character.
Definition: LocalDate.h:260
ace_time::LocalDate::month
uint8_t month() const
Return the month with January=1, December=12.
Definition: LocalDate.h:307
ace_time::LocalDate::day
uint8_t day() const
Return the day of the month.
Definition: LocalDate.h:313
ace_time::LocalTime
The time (hour, minute, second) fields representing the time without regards to the day or the time z...
Definition: LocalTime.h:27
ace_time::LocalTime::forTimeStringChainable
static LocalTime forTimeStringChainable(const char *&timeString)
Variant of forTimeString() that updates the pointer to the next unprocessed character.
Definition: LocalTime.cpp:36
ace_time::LocalTime::hour
uint8_t hour() const
Return the hour.
Definition: LocalTime.h:118
ace_time::LocalTime::minute
uint8_t minute() const
Return the minute.
Definition: LocalTime.h:124
ace_time::LocalTime::second
uint8_t second() const
Return the second.
Definition: LocalTime.h:130