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 
28  if (strlen(dateString) < kDateStringLength) {
29  return forError();
30  }
31  return forDateStringChainable(dateString);
32 }
33 
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 
53  const char* s = dateString;
54 
57 
58  dateString = s;
59  return OffsetDateTime(ldt, offset);
60 }
61 
62 }
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.
static LocalDateTime forDateStringChainable(const char *&dateString)
Variant of forDateString() that updates the pointer to the next unprocessed character.
The date (year, month, day), time (hour, minute, second) and fixed offset from UTC (timeOffset).
static OffsetDateTime forDateStringChainable(const char *&dateString)
Variant of forDateString() that updates the pointer to the next unprocessed character.
bool isError() const
Return true if any component indicates an error condition.
OffsetDateTime()
Constructor.
void printTo(Print &printer) const
Print OffsetDateTime to 'printer' in ISO 8601 format.
static OffsetDateTime forError()
Factory method that returns an instance whose isError() is true.
static OffsetDateTime forDateString(const char *dateString)
Factory method.
A thin wrapper that represents a time offset from a reference point, usually 00:00 at UTC,...
Definition: TimeOffset.h:56
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
static TimeOffset forOffsetStringChainable(const char *&offsetString)
Variant of forOffsetString() that updates the string pointer to the next unprocessed character.
Definition: TimeOffset.cpp:48