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

LocalDate.cpp
1 /*
2 * MIT License
3 * Copyright (c) 2018 Brian T. Park
4 */
5
6 #include <Arduino.h>
7 #include <AceCommon.h> // printPad2To()
8 #include "common/DateStrings.h" // DateStrings
9 #include "LocalDate.h"
10
11 namespace ace_time {
12
13 // Offsets used to calculate the day of the week of a particular (year, month,
14 // day). The element represents the number of days that the first of month of
15 // the given index was shifted by the cummulative days from the previous months.
16 // To determine the "day of the week", we must normalize the resulting "day of
17 // the week" modulo 7.
18 //
19 // January is index 0, but we also use a modified year, where the year starts in
20 // March to make leap years easier to handle, so the shift for March=3 is 0.
21 //
22 // For example:
23 // * atc_days_of_week[3] is 3 because April (index=3) 1st is shifted by 3
24 // days because March has 31 days (28 + 3).
25 // * atc_days_of_week[4] is 5 because May (index=4) 1st is shifted by 2
26 // additional days from April, because April has 30 days (28 + 2).
27 const uint8_t LocalDate::sDayOfWeek[12] = {
28 5 /*Jan=31*/,
29 1 /*Feb=28*/,
30 0 /*Mar=31, start of "year"*/,
31 3 /*Apr=30*/,
32 5 /*May=31*/,
33 1 /*Jun=30*/,
34 3 /*Jul=31*/,
35 6 /*Aug=31*/,
36 2 /*Sep=30*/,
37 4 /*Oct=31*/,
38 0 /*Nov=30*/,
39 2 /*Dec=31*/,
40 };
41
42 // Using 0=Jan offset.
43 const uint8_t LocalDate::sDaysInMonth[12] = {
44 31 /*Jan=31*/,
45 28 /*Feb=28*/,
46 31 /*Mar=31*/,
47 30 /*Apr=30*/,
48 31 /*May=31*/,
49 30 /*Jun=30*/,
50 31 /*Jul=31*/,
51 31 /*Aug=31*/,
52 30 /*Sep=30*/,
53 31 /*Oct=31*/,
54 30 /*Nov=30*/,
55 31 /*Dec=31*/,
56 };
57
58 void LocalDate::printTo(Print& printer) const {
59 if (isError()) {
60 printer.print(F("<Invalid LocalDate>"));
61 return;
62 }
63
64 // Date
65 using ace_common::printPad2To;
66 printer.print(year());
67 printer.print('-');
68 printPad2To(printer, mMonth, '0');
69 printer.print('-');
70 printPad2To(printer, mDay, '0');
71 printer.print(' ');
72
73 // Week day
74 DateStrings ds;
75 printer.print(ds.dayOfWeekLongString(dayOfWeek()));
76 }
77
78 }
ace_time::DateStrings
Class that translates a numeric month (1-12) or dayOfWeek (1-7) into a human readable string.
Definition: DateStrings.h:26
ace_time::DateStrings::dayOfWeekLongString
const char * dayOfWeekLongString(uint8_t dayOfWeek)
Return the short dayOfWeek name.
Definition: DateStrings.h:56
ace_time::LocalDate::isError
bool isError() const
Return true if any component indicates an error condition.
Definition: LocalDate.h:337
ace_time::LocalDate::dayOfWeek
uint8_t dayOfWeek() const
Calculate the day of week given the (year, month, day).
Definition: LocalDate.h:324
ace_time::LocalDate::printTo
void printTo(Print &printer) const
Print LocalDate to 'printer' in ISO 8601 format, along with the day of week.
Definition: LocalDate.cpp:58
ace_time::LocalDate::year
int16_t year() const
Return the year.
Definition: LocalDate.h:301