AceTime
1.7.3
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.
|
6 #ifndef ACE_TIME_LOCAL_DATE_H
7 #define ACE_TIME_LOCAL_DATE_H
10 #include "common/common.h"
11 #include "LocalTime.h"
162 acetime_t days = (epochSeconds < 0)
163 ? (epochSeconds + 1) / 86400 - 1
164 : epochSeconds / 86400;
212 return ((
year % 4 == 0) && (
year % 100 != 0)) || (
year % 400 == 0);
223 uint8_t days = sDaysInMonth[
month - 1];
251 uint8_t
month()
const {
return mMonth; }
257 uint8_t
day()
const {
return mDay; }
270 int16_t y =
year() - (mMonth < 3);
271 int16_t d = y + y/4 - y/100 + y/400 + sDayOfWeek[mMonth-1] + mDay;
274 return (d < -1) ? (d + 1) % 7 + 8 : (d + 1) % 7 + 1;
280 || mDay < 1 || mDay > 31
281 || mMonth < 1 || mMonth > 12;
315 int8_t mm = (mMonth - 14)/12;
317 int32_t jdn = ((int32_t) 1461 * (yy + 4800 + mm))/4
318 + (367 * (mMonth - 2 - 12 * mm))/12
319 - (3 * ((yy + 4900 + mm)/100))/4
321 return jdn - kDaysSinceJulianEpoch;
360 if (mYearTiny < that.mYearTiny)
return -1;
361 if (mYearTiny > that.mYearTiny)
return 1;
362 if (mMonth < that.mMonth)
return -1;
363 if (mMonth > that.mMonth)
return 1;
364 if (mDay < that.mDay)
return -1;
365 if (mDay > that.mDay)
return 1;
375 void printTo(Print& printer)
const;
388 static const acetime_t kDaysSinceJulianEpoch = 2451545;
391 static const uint8_t kDateStringLength = 10;
398 static const uint8_t sDayOfWeek[12];
401 static const uint8_t sDaysInMonth[12];
414 static void extractYearMonthDay(acetime_t epochDays, int16_t&
year,
416 uint32_t J = epochDays + kDaysSinceJulianEpoch;
417 uint32_t f = J + 1401 + (((4 * J + 274277 ) / 146097) * 3) / 4 - 38;
418 uint32_t e = 4 * f + 3;
419 uint32_t g = e % 1461 / 4;
420 uint32_t h = 5 * g + 2;
421 day = (h % 153) / 5 + 1;
422 month = (h / 153 + 2) % 12 + 1;
423 year = (e / 1461) - 4716 + (12 + 2 -
month) / 12;
441 return a.mDay == b.mDay
442 && a.mMonth == b.mMonth
443 && a.mYearTiny == b.mYearTiny;
void printTo(Print &printer) const
Print LocalDate to 'printer' in ISO 8601 format, along with the day of week.
static LocalDate forError()
Factory method that returns a LocalDate which represents an error condition.
static const acetime_t kSecondsSinceUnixEpoch
Number of seconds from Unix epoch (1970-01-01 00:00:00Z) to the AceTime epoch (2000-01-01 00:00:00Z).
LocalDate()
Default constructor does nothing.
static LocalDate forEpochDays(acetime_t epochDays)
Factory method using the number of days since AceTime epoch of 2000-01-01.
uint8_t month() const
Return the month with January=1, December=12.
static const acetime_t kInvalidEpochSeconds
Sentinel epochSeconds which indicates an error.
static const int8_t kMaxYearTiny
Sentinel yearTiny which represents the largest year, effectively +Infinity.
static const uint8_t kThursday
Thursday ISO 8601 number.
static const acetime_t kInvalidEpochDays
Sentinel epochDays which indicates an error.
acetime_t toEpochDays() const
Return number of days since AceTime epoch (2000-01-01 00:00:00Z).
static const acetime_t kInvalidSeconds
An invalid seconds marker that indicates isError() true.
static bool isLeapYear(int16_t year)
True if year is a leap year.
static const int8_t kMinYearTiny
Sentinel yearTiny which represents the smallest year, effectively -Infinity.
uint8_t dayOfWeek() const
Calculate the day of week given the (year, month, day).
void year(int16_t year)
Set the year given the full year.
The date (year, month, day) representing the date without regards to time zone.
acetime_t toUnixSeconds() const
Return the number of seconds since Unix epoch (1970-01-01 00:00:00).
acetime_t toEpochSeconds() const
Return the number of seconds since AceTime epoch (2000-01-01 00:00:00).
friend bool operator==(const LocalDate &a, const LocalDate &b)
Return true if two LocalDate objects are equal in all components.
static LocalDate forEpochSeconds(acetime_t epochSeconds)
Factory method using the number of seconds since AceTime epoch of 2000-01-01.
acetime_t toUnixDays() const
Return the number of days since Unix epoch (1970-01-01 00:00:00).
static LocalDate forDateStringChainable(const char *&dateString)
Variant of forDateString() that updates the pointer to the next unprocessed character.
static const uint8_t kFriday
Friday ISO 8601 number.
static LocalDate forDateString(const char *dateString)
Factory method.
static const int8_t kInvalidYearTiny
Sentinel yearTiny which indicates an error condition or sometimes a year that 'does not exist'.
uint8_t day() const
Return the day of the month.
static const uint8_t kSaturday
Saturday ISO 8601 number.
void day(uint8_t day)
Set the day of the month.
static LocalDate forTinyComponents(int8_t yearTiny, uint8_t month, uint8_t day)
Factory method using components with an int8_t yearTiny.
bool isError() const
Return true if any component indicates an error condition.
static LocalDate forComponents(int16_t year, uint8_t month, uint8_t day)
Factory method using separated year, month and day fields.
int16_t year() const
Return the full year instead of just the last 2 digits.
static const int16_t kEpochYear
Base year of epoch.
static LocalDate forUnixSeconds(acetime_t unixSeconds)
Factory method that takes the number of seconds since Unix Epoch of 1970-01-01.
static const uint8_t kSunday
Sunday ISO 8601 number.
int8_t compareTo(const LocalDate &that) const
Compare 'this' LocalDate to 'that' LocalDate, returning (<0, 0, >0) according to whether 'this' occur...
static const uint8_t kTuesday
Tuesday ISO 8601 number.
static uint8_t daysInMonth(int16_t year, uint8_t month)
Return the number of days in the current month.
void yearTiny(int8_t yearTiny)
Set the single-byte year offset from year 2000.
static LocalDate forUnixDays(acetime_t unixDays)
Factory method using the number of days since Unix epoch 1970-01-1.
void month(uint8_t month)
Set the month.
static const acetime_t kDaysSinceUnixEpoch
Number of days from Unix epoch (1970-01-01 00:00:00Z) to the AceTime epoch (2000-01-01 00:00:00Z).
static bool isYearValid(int16_t year)
Return true if year is within valid range of [1873, 2127].
static const uint8_t kMonday
Monday ISO 8601 number.
int8_t yearTiny() const
Return the single-byte year offset from year 2000.
static const uint8_t kWednesday
Wednesday ISO 8601 number.