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