AceTime  0.5.2
Date and time classes for Arduino that support timezones from the TZ Database, and a system clock that can synchronize from an NTP server or an RTC chip.
LocalDate.cpp
1 /*
2  * MIT License
3  * Copyright (c) 2018 Brian T. Park
4  */
5 
6 #include "common/util.h"
7 #include "common/DateStrings.h"
8 #include "LocalDate.h"
9 
10 namespace ace_time {
11 
12 using common::printPad2;
13 using common::printPad3;
14 using common::DateStrings;
15 
16 // Using 0=Jan offset.
17 const uint8_t LocalDate::sDayOfWeek[12] = {
18  5 /*Jan=31*/,
19  1 /*Feb=28*/,
20  0 /*Mar=31, start of "year"*/,
21  3 /*Apr=30*/,
22  5 /*May=31*/,
23  1 /*Jun=30*/,
24  3 /*Jul=31*/,
25  6 /*Aug=31*/,
26  2 /*Sep=30*/,
27  4 /*Oct=31*/,
28  0 /*Nov=30*/,
29  2 /*Dec=31*/,
30 };
31 
32 // Using 0=Jan offset.
33 const uint8_t LocalDate::sDaysInMonth[12] = {
34  31 /*Jan=31*/,
35  28 /*Feb=28*/,
36  31 /*Mar=31*/,
37  30 /*Apr=30*/,
38  31 /*May=31*/,
39  30 /*Jun=30*/,
40  31 /*Jul=31*/,
41  31 /*Aug=31*/,
42  30 /*Sep=30*/,
43  31 /*Oct=31*/,
44  30 /*Nov=30*/,
45  31 /*Dec=31*/,
46 };
47 
48 void LocalDate::printTo(Print& printer) const {
49  if (isError()) {
50  printer.print(F("<Invalid LocalDate>"));
51  return;
52  }
53 
54  // Date
55  printer.print(year());
56  printer.print('-');
57  printPad2(printer, mMonth);
58  printer.print('-');
59  printPad2(printer, mDay);
60  printer.print(' ');
61 
62  // Week day
63  DateStrings ds;
64  printer.print(ds.dayOfWeekLongString(dayOfWeek()));
65 }
66 
67 LocalDate LocalDate::forDateString(const char* dateString) {
68  if (strlen(dateString) < kDateStringLength) {
69  return forError();
70  }
71  return forDateStringChainable(dateString);
72 }
73 
74 LocalDate LocalDate::forDateStringChainable(const char*& dateString) {
75  const char* s = dateString;
76 
77  // year (assumes 4 digit year)
78  int16_t year = (*s++ - '0');
79  year = 10 * year + (*s++ - '0');
80  year = 10 * year + (*s++ - '0');
81  year = 10 * year + (*s++ - '0');
82 
83  // '-'
84  s++;
85 
86  // month
87  uint8_t month = (*s++ - '0');
88  month = 10 * month + (*s++ - '0');
89 
90  // '-'
91  s++;
92 
93  // day
94  uint8_t day = (*s++ - '0');
95  day = 10 * day + (*s++ - '0');
96 
97  dateString = s;
98  return forComponents(year, month, day);
99 }
100 
101 }
Class that translates a numeric month (1-12) or dayOfWeek (1-7) into a human readable string...
Definition: DateStrings.h:27
static LocalDate forError()
Factory method that returns a LocalDate which represents an error condition.
Definition: LocalDate.h:194
static LocalDate forDateString(const char *dateString)
Factory method.
Definition: LocalDate.cpp:67
void printTo(Print &printer) const
Print LocalDate to &#39;printer&#39; in ISO 8601 format, along with the day of week.
Definition: LocalDate.cpp:48
static LocalDate forDateStringChainable(const char *&dateString)
Variant of forDateString() that updates the pointer to the next unprocessed character.
Definition: LocalDate.cpp:74
bool isError() const
Return true if any component indicates an error condition.
Definition: LocalDate.h:258
uint8_t day() const
Return the day of the month.
Definition: LocalDate.h:237
static LocalDate forComponents(int16_t year, uint8_t month, uint8_t day)
Factory method using separated year, month and day fields.
Definition: LocalDate.h:94
The date (year, month, day) representing the date without regards to time zone.
Definition: LocalDate.h:36
const char * dayOfWeekLongString(uint8_t dayOfWeek)
Return the short dayOfWeek name.
Definition: DateStrings.h:57
uint8_t month() const
Return the month with January=1, December=12.
Definition: LocalDate.h:231
int16_t year() const
Return the full year instead of just the last 2 digits.
Definition: LocalDate.h:219
uint8_t dayOfWeek() const
Calculate the day of week given the (year, month, day).
Definition: LocalDate.h:248