Skip to content

Commit 431be4f

Browse files
committed
Merge pull request #5 from sandeepmistry/time.h
Use time.h API's to convert to/from epoch time and new epoch time example
2 parents 8652a6f + 951458b commit 431be4f

File tree

2 files changed

+91
-61
lines changed

2 files changed

+91
-61
lines changed

examples/Epoch/Epoch.ino

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
Epoch time example for Arduino Zero
3+
4+
Demonstrates the use of the RTC library for the Arduino Zero
5+
6+
This example code is in the public domain
7+
8+
created by Sandeep Mistry <[email protected]>
9+
31 Dec 2015
10+
*/
11+
12+
#include <RTCZero.h>
13+
14+
/* Create an rtc object */
15+
RTCZero rtc;
16+
17+
void setup() {
18+
Serial.begin(9600);
19+
20+
rtc.begin(); // initialize RTC
21+
22+
rtc.setEpoch(1451606400); // Jan 1, 2016
23+
}
24+
25+
void loop() {
26+
Serial.print("Unix time = ");
27+
Serial.println(rtc.getEpoch());
28+
29+
Serial.print("Seconds since Jan 1 2000 = ");
30+
Serial.println(rtc.getY2kEpoch());
31+
32+
// Print date...
33+
Serial.print(rtc.getDay());
34+
Serial.print("/");
35+
Serial.print(rtc.getMonth());
36+
Serial.print("/");
37+
Serial.print(rtc.getYear());
38+
Serial.print("\t");
39+
40+
// ...and time
41+
print2digits(rtc.getHours());
42+
Serial.print(":");
43+
print2digits(rtc.getMinutes());
44+
Serial.print(":");
45+
print2digits(rtc.getSeconds());
46+
47+
Serial.println();
48+
49+
delay(1000);
50+
}
51+
52+
void print2digits(int number) {
53+
if (number < 10) {
54+
Serial.print("0");
55+
}
56+
Serial.print(number);
57+
}
58+

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)