1 #ifndef ACE_TIME_LOCAL_DATE_H 2 #define ACE_TIME_LOCAL_DATE_H 5 #include "common/common.h" 103 return LocalDate(year - kEpochYear, month, day);
117 if (epochDays == kInvalidEpochDays) {
118 year = month = day = 0;
120 extractYearMonthDay(epochDays, year, month, day);
127 if (unixDays == kInvalidEpochDays) {
146 if (epochSeconds == kInvalidEpochSeconds) {
150 acetime_t days = (epochSeconds < 0)
151 ? (epochSeconds + 1) / 86400 - 1
152 : epochSeconds / 86400;
163 if (unixSeconds == kInvalidEpochSeconds) {
190 return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
195 uint8_t days = sDaysInMonth[month - 1];
196 return (month == 2 &&
isLeapYear(year)) ? days + 1 : days;
215 uint8_t
month()
const {
return mMonth; }
221 uint8_t
day()
const {
return mDay; }
234 int16_t y =
year() - (mMonth < 3);
235 int16_t d = y + y/4 - y/100 + y/400 + sDayOfWeek[mMonth-1] + mDay;
238 return (d < -1) ? (d + 1) % 7 + 8 : (d + 1) % 7 + 1;
243 return mDay < 1 || mDay > 31
244 || mMonth < 1 || mMonth > 12;
278 int8_t mm = (mMonth - 14)/12;
280 int32_t jdn = ((int32_t) 1461 * (yy + 4800 + mm))/4
281 + (367 * (mMonth - 2 - 12 * mm))/12
282 - (3 * ((yy + 4900 + mm)/100))/4
322 if (mYearTiny < that.mYearTiny)
return -1;
323 if (mYearTiny > that.mYearTiny)
return 1;
324 if (mMonth < that.mMonth)
return -1;
325 if (mMonth > that.mMonth)
return 1;
326 if (mDay < that.mDay)
return -1;
327 if (mDay > that.mDay)
return 1;
336 void printTo(Print& printer)
const;
348 static const uint8_t kDateStringLength = 10;
355 static const uint8_t sDayOfWeek[12];
358 static const uint8_t sDaysInMonth[12];
367 static LocalDate forDateStringChainable(
const char*& dateString);
380 static void extractYearMonthDay(acetime_t epochDays, int16_t&
year,
381 uint8_t& month, uint8_t& day) {
383 uint32_t f = J + 1401 + (((4 * J + 274277 ) / 146097) * 3) / 4 - 38;
384 uint32_t e = 4 * f + 3;
385 uint32_t g = e % 1461 / 4;
386 uint32_t h = 5 * g + 2;
387 day = (h % 153) / 5 + 1;
388 month = (h / 153 + 2) % 12 + 1;
389 year = (e / 1461) - 4716 + (12 + 2 - month) / 12;
407 return a.mDay == b.mDay
408 && a.mMonth == b.mMonth
409 && a.mYearTiny == b.mYearTiny;
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)...
static LocalDate forEpochDays(acetime_t epochDays)
Factory method using the number of days since AceTime epoch of 2000-01-01.
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 const uint8_t kWednesday
Wednesday ISO 8601 number.
static LocalDate forError()
Factory method that returns a LocalDate which represents an error condition.
static const uint8_t kThursday
Thursday ISO 8601 number.
static const acetime_t kInvalidEpochDays
Sentinel epochDays which indicates an error.
static LocalDate forUnixDays(acetime_t unixDays)
Factory method using the number of days since Unix epoch 1970-01-1.
LocalDate()
Default constructor does nothing.
static const int8_t kMinYearTiny
Sentinel yearTiny which represents the smallest year, effectively -Infinity.
static const acetime_t kInvalidEpochSeconds
Sentinel epochSeconds which indicates an error.
static const acetime_t kInvalidSeconds
An invalid seconds marker that indicates isError() true.
acetime_t toEpochSeconds() const
Return the number of seconds since AceTime epoch (2000-01-01 00:00:00).
static LocalDate forDateString(const char *dateString)
Factory method.
static bool isLeapYear(int16_t year)
True if year is a leap year.
uint8_t month() const
Return the month with January=1, December=12.
bool isError() const
Return true if any component indicates an error condition.
An implementation of ZoneSpecifier that works for all zones defined by the TZ Database (with some zon...
void printTo(Print &printer) const
Print LocalDate to 'printer' in ISO 8601 format, along with the day of week.
int16_t year() const
Return the full year instead of just the last 2 digits.
friend bool operator==(const LocalDate &a, const LocalDate &b)
Return true if two LocalDate objects are equal in all components.
uint8_t day() const
Return the day of the month.
static LocalDate forEpochSeconds(acetime_t epochSeconds)
Factory method using the number of seconds since AceTime epoch of 2000-01-01.
static const acetime_t kDaysSinceJulianEpoch
Number of days between the Julian calendar epoch (4713 BC 01-01) and the AceTime epoch (2000-01-01)...
void year(int16_t year)
Set the year given the full year.
void yearTiny(int8_t yearTiny)
Set the single-byte year offset from year 2000.
static LocalDate forUnixSeconds(acetime_t unixSeconds)
Factory method that takes the number of seconds since Unix Epoch of 1970-01-01.
static const int8_t kInvalidYearTiny
Sentinel yearTiny which indicates an error condition or sometimes a year that 'does not exist'...
static LocalDate forComponents(int16_t year, uint8_t month, uint8_t day)
Factory method using separated year, month and day fields.
static const uint8_t kSunday
Sunday ISO 8601 number.
The date (year, month, day) representing the date without regards to time zone.
static const uint8_t kFriday
Friday ISO 8601 number.
static const uint8_t kSaturday
Saturday ISO 8601 number.
static const uint8_t kTuesday
Tuesday ISO 8601 number.
void month(uint8_t month)
Set the month.
static const int16_t kEpochYear
Base year of epoch.
static uint8_t daysInMonth(int16_t year, uint8_t month)
Return the number of days in the current month.
uint8_t dayOfWeek() const
Calculate the day of week given the (year, month, day).
acetime_t toUnixSeconds() const
Return the number of seconds since Unix epoch (1970-01-01 00:00:00).
void day(uint8_t day)
Set the day of the month.
int8_t yearTiny() const
Return the single-byte year offset from year 2000.
acetime_t toEpochDays() const
Return number of days since AceTime epoch (2000-01-01 00:00:00Z).
int8_t compareTo(const LocalDate &that) const
Compare this LocalDate to that LocalDate, returning (<0, 0, >0) according to whether the epochSeconds...
acetime_t toUnixDays() const
Return the number of days since Unix epoch (1970-01-01 00:00:00).
static const uint8_t kMonday
Monday ISO 8601 number.