Skip to content

Commit 57c81bd

Browse files
committed
Revert "Removed code using <time.h> non-core library (ie. Epoch stuff)"
This reverts commit ece522d.
1 parent 847eb3d commit 57c81bd

File tree

4 files changed

+86
-0
lines changed

4 files changed

+86
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
Author: Adam Garbo and Nathan Seidle
3+
Created: June 3rd, 2020
4+
License: MIT. See SparkFun Arduino Apollo3 Project for more information
5+
6+
This example demonstrates how to set the RTC using UNIX Epoch time.
7+
*/
8+
9+
#include "RTC.h" // Include RTC library included with the Aruino_Apollo3 core
10+
APM3_RTC myRTC; // Create instance of RTC class
11+
12+
void setup()
13+
{
14+
Serial.begin(115200);
15+
Serial.println("SparkFun RTC Set UNIX Epoch Time Example");
16+
17+
// Set the RTC time using UNIX Epoch time
18+
myRTC.setEpoch(1591185600); // E.g. 12:00:00, June 3rd, 2020
19+
}
20+
21+
void loop()
22+
{
23+
// Print UNIX Epoch timestamp
24+
Serial.print("Epoch time: "); Serial.println(myRTC.getEpoch());
25+
26+
// Print RTC's date and time
27+
Serial.print("Timestamp: "); printDateTime();
28+
29+
delay(1000);
30+
}
31+
32+
// Print the RTC's current date and time
33+
void printDateTime()
34+
{
35+
myRTC.getTime();
36+
char dateTime[20];
37+
sprintf(dateTime, "20%02d-%02d-%02d %02d:%02d:%02d",
38+
myRTC.year, myRTC.month, myRTC.dayOfMonth,
39+
myRTC.hour, myRTC.minute, myRTC.seconds);
40+
Serial.println(dateTime);
41+
}

libraries/RTC/keywords.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ RTC KEYWORD1
1313
#######################################
1414

1515
getTime KEYWORD2
16+
getEpoch KEYWORD2
1617

1718
setTime KEYWORD2
1819
setTimeToCompiler KEYWORD2
20+
setEpoch KEYWORD2
1921

2022
getAlarm KEYWORD2
2123
setAlarm KEYWORD2

libraries/RTC/src/RTC.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44

55
#include "RTC.h"
6+
#include <time.h>
67

78
am_hal_rtc_time_t hal_time;
89
am_hal_rtc_time_t alm_time;
@@ -88,6 +89,28 @@ void APM3_RTC::setToCompilerTime()
8889
getTime();
8990
}
9091

92+
void APM3_RTC::setEpoch(uint32_t ts)
93+
{
94+
if (ts < EPOCH_TIME) {
95+
ts = EPOCH_TIME;
96+
}
97+
98+
struct tm tm;
99+
100+
time_t t = ts;
101+
struct tm* tmp = gmtime(&t);
102+
hal_time.ui32Weekday = 0;
103+
hal_time.ui32Century = 0;
104+
hal_time.ui32Year = tmp->tm_year - 100;
105+
hal_time.ui32Month = tmp->tm_mon + 1;
106+
hal_time.ui32DayOfMonth = tmp->tm_mday;
107+
hal_time.ui32Hour = tmp->tm_hour;
108+
hal_time.ui32Minute = tmp->tm_min;
109+
hal_time.ui32Second = tmp->tm_sec;
110+
hal_time.ui32Hundredths = 0;
111+
112+
am_hal_rtc_time_set(&hal_time); //Initialize the RTC with this date/time
113+
}
91114

92115
void APM3_RTC::getTime()
93116
{
@@ -104,6 +127,24 @@ void APM3_RTC::getTime()
104127
hundredths = hal_time.ui32Hundredths;
105128
}
106129

130+
uint32_t APM3_RTC::getEpoch()
131+
{
132+
am_hal_rtc_time_get(&hal_time);
133+
134+
struct tm tm;
135+
136+
tm.tm_isdst = -1;
137+
tm.tm_yday = 0;
138+
tm.tm_wday = 0;
139+
tm.tm_year = hal_time.ui32Year + 100; //Number of years since 1900.
140+
tm.tm_mon = hal_time.ui32Month - 1; //mktime is expecting 0 to 11 months
141+
tm.tm_mday = hal_time.ui32DayOfMonth;
142+
tm.tm_hour = hal_time.ui32Hour;
143+
tm.tm_min = hal_time.ui32Minute;
144+
tm.tm_sec = hal_time.ui32Second;
145+
146+
return mktime(&tm);
147+
}
107148

108149
void APM3_RTC::getAlarm()
109150
{

libraries/RTC/src/RTC.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ class APM3_RTC
99
APM3_RTC();
1010

1111
void getTime(); //Query the RTC for the current time/date
12+
uint32_t getEpoch(); //Return the current RTC time/date as UNIX Epoch time
1213

1314
void setTime(uint8_t hour, uint8_t min, uint8_t sec, uint8_t hund,
1415
uint8_t dayOfMonth, uint8_t month, uint16_t year); //Set current time to provided hundredths/seconds/etc
1516
void setToCompilerTime(); //Set to time when sketch was compiled
17+
void setEpoch(uint32_t ts); //Set current time to provided UNIX Epoch time
1618

1719
void getAlarm(); //Query the RTC for the current alarm time/date
1820
void setAlarm(uint8_t hour, uint8_t min, uint8_t sec, uint8_t hund, uint8_t dayOfMonth, uint8_t month); //Set alarm time to provided hundredths/seconds/etc

0 commit comments

Comments
 (0)