|
16 | 16 | License along with this library; if not, write to the Free Software
|
17 | 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
18 | 18 | */
|
19 |
| - |
20 |
| -#include "RTCZero.h" |
21 | 19 |
|
22 |
| -#define EPOCH_TIME_OFF 946684800 // This is 1st January 2000, 00:00:00 in epoch time |
| 20 | +#include <time.h> |
| 21 | + |
| 22 | +#include "RTCZero.h" |
23 | 23 |
|
24 |
| -static const uint8_t daysInMonth[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 }; |
| 24 | +#define EPOCH_TIME_OFF 946684800 // This is 1st January 2000, 00:00:00 in epoch time |
| 25 | +#define EPOCH_TIME_YEAR_OFF 100 // years since 1900 |
25 | 26 |
|
26 | 27 | voidFuncPtr RTC_callBack = NULL;
|
27 | 28 |
|
@@ -296,80 +297,51 @@ void RTCZero::setAlarmDate(uint8_t day, uint8_t month, uint8_t year)
|
296 | 297 |
|
297 | 298 | uint32_t RTCZero::getEpoch()
|
298 | 299 | {
|
299 |
| - return getY2kEpoch() + EPOCH_TIME_OFF; |
| 300 | + struct tm tm; |
| 301 | + |
| 302 | + tm.tm_isdst = -1; |
| 303 | + tm.tm_yday = 0; |
| 304 | + tm.tm_wday = 0; |
| 305 | + tm.tm_year = getYear() + EPOCH_TIME_YEAR_OFF; |
| 306 | + tm.tm_mon = getMonth() - 1; |
| 307 | + tm.tm_mday = getDay(); |
| 308 | + tm.tm_hour = getHours(); |
| 309 | + tm.tm_min = getMinutes(); |
| 310 | + tm.tm_sec = getSeconds(); |
| 311 | + |
| 312 | + return mktime(&tm); |
300 | 313 | }
|
301 | 314 |
|
302 | 315 | uint32_t RTCZero::getY2kEpoch()
|
303 | 316 | {
|
304 |
| - uint16_t days = RTC->MODE2.CLOCK.bit.DAY; |
305 |
| - days = days > 0 ? days : 1; |
306 |
| - uint8_t months = RTC->MODE2.CLOCK.bit.MONTH; |
307 |
| - uint16_t years = RTC->MODE2.CLOCK.bit.YEAR; |
308 |
| - |
309 |
| - for (uint8_t i = 1; i < months; ++i) { |
310 |
| - days += daysInMonth[i - 1]; |
311 |
| - } |
312 |
| - |
313 |
| - if ((months > 2) && (years % 4 == 0)) { |
314 |
| - ++days; |
315 |
| - } |
316 |
| - days += 365 * years + (years + 3) / 4 - 1; |
317 |
| - |
318 |
| - uint8_t hours = RTC->MODE2.CLOCK.bit.HOUR; |
319 |
| - |
320 |
| - return ((days * 24 + hours) * 60 + |
321 |
| - RTC->MODE2.CLOCK.bit.MINUTE) * 60 + RTC->MODE2.CLOCK.bit.SECOND; |
| 317 | + return (getEpoch() - EPOCH_TIME_OFF); |
322 | 318 | }
|
323 | 319 |
|
324 | 320 | void RTCZero::setEpoch(uint32_t ts)
|
325 | 321 | {
|
326 | 322 | if (ts < EPOCH_TIME_OFF) {
|
327 |
| - setY2kEpoch(0); |
328 |
| - } |
329 |
| - else { |
330 |
| - setY2kEpoch(ts - EPOCH_TIME_OFF); |
| 323 | + ts = EPOCH_TIME_OFF; |
331 | 324 | }
|
332 |
| -} |
333 |
| - |
334 |
| -void RTCZero::setY2kEpoch(uint32_t ts) |
335 |
| -{ |
336 |
| - RTC->MODE2.CLOCK.bit.SECOND = ts % 60; |
337 |
| - ts /= 60; |
338 |
| - RTC->MODE2.CLOCK.bit.MINUTE = ts % 60; |
339 |
| - ts /= 60; |
340 |
| - RTC->MODE2.CLOCK.bit.HOUR = ts % 24; |
341 |
| - |
342 |
| - uint16_t days = ts / 24; |
343 |
| - uint8_t months; |
344 |
| - uint8_t years; |
345 |
| - |
346 |
| - uint8_t leap; |
347 | 325 |
|
348 |
| - // Calculate years |
349 |
| - for (years = 0; ; ++years) { |
350 |
| - leap = years % 4 == 0; |
351 |
| - if (days < 365 + leap) |
352 |
| - break; |
353 |
| - days -= 365 + leap; |
354 |
| - } |
| 326 | + time_t t = ts; |
| 327 | + struct tm* tmp = gmtime(&t); |
355 | 328 |
|
356 |
| - // Calculate months |
357 |
| - for (months = 1; ; ++months) { |
358 |
| - uint8_t daysPerMonth = daysInMonth[months - 1]; |
359 |
| - if (leap && months == 2) |
360 |
| - ++daysPerMonth; |
361 |
| - if (days < daysPerMonth) |
362 |
| - break; |
363 |
| - days -= daysPerMonth; |
364 |
| - } |
| 329 | + RTC->MODE2.CLOCK.bit.YEAR = tmp->tm_year - EPOCH_TIME_YEAR_OFF; |
| 330 | + RTC->MODE2.CLOCK.bit.MONTH = tmp->tm_mon + 1; |
| 331 | + RTC->MODE2.CLOCK.bit.DAY = tmp->tm_mday; |
| 332 | + RTC->MODE2.CLOCK.bit.HOUR = tmp->tm_hour; |
| 333 | + RTC->MODE2.CLOCK.bit.MINUTE = tmp->tm_min; |
| 334 | + RTC->MODE2.CLOCK.bit.SECOND = tmp->tm_sec; |
365 | 335 |
|
366 |
| - RTC->MODE2.CLOCK.bit.YEAR = years; |
367 |
| - RTC->MODE2.CLOCK.bit.MONTH = months; |
368 |
| - RTC->MODE2.CLOCK.bit.DAY = days + 1; |
369 | 336 | while (RTCisSyncing())
|
370 | 337 | ;
|
371 | 338 | }
|
372 | 339 |
|
| 340 | +void RTCZero::setY2kEpoch(uint32_t ts) |
| 341 | +{ |
| 342 | + setEpoch(ts + EPOCH_TIME_OFF); |
| 343 | +} |
| 344 | + |
373 | 345 | /*
|
374 | 346 | * Private Utility Functions
|
375 | 347 | */
|
|
0 commit comments