24 #if defined(ARDUINO) && ARDUINO >= 100
58 #define DEFAULT_BYTE_COUNT 43 // Default simulates DS1307 NVRAM size.
60 #define MILLIS_PER_DAY 86400000UL
61 #define MILLIS_PER_SECOND 1000UL
62 #define MILLIS_PER_MINUTE 60000UL
63 #define MILLIS_PER_HOUR 3600000UL
65 static uint8_t monthLengths[] = {
66 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
69 static unsigned int monthOffsets[] = {
75 31 + 28 + 31 + 30 + 31,
76 31 + 28 + 31 + 30 + 31 + 30,
77 31 + 28 + 31 + 30 + 31 + 30 + 31,
78 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31,
79 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
80 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
81 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30
84 inline bool isLeapYear(
unsigned int year)
86 if ((year % 100) == 0)
87 return (year % 400) == 0;
89 return (year % 4) == 0;
92 inline uint8_t monthLength(
const RTCDate *date)
94 if (date->
month != 2 || !isLeapYear(date->
year))
95 return monthLengths[date->
month - 1];
106 : midnight(millis() - 9 * MILLIS_PER_HOUR)
115 for (uint8_t index = 0; index <
ALARM_COUNT; ++index) {
116 alarms[index].
hour = 6;
118 alarms[index].
flags = 0;
147 unsigned long sinceMidnight = millis() - midnight;
148 if (sinceMidnight >= MILLIS_PER_DAY) {
150 midnight += MILLIS_PER_DAY;
151 sinceMidnight -= MILLIS_PER_DAY;
156 value->
second = (uint8_t)(((sinceMidnight / MILLIS_PER_SECOND) % 60));
157 value->
minute = (uint8_t)(((sinceMidnight / MILLIS_PER_MINUTE) % 60));
158 value->
hour = (uint8_t)(sinceMidnight / MILLIS_PER_HOUR);
182 unsigned long sinceMidnight =
183 value->
second * MILLIS_PER_SECOND +
184 value->
minute * MILLIS_PER_MINUTE +
185 value->
hour * MILLIS_PER_HOUR;
186 midnight = millis() - sinceMidnight;
211 *value = alarms[alarmNum];
226 alarms[alarmNum] = *value;
237 return DEFAULT_BYTE_COUNT;
250 return nvram[offset];
265 nvram[offset] = value;
267 nvram = (uint8_t *)malloc(DEFAULT_BYTE_COUNT);
269 memset(nvram, 0, DEFAULT_BYTE_COUNT);
270 nvram[offset] = value;
317 if (date->
day == 0) {
318 if (!(flags &
WRAP)) {
320 if (date->
month == 0)
323 date->
day = monthLength(date);
327 if (date->
day > monthLength(date)) {
328 if (!(flags &
WRAP)) {
330 if (date->
month == 13)
347 if (date->
month == 0) {
349 if (!(flags &
WRAP) && date->
year > 2000)
354 if (date->
month == 13) {
356 if (!(flags &
WRAP) && date->
year < 2099)
360 uint8_t len = monthLength(date);
374 if (date->
year < 2000)
378 if (date->
year > 2099)
381 uint8_t len = monthLength(date);
402 unsigned long daynum = date->
day + 4;
403 daynum += monthOffsets[date->
month - 1];
404 if (date->
month > 2 && isLeapYear(date->
year))
406 daynum += 365UL * (date->
year - 2000);
407 if (date->
year > 2000)
408 daynum += ((date->
year - 2001) / 4) + 1;