/* Constructor for the RTC on the logging shield. */
DFRobot_DS1307 DS1307;
char daynames[]="SUN\0MON\0TUE\0WED\0THU\0FRI\0SAT\0";
// Set the ESP32 time from the RTC.
bool StartRTC() { // See http://www.rinkydinkelectronics.com/resource/DS1307/DS1307.pdf
if( !(DS1307.begin()) ){
Serial.println(F("Communication with DS1307 RTC device failed"));
return 1;
}
struct dsTimeStruct {
uint16_t tm_sec; /* seconds, range 0 to 59 */
uint16_t tm_min; /* minutes, range 0 to 59 */
uint16_t tm_hour; /* hours, range 0 to 23 */
uint16_t tm_wday; /* day of the week, range 0 to 6 */
uint16_t tm_mday; /* day of the month, range 1 to 31 */
uint16_t tm_mon; /* month, range 1 to 12 (not 0 to 11!) */
uint16_t tm_year; /* The number of years since 1900 */
uint16_t tm_yday; /* day in the year, range 0 to 365 */
uint16_t tm_isdst; /* daylight saving time */
} dstm ={0};
DS1307.getTime((uint16_t *)&dstm); //dstm.tm_mon--; // The month on the DS1307 is 1-12.
Serial.printf("DS1307 time: %s %4d/%02d/%02d %02d:%02d:%02d UTC\r\n", // DS1307 time: SUN 2022/07/17 13:06:10 UTC
&daynames[dstm.tm_wday*4], //Weekday &daynames[(getTimeBuff[3])*4], //Weekday [3]
dstm.tm_year, // Year [6]
dstm.tm_mon, // Month [5]
dstm.tm_mday, // Day [4]
dstm.tm_hour, // Hours dstm.dsRaw[2]
dstm.tm_min, // Mins dstm.dsRaw[1]
dstm.tm_sec // seconds dstm.dsRaw[0]
);
time_t seconds;
time(&seconds); // Put the time into seconds
struct tm *esptm;
esptm=gmtime(&seconds);
Serial.printf("ESP32 internal time: %s",asctime(esptm)); // ESP32 internal time: Thu Jan 1 00:00:00 1970
//DS1307 format is not same as linux ( they put the tm_wday in the middle, and don't have yday and isdst).
struct tm newdstm; newdstm.tm_sec =dstm.tm_sec; newdstm.tm_min =dstm.tm_min; newdstm.tm_hour=dstm.tm_hour;
newdstm.tm_wday=dstm.tm_wday; newdstm.tm_mday=dstm.tm_mday; newdstm.tm_mon =dstm.tm_mon-1; newdstm.tm_year=dstm.tm_year-1900;
// Serial.printf("newdstm time: %s",asctime(&newdstm)); // newdstm time: Mon Jul 17 13:09:32 2022
struct timeval dstv = { .tv_sec = mktime(&newdstm), .tv_usec = 0}; // &dstm.tm_ds
settimeofday(&dstv, NULL);
time(&seconds); // Put the updated-above time into seconds
esptm=gmtime(&seconds); // Convert epoc-based to D/M/Y...
Serial.printf("new ESP32 internal time: %s",asctime(esptm)); // new ESP32 internal time: Sun Jul 17 13:14:29 2022
return 0;
} // StartRTC