Skip to content

Commit

Permalink
Implement #95 by adding LRTC::epoch
Browse files Browse the repository at this point in the history
The new method epoch() converts local time (year/month/day) to UNIX epoch time  with `mktime`.
  • Loading branch information
pablosun committed Aug 14, 2018
1 parent 78e554f commit 53acd43
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <Arduino.h>
#include "LRTC.h"
#include <hal_rtc.h>
#include <time.h>

LRTCClass LRTC;

Expand Down Expand Up @@ -97,3 +98,32 @@ int32_t LRTCClass::second()
{
return m_second;
}

time_t LRTCClass::epoch()
{
tm t;
time_t epoch = 0;
t.tm_sec = m_second;
t.tm_min = m_minute;
t.tm_hour = m_hour % 24;
t.tm_mday = m_day;
// C99 tm_month ranges from 0-11
t.tm_mon = m_month - 1;
// tm_year is years from 1900
t.tm_year = m_year - 1900;

// no day-light saving time
t.tm_isdst = 0;

// These are ignored by mktime
t.tm_wday = 0;
t.tm_yday = 0;

epoch = mktime(&t);

if(-1 == epoch) {
return 0;
} else {
return (uint64_t)epoch;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#define __LRTC_H__

#include <Arduino.h>

#include <time.h>
/***
***/

Expand All @@ -21,6 +21,10 @@ class LRTCClass
int32_t minute();
int32_t second();

// This methods return the time retrieved get() in unix epoch format.
// 0 is returned if the
time_t epoch();

void begin();
void get();
void set(int32_t year, int32_t month, int32_t day, int32_t hour, int32_t minute, int32_t second);
Expand Down

0 comments on commit 53acd43

Please sign in to comment.