AceTime  0.1
Date and time classes for Arduino that supports the TZ DAtabase, and a system clock synchronized 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.weekDayLongString(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  int8_t yearTiny = year - kEpochYear;
78 
79  // '-'
80  s++;
81 
82  // month
83  uint8_t month = (*s++ - '0');
84  month = 10 * month + (*s++ - '0');
85 
86  // '-'
87  s++;
88 
89  // day
90  uint8_t day = (*s++ - '0');
91  day = 10 * day + (*s++ - '0');
92 
93  dateString = s;
94  return LocalDate(yearTiny, month, day);
95 }
96 
97 }
static LocalDate forError()
Factory method that returns a LocalDate which represents an error condition.
Definition: LocalDate.h:184
LocalDate()
Default constructor does nothing.
Definition: LocalDate.h:200
static LocalDate forDateString(const char *dateString)
Factory method.
Definition: LocalDate.cpp:62
uint8_t month() const
Return the month with January=1, December=12.
Definition: LocalDate.h:215
bool isError() const
Return true if any component indicates an error condition.
Definition: LocalDate.h:242
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
int16_t year() const
Return the full year instead of just the last 2 digits.
Definition: LocalDate.h:203
uint8_t day() const
Return the day of the month.
Definition: LocalDate.h:221
The date (year, month, day) representing the date without regards to time zone.
Definition: LocalDate.h:32
static const int16_t kEpochYear
Base year of epoch.
Definition: LocalDate.h:35
uint8_t dayOfWeek() const
Calculate the day of week given the (year, month, day).
Definition: LocalDate.h:232
int8_t yearTiny() const
Return the single-byte year offset from year 2000.
Definition: LocalDate.h:209