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