AceTime  1.9.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 <AceCommon.h>
7 #include "common/DateStrings.h"
8 #include "LocalDate.h"
9 
10 using ace_common::printPad2To;
11 
12 namespace ace_time {
13 
14 // Using 0=Jan offset.
15 const uint8_t LocalDate::sDayOfWeek[12] = {
16  5 /*Jan=31*/,
17  1 /*Feb=28*/,
18  0 /*Mar=31, start of "year"*/,
19  3 /*Apr=30*/,
20  5 /*May=31*/,
21  1 /*Jun=30*/,
22  3 /*Jul=31*/,
23  6 /*Aug=31*/,
24  2 /*Sep=30*/,
25  4 /*Oct=31*/,
26  0 /*Nov=30*/,
27  2 /*Dec=31*/,
28 };
29 
30 // Using 0=Jan offset.
31 const uint8_t LocalDate::sDaysInMonth[12] = {
32  31 /*Jan=31*/,
33  28 /*Feb=28*/,
34  31 /*Mar=31*/,
35  30 /*Apr=30*/,
36  31 /*May=31*/,
37  30 /*Jun=30*/,
38  31 /*Jul=31*/,
39  31 /*Aug=31*/,
40  30 /*Sep=30*/,
41  31 /*Oct=31*/,
42  30 /*Nov=30*/,
43  31 /*Dec=31*/,
44 };
45 
46 void LocalDate::printTo(Print& printer) const {
47  if (isError()) {
48  printer.print(F("<Invalid LocalDate>"));
49  return;
50  }
51 
52  // Date
53  printer.print(year());
54  printer.print('-');
55  printPad2To(printer, mMonth, '0');
56  printer.print('-');
57  printPad2To(printer, mDay, '0');
58  printer.print(' ');
59 
60  // Week day
61  DateStrings ds;
62  printer.print(ds.dayOfWeekLongString(dayOfWeek()));
63 }
64 
65 LocalDate LocalDate::forDateString(const char* dateString) {
66  if (strlen(dateString) < kDateStringLength) {
67  return forError();
68  }
69  return forDateStringChainable(dateString);
70 }
71 
72 LocalDate LocalDate::forDateStringChainable(const char*& dateString) {
73  const char* s = dateString;
74 
75  // year (assumes 4 digit year)
76  int16_t year = (*s++ - '0');
77  year = 10 * year + (*s++ - '0');
78  year = 10 * year + (*s++ - '0');
79  year = 10 * year + (*s++ - '0');
80 
81  // '-'
82  s++;
83 
84  // month
85  uint8_t month = (*s++ - '0');
86  month = 10 * month + (*s++ - '0');
87 
88  // '-'
89  s++;
90 
91  // day
92  uint8_t day = (*s++ - '0');
93  day = 10 * day + (*s++ - '0');
94 
95  dateString = s;
96  return forComponents(year, month, day);
97 }
98 
99 }
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:46
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:72
ace_time::LocalDate::forDateString
static LocalDate forDateString(const char *dateString)
Factory method.
Definition: LocalDate.cpp:65
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