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.
|
The date (year, month, day) representing the date without regards to time zone. More...
#include <LocalDate.h>
Public Member Functions | |
LocalDate () | |
Default constructor does nothing. More... | |
int16_t | year () const |
Return the full year instead of just the last 2 digits. More... | |
void | year (int16_t year) |
Set the year given the full year. More... | |
int8_t | yearTiny () const |
Return the single-byte year offset from year 2000. More... | |
void | yearTiny (int8_t yearTiny) |
Set the single-byte year offset from year 2000. More... | |
uint8_t | month () const |
Return the month with January=1, December=12. More... | |
void | month (uint8_t month) |
Set the month. More... | |
uint8_t | day () const |
Return the day of the month. More... | |
void | day (uint8_t day) |
Set the day of the month. More... | |
uint8_t | dayOfWeek () const |
Calculate the day of week given the (year, month, day). More... | |
bool | isError () const |
Return true if any component indicates an error condition. More... | |
acetime_t | toEpochDays () const |
Return number of days since AceTime epoch (2000-01-01 00:00:00Z). More... | |
acetime_t | toUnixDays () const |
Return the number of days since Unix epoch (1970-01-01 00:00:00). More... | |
acetime_t | toEpochSeconds () const |
Return the number of seconds since AceTime epoch (2000-01-01 00:00:00). More... | |
acetime_t | toUnixSeconds () const |
Return the number of seconds since Unix epoch (1970-01-01 00:00:00). | |
int8_t | compareTo (const LocalDate &that) const |
Compare this LocalDate to that LocalDate, returning (<0, 0, >0) according to whether the epochSeconds is (this<that, this==this, this>that). More... | |
void | printTo (Print &printer) const |
Print LocalDate to 'printer' in ISO 8601 format, along with the day of week. More... | |
LocalDate (const LocalDate &)=default | |
LocalDate & | operator= (const LocalDate &)=default |
Static Public Member Functions | |
static LocalDate | forComponents (int16_t year, uint8_t month, uint8_t day) |
Factory method using separated year, month and day fields. More... | |
static LocalDate | forEpochDays (acetime_t epochDays) |
Factory method using the number of days since AceTime epoch of 2000-01-01. More... | |
static LocalDate | forUnixDays (acetime_t unixDays) |
Factory method using the number of days since Unix epoch 1970-01-1. More... | |
static LocalDate | forEpochSeconds (acetime_t epochSeconds) |
Factory method using the number of seconds since AceTime epoch of 2000-01-01. More... | |
static LocalDate | forUnixSeconds (acetime_t unixSeconds) |
Factory method that takes the number of seconds since Unix Epoch of 1970-01-01. More... | |
static LocalDate | forDateString (const char *dateString) |
Factory method. More... | |
static LocalDate | forError () |
Factory method that returns a LocalDate which represents an error condition. More... | |
static bool | isLeapYear (int16_t year) |
True if year is a leap year. More... | |
static uint8_t | daysInMonth (int16_t year, uint8_t month) |
Return the number of days in the current month. More... | |
Static Public Attributes | |
static const int16_t | kEpochYear = 2000 |
Base year of epoch. More... | |
static const int8_t | kInvalidYearTiny = INT8_MIN |
Sentinel yearTiny which indicates an error condition or sometimes a year that 'does not exist'. | |
static const int8_t | kMinYearTiny = INT8_MIN + 1 |
Sentinel yearTiny which represents the smallest year, effectively -Infinity. | |
static const acetime_t | kInvalidEpochDays = INT32_MIN |
Sentinel epochDays which indicates an error. More... | |
static const acetime_t | kInvalidEpochSeconds = LocalTime::kInvalidSeconds |
Sentinel epochSeconds which indicates an error. More... | |
static const acetime_t | kSecondsSinceUnixEpoch = 946684800 |
Number of seconds from Unix epoch (1970-01-01 00:00:00Z) to the AceTime epoch (2000-01-01 00:00:00Z). | |
static const acetime_t | kDaysSinceUnixEpoch = 10957 |
Number of days from Unix epoch (1970-01-01 00:00:00Z) to the AceTime epoch (2000-01-01 00:00:00Z). | |
static const acetime_t | kDaysSinceJulianEpoch = 2451545 |
Number of days between the Julian calendar epoch (4713 BC 01-01) and the AceTime epoch (2000-01-01). | |
static const uint8_t | kMonday = 1 |
Monday ISO 8601 number. More... | |
static const uint8_t | kTuesday = 2 |
Tuesday ISO 8601 number. More... | |
static const uint8_t | kWednesday = 3 |
Wednesday ISO 8601 number. More... | |
static const uint8_t | kThursday = 4 |
Thursday ISO 8601 number. More... | |
static const uint8_t | kFriday = 5 |
Friday ISO 8601 number. More... | |
static const uint8_t | kSaturday = 6 |
Saturday ISO 8601 number. More... | |
static const uint8_t | kSunday = 7 |
Sunday ISO 8601 number. More... | |
Friends | |
class | LocalDateTime |
class | ExtendedZoneSpecifier |
bool | operator== (const LocalDate &a, const LocalDate &b) |
Return true if two LocalDate objects are equal in all components. More... | |
The date (year, month, day) representing the date without regards to time zone.
The "epoch" for this library is 2000-01-01.
The year field is internally represented as int8_t offset from the year 2000, so in theory it is valid from [1872, 2127]. However, the internal year value of -128 is often used to indicate an error condition. Secondly, the value of 127 will sometimes cause for-loops to misbehave due to integer overflow. Therefore, it's safer to restrict the valid interval to [1873, 2126].
If the year is restricted to 2000-2099 (2 digit years), these fields correspond to the range supported by the DS3231 RTC chip.
The dayOfWeek (1=Monday, 7=Sunday, per ISO 8601) is calculated from the date fields.
Parts of this class were inspired by the java.time.LocalDate class of Java 8 (https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html).
Definition at line 32 of file LocalDate.h.
|
inlineexplicit |
Default constructor does nothing.
Definition at line 200 of file LocalDate.h.
|
inline |
Compare this LocalDate to that LocalDate, returning (<0, 0, >0) according to whether the epochSeconds is (this<that, this==this, this>that).
If isError() is true, the behavior is undefined.
Definition at line 321 of file LocalDate.h.
|
inline |
Return the day of the month.
Definition at line 221 of file LocalDate.h.
|
inline |
Set the day of the month.
Definition at line 224 of file LocalDate.h.
|
inline |
Calculate the day of week given the (year, month, day).
Idea borrowed from https://github.com/evq/utz. No validation of year, month or day is performed. If this is found to be too slow, then consider caching the results.
Definition at line 232 of file LocalDate.h.
|
inlinestatic |
Return the number of days in the current month.
Definition at line 194 of file LocalDate.h.
|
inlinestatic |
Factory method using separated year, month and day fields.
year | [1872-2127] for 8-bit implementation, [0000-9999] for 16-bit implementation |
month | month with January=1, December=12 |
day | day of month (1-31) |
Definition at line 102 of file LocalDate.h.
|
static |
Factory method.
Create a LocalDate from the ISO 8601 date string. If the string cannot be parsed, then isError() on the constructed object returns true, but the data validation is very weak. Year should probably be between 1872 and 2127.
dateString | the date in ISO 8601 format (yyyy-mm-dd) |
Definition at line 62 of file LocalDate.cpp.
|
inlinestatic |
Factory method using the number of days since AceTime epoch of 2000-01-01.
If epochDays is kInvalidEpochDays, isError() will return true.
epochDays | number of days since AceTime epoch (2000-01-01) |
Definition at line 113 of file LocalDate.h.
|
inlinestatic |
Factory method using the number of seconds since AceTime epoch of 2000-01-01.
The number of seconds from midnight of the given day is thrown away. For negative values of epochSeconds, the method performs a floor operation when rounding to the nearest day, in other words towards negative infinity.
If epochSeconds is kInvalidEpochSeconds, isError() will return true.
epochSeconds | number of seconds since AceTime epoch (2000-01-01) |
Definition at line 145 of file LocalDate.h.
|
inlinestatic |
Factory method that returns a LocalDate which represents an error condition.
The isError() method will return true.
Definition at line 184 of file LocalDate.h.
|
inlinestatic |
Factory method using the number of days since Unix epoch 1970-01-1.
Definition at line 126 of file LocalDate.h.
|
inlinestatic |
Factory method that takes the number of seconds since Unix Epoch of 1970-01-01.
Similar to forEpochSeconds(), the seconds corresponding to the partial day are truncated down towards the smallest whole day.
Definition at line 162 of file LocalDate.h.
|
inline |
Return true if any component indicates an error condition.
Definition at line 242 of file LocalDate.h.
|
inlinestatic |
True if year is a leap year.
Definition at line 189 of file LocalDate.h.
|
inline |
Return the month with January=1, December=12.
Definition at line 215 of file LocalDate.h.
|
inline |
Set the month.
Definition at line 218 of file LocalDate.h.
void ace_time::LocalDate::printTo | ( | Print & | printer | ) | const |
Print LocalDate to 'printer' in ISO 8601 format, along with the day of week.
Does not implement Printable to avoid memory cost of a vtable pointer.
Definition at line 43 of file LocalDate.cpp.
|
inline |
Return number of days since AceTime epoch (2000-01-01 00:00:00Z).
Returns kInvalidEpochDays if isError() is true, which allows round trip conversions of forEpochDays() and toEpochDays() even when isError() is true.
In an 8-bit implementation:
Uses Julian days which normally start at 12:00:00. But this method returns the delta number of days since 00:00:00, so we can interpret the Gregorian calendar day to start at 00:00:00.
See https://en.wikipedia.org/wiki/Julian_day
Definition at line 263 of file LocalDate.h.
|
inline |
Return the number of seconds since AceTime epoch (2000-01-01 00:00:00).
Returns kInvalidEpochSeconds if isError() is true. This is a convenience method that returns (86400 * toEpochDays()). Since acetime_t is a 32-bit signed integer, the limits are different:
Definition at line 303 of file LocalDate.h.
|
inline |
Return the number of days since Unix epoch (1970-01-01 00:00:00).
Definition at line 288 of file LocalDate.h.
|
inline |
Return the full year instead of just the last 2 digits.
Definition at line 203 of file LocalDate.h.
|
inline |
Set the year given the full year.
Definition at line 206 of file LocalDate.h.
|
inline |
Return the single-byte year offset from year 2000.
Definition at line 209 of file LocalDate.h.
|
inline |
Set the single-byte year offset from year 2000.
Definition at line 212 of file LocalDate.h.
Return true if two LocalDate objects are equal in all components.
Definition at line 406 of file LocalDate.h.
|
static |
Base year of epoch.
Definition at line 35 of file LocalDate.h.
|
static |
Friday ISO 8601 number.
Definition at line 86 of file LocalDate.h.
|
static |
Sentinel epochDays which indicates an error.
Definition at line 50 of file LocalDate.h.
|
static |
Sentinel epochSeconds which indicates an error.
Definition at line 53 of file LocalDate.h.
|
static |
Monday ISO 8601 number.
Definition at line 74 of file LocalDate.h.
|
static |
Saturday ISO 8601 number.
Definition at line 89 of file LocalDate.h.
|
static |
Sunday ISO 8601 number.
Definition at line 92 of file LocalDate.h.
|
static |
Thursday ISO 8601 number.
Definition at line 83 of file LocalDate.h.
|
static |
Tuesday ISO 8601 number.
Definition at line 77 of file LocalDate.h.
|
static |
Wednesday ISO 8601 number.
Definition at line 80 of file LocalDate.h.