Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a Epoch convert function, so you can easily update the RTC if the time is smaller in RTC then in Compiler Time&Date? #2

Open
MarcelVollebregt opened this issue May 9, 2021 · 0 comments

Comments

@MarcelVollebregt
Copy link

Maybe add:
/*!

  • \brief Convert to Unix UTC epoch time_t
  • \return
  •  Unix epoch time_t seconds since 1970.
    

*/
time_t ErriezDS1307::getEpoch(struct tm *rdt)
{
time_t t;

// Convert date/time struct tm to time_t
t = mktime(rdt);

// An offset is needed for AVR target

#ifdef ARDUINO_ARCH_AVR
t += UNIX_OFFSET;
#endif

// Return Unix epoch UTC
return t;

}

It helped me to get an automatic RTC update based on Compiled time&date in my application:
if (rtc.read(&rtc_dt))
{
Serial.println("Time fetched from RTC IC....");
}
else
{
Serial.println("Time NOT fetched from RTC IC....");
}

if (rtc.getEpoch(&dt) > rtc.getEpoch(&rtc_dt))
{
// Set new date time
Serial.print(F("Set RTC date time..."));
if (!rtc.write(&dt)) {
Serial.println(F("FAILED"));
} else {
Serial.println(F("OK"));
}
}
And of course no battery yet installed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant