Skip to content

Commit 5975b88

Browse files
committed
Use time.h API to convert to/from epoch time (mktime, gmtime)
1 parent 8652a6f commit 5975b88

File tree

1 file changed

+33
-61
lines changed

1 file changed

+33
-61
lines changed

src/RTCZero.cpp

Lines changed: 33 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616
License along with this library; if not, write to the Free Software
1717
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1818
*/
19-
20-
#include "RTCZero.h"
2119

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"
2323

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
2526

2627
voidFuncPtr RTC_callBack = NULL;
2728

@@ -296,80 +297,51 @@ void RTCZero::setAlarmDate(uint8_t day, uint8_t month, uint8_t year)
296297

297298
uint32_t RTCZero::getEpoch()
298299
{
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);
300313
}
301314

302315
uint32_t RTCZero::getY2kEpoch()
303316
{
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);
322318
}
323319

324320
void RTCZero::setEpoch(uint32_t ts)
325321
{
326322
if (ts < EPOCH_TIME_OFF) {
327-
setY2kEpoch(0);
328-
}
329-
else {
330-
setY2kEpoch(ts - EPOCH_TIME_OFF);
323+
ts = EPOCH_TIME_OFF;
331324
}
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;
347325

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);
355328

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;
365335

366-
RTC->MODE2.CLOCK.bit.YEAR = years;
367-
RTC->MODE2.CLOCK.bit.MONTH = months;
368-
RTC->MODE2.CLOCK.bit.DAY = days + 1;
369336
while (RTCisSyncing())
370337
;
371338
}
372339

340+
void RTCZero::setY2kEpoch(uint32_t ts)
341+
{
342+
setEpoch(ts + EPOCH_TIME_OFF);
343+
}
344+
373345
/*
374346
* Private Utility Functions
375347
*/

0 commit comments

Comments
 (0)