6 #ifndef ACE_TIME_LOCAL_DATE_H 7 #define ACE_TIME_LOCAL_DATE_H 10 #include "common/common.h" 11 #include "LocalTime.h" 129 if (epochDays == kInvalidEpochDays) {
130 year = month = day = 0;
132 extractYearMonthDay(epochDays, year, month, day);
139 if (unixDays == kInvalidEpochDays) {
158 if (epochSeconds == kInvalidEpochSeconds) {
162 acetime_t days = (epochSeconds < 0)
163 ? (epochSeconds + 1) / 86400 - 1
164 : epochSeconds / 86400;
175 if (unixSeconds == kInvalidEpochSeconds) {
207 return LocalDate(kInvalidYearTiny, 0, 0);
212 return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
217 return year >= kEpochYear + kMinYearTiny
223 uint8_t days = sDaysInMonth[month - 1];
224 return (month == 2 &&
isLeapYear(year)) ? days + 1 : days;
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;
279 return mYearTiny == kInvalidYearTiny
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;
359 if (mYearTiny < that.mYearTiny)
return -1;
360 if (mYearTiny > that.mYearTiny)
return 1;
361 if (mMonth < that.mMonth)
return -1;
362 if (mMonth > that.mMonth)
return 1;
363 if (mDay < that.mDay)
return -1;
364 if (mDay > that.mDay)
return 1;
374 void printTo(Print& printer)
const;
387 static const acetime_t kDaysSinceJulianEpoch = 2451545;
390 static const uint8_t kDateStringLength = 10;
397 static const uint8_t sDayOfWeek[12];
400 static const uint8_t sDaysInMonth[12];
413 static void extractYearMonthDay(acetime_t epochDays, int16_t&
year,
414 uint8_t& month, uint8_t& day) {
415 uint32_t J = epochDays + kDaysSinceJulianEpoch;
416 uint32_t f = J + 1401 + (((4 * J + 274277 ) / 146097) * 3) / 4 - 38;
417 uint32_t e = 4 * f + 3;
418 uint32_t g = e % 1461 / 4;
419 uint32_t h = 5 * g + 2;
420 day = (h % 153) / 5 + 1;
421 month = (h / 153 + 2) % 12 + 1;
422 year = (e / 1461) - 4716 + (12 + 2 - month) / 12;
440 return a.mDay == b.mDay
441 && a.mMonth == b.mMonth
442 && 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.
int8_t yearTiny() const
Return the single-byte year offset from year 2000.
static const acetime_t kInvalidEpochSeconds
Sentinel epochSeconds which indicates an error.
static const acetime_t kInvalidSeconds
An invalid seconds marker that indicates isError() true.
static LocalDate forDateString(const char *dateString)
Factory method.
static bool isLeapYear(int16_t year)
True if year is a leap year.
void printTo(Print &printer) const
Print LocalDate to 'printer' in ISO 8601 format, along with the day of week.
acetime_t toEpochDays() const
Return number of days since AceTime epoch (2000-01-01 00:00:00Z).
static const int8_t kMaxYearTiny
Sentinel yearTiny which represents the largest year, effectively -Infinity.
static LocalDate forTinyComponents(int8_t yearTiny, uint8_t month, uint8_t day)
Factory method using components with an int8_t yearTiny.
static LocalDate forDateStringChainable(const char *&dateString)
Variant of forDateString() that updates the pointer to the next unprocessed character.
bool isError() const
Return true if any component indicates an error condition.
uint8_t day() const
Return the day of the month.
acetime_t toEpochSeconds() const
Return the number of seconds since AceTime epoch (2000-01-01 00:00:00).
acetime_t toUnixDays() const
Return the number of days since Unix epoch (1970-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.
void year(int16_t year)
Set the year given the full year.
acetime_t toUnixSeconds() const
Return the number of seconds since Unix epoch (1970-01-01 00:00:00).
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 month() const
Return the month with January=1, December=12.
static bool isYearValid(int16_t year)
Return true if year is within valid range of [1873, 2127].
int16_t year() const
Return the full year instead of just the last 2 digits.
void day(uint8_t day)
Set the day of the month.
int8_t compareTo(const LocalDate &that) const
Compare this LocalDate to that LocalDate, returning (<0, 0, >0) according to whether the epochSeconds...
static const uint8_t kMonday
Monday ISO 8601 number.
uint8_t dayOfWeek() const
Calculate the day of week given the (year, month, day).