/*
https://forum.arduino.cc/t/gps-utc-calender-problem-in-time-zone-0/1230832/8
2024-03-03 by noiasca
code in thread
*/
#include <time.h> // comes with Arduino Core see https://cplusplus.com/reference/ctime/
void test() {
// get your time elements in UTC:
int year = 2024;
int month = 3;
int day = 3;
int hour = 22;
int minute = 20;
int second = 30;
// generate a struct tm with the given data:
tm timeinfo = {0};
timeinfo.tm_year = year - 1900;
timeinfo.tm_mon = month - 1;
timeinfo.tm_mday = day;
timeinfo.tm_hour = hour;
timeinfo.tm_min = minute;
timeinfo.tm_sec = second;
time_t utc = mktime(&timeinfo); // time_t UTC (epoche)
uint16_t offset = 2 * 60 * 60UL; // difference in seconds
time_t local = utc + offset; // time_t in local time (epoche)
tm * localinfo; // struct tm for local time
localinfo = localtime (&local); // update local timeinfo based on local
char buffer[42] {0}; // a buffer large enough to hold your output
strftime (buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", &timeinfo);
//Serial.println(utc);
Serial.println(buffer);
strftime (buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", localinfo); // for different formats see https://cplusplus.com/reference/ctime/strftime/
//Serial.println(local);
Serial.println(buffer);
}
void setup() {
Serial.begin(115200);
Serial.println("time.h");
test();
}
void loop() {
// put your main code here, to run repeatedly:
}