AceTime 3.0.0 Date and time classes for Arduino that support timezones from the TZ Database.

OffsetDateTime.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 "LocalDateTime.h"
9 #include "OffsetDateTime.h"
10 #include "TimeOffset.h"
11
12 namespace ace_time {
13
14 void OffsetDateTime::printTo(Print& printer) const {
15 if (isError()) {
16 printer.print(F("<Invalid OffsetDateTime>"));
17 return;
18 }
19
20 // LocalDateTime
21 mLocalDateTime.printTo(printer);
22
23 // TimeOffset "+/-hh:mm
24 mTimeOffset.printTo(printer);
25 }
26
27 OffsetDateTime OffsetDateTime::forDateString(const char* dateString) {
28 if (strlen(dateString) < kDateStringLength) {
29 return forError();
30 }
31 return forDateStringChainable(dateString);
32 }
33
34 OffsetDateTime OffsetDateTime::forDateString(
35 const __FlashStringHelper* dateString) {
36 // Copy the F() string into a buffer. Use strncpy_P() because ESP32 and
37 // ESP8266 do not have strlcpy_P(). We need +1 for the '\0' character and
38 // another +1 to determine if the dateString is too long to fit.
39 char buffer[kDateStringLength + 2];
40 strncpy_P(buffer, (const char*) dateString, sizeof(buffer));
41 buffer[kDateStringLength + 1] = 0;
42
43 // check if the original F() was too long
44 size_t len = strlen(buffer);
45 if (len > kDateStringLength) {
46 return forError();
47 }
48
49 return forDateString(buffer);
50 }
51
52 OffsetDateTime OffsetDateTime::forDateStringChainable(const char*& dateString) {
53 const char* s = dateString;
54
55 LocalDateTime ldt = LocalDateTime::forDateStringChainable(s);
56 TimeOffset offset = TimeOffset::forOffsetStringChainable(s);
57
58 dateString = s;
59 return OffsetDateTime(ldt, offset);
60 }
61
62 }
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::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::OffsetDateTime
The date (year, month, day), time (hour, minute, second) and fixed offset from UTC (timeOffset).
Definition: OffsetDateTime.h:37
ace_time::OffsetDateTime::forDateStringChainable
static OffsetDateTime forDateStringChainable(const char *&dateString)
Variant of forDateString() that updates the pointer to the next unprocessed character.
Definition: OffsetDateTime.cpp:52
ace_time::OffsetDateTime::isError
bool isError() const
Return true if any component indicates an error condition.
Definition: OffsetDateTime.h:150
ace_time::OffsetDateTime::OffsetDateTime
OffsetDateTime()
Constructor.
Definition: OffsetDateTime.h:147
ace_time::OffsetDateTime::printTo
void printTo(Print &printer) const
Print OffsetDateTime to 'printer' in ISO 8601 format.
Definition: OffsetDateTime.cpp:14
ace_time::OffsetDateTime::forError
static OffsetDateTime forError()
Factory method that returns an instance whose isError() is true.
Definition: OffsetDateTime.h:142
ace_time::OffsetDateTime::forDateString
static OffsetDateTime forDateString(const char *dateString)
Factory method.
Definition: OffsetDateTime.cpp:27
ace_time::TimeOffset
A thin wrapper that represents a time offset from a reference point, usually 00:00 at UTC,...
Definition: TimeOffset.h:56
ace_time::TimeOffset::printTo
void printTo(Print &printer) const
Print the human readable string, including a "-" or "+" prefix, in the form of "+/-hh:mm" or "+/-hh:m...
Definition: TimeOffset.cpp:15
ace_time::TimeOffset::forOffsetStringChainable
static TimeOffset forOffsetStringChainable(const char *&offsetString)
Variant of forOffsetString() that updates the string pointer to the next unprocessed character.
Definition: TimeOffset.cpp:48