From my below test sketch, the output is:
New Time: 7:30:0 on 2/23/2025
I would like to format the time as HH:MM:SS, or at least H:MM:SS, and subtracting 6 hours from setTime(1, 30, 0, 23, 2, 2025) should land on 22, 2, 2025 right?
"format" is not allowed, "fread" suggested by compiler doesn't work. Seems like TimeLib isn't subtracting Day based on the number of hours subtracted.
My searches are not finding the solution or example, any ideas for the correct method/syntax appreciated?
#include <TimeLib.h>
void setup() {
Serial.begin(9600);
// Set the current time (e.g., 22nd February 2025, 19:20:00)
setTime(1, 30, 0, 23, 2, 2025);
// Number of hours to subtract
int qtrs = -24; // CST difference from GMT
//int hrs = qtrs/4;
int hoursToSubtract = qtrs/4;
// Calculate the new time
time_t newTime = now() - (hoursToSubtract * SECS_PER_HOUR);
// Print the new time
Serial.print("New Time: ");
Serial.print(hour(newTime));
Serial.print(":");
Serial.print(minute(newTime));
Serial.print(":");
Serial.print(second(newTime));
//Serial.print(fread("%02d", second(newTime))); //(second(newTime));
//String formattedTime = String(format("%02d:%02d:%02d", hour, minute, second));
Serial.print(" on ");
Serial.print(month(newTime));
Serial.print("/");
Serial.print(day(newTime));
Serial.print("/");
Serial.println(year(newTime));
}
void loop() {
// Nothing to do here
}
Thanks all,
I'll use the recommendation.
I was hoping to use my initial code code because it was so compact without the Serial.print commands. I'm just trying to convert GSM modem time ( with timezone +/-quarters) to local time. The SIM7600/7670 modules no longer have the (CLTS) local time stamp AT command. I'm just getting GMT time with -24(6 Hr) from the CCLK command.
The major problem with the code you posted is that you are subtracting -6 hours (that is NEGATIVE 6 hours), which in effects ADDS 6 hours to the GMT time.
The non-printf method is not that complicated, just print a leading zero if the value is less than 10. Most useful when you are low on program memory and don't want to include all the code needed for sprintf().
#include <TimeLib.h>
void setup() {
Serial.begin(9600);
// Set the current time (e.g., 22nd February 2025, 19:20:00)
setTime(1, 30, 0, 23, 2, 2025);
// Number of hours to subtract
int qtrs = 24; // CST difference from GMT
//int hrs = qtrs/4;
int hoursToSubtract = qtrs/4;
// Calculate the new time
time_t newTime = now() - (hoursToSubtract * SECS_PER_HOUR);
// Print the new time
Serial.print("New Time: ");
if (hour(newTime) < 10) Serial.print('0');
Serial.print(hour(newTime));
Serial.print(":");
if (minute(newTime) < 10) Serial.print('0');
Serial.print(minute(newTime));
Serial.print(":");
if (second(newTime) < 10) Serial.print('0');
Serial.print(second(newTime));
//Serial.print(fread("%02d", second(newTime))); //(second(newTime));
//String formattedTime = String(format("%02d:%02d:%02d", hour, minute, second));
Serial.print(" on ");
Serial.print(month(newTime));
Serial.print("/");
if (day(newTime) < 10) Serial.print('0');
Serial.print(day(newTime));
Serial.print("/");
Serial.println(year(newTime));
}
void loop() {
}
If your location has daylight savings time, you might want to use the Timezone library by Jack Christensen, which will handle the changes between standard and daylight savings time.
That would be the simple approach, otherwise there needs to be a method of telling the device which time zone you are in. Not to mention the date the time changes is subject to political whims.
I had an alarm clock that tried to do it automatically (I assume with its built in timezone library code) but the political whim changed, and I then had to reset the time 4x per year.