Some more fixes to usage of gmtime (#1150)

* Some more fixes to usage of gmtime

* Second version for drv_ntp_events.c - changing to time_t (hence do casting in calculations)

* Fix for unset NTP time if no NTP drivers present. Rely on "fake" definitions of
NTP_GetCurrentTime() and NTP_GetCurrentTimeWithoutOffset() to make sure
code using time is not called, if it is not set.

* If time is not set, give 1970-01-01T00:00:00 as startupUTC
This commit is contained in:
MaxineMuster
2024-03-27 23:47:28 +01:00
committed by GitHub
parent c07f66f381
commit eaaa3848b0
3 changed files with 14 additions and 13 deletions

View File

@@ -70,7 +70,7 @@ void Clock_Send(int type) {
char *p;
// NOTE: on windows, you need _USE_32BIT_TIME_T
ltm = gmtime((time_t*)&g_ntpTime);
ltm = gmtime(&g_ntpTime);
if (ltm == 0) {
return;

View File

@@ -15,7 +15,7 @@
#define M_PI 3.14159265358979323846264338327950288
#define LOG_FEATURE LOG_FEATURE_NTP
unsigned int ntp_eventsTime = 0;
time_t ntp_eventsTime = 0;
typedef struct ntpEvent_s {
byte hour;
@@ -166,12 +166,12 @@ void NTP_CalculateSunset(byte *outHour, byte *outMinute) {
}
#endif
void NTP_RunEventsForSecond(unsigned int runTime) {
void NTP_RunEventsForSecond(time_t runTime) {
ntpEvent_t *e;
struct tm *ltm;
// NOTE: on windows, you need _USE_32BIT_TIME_T
ltm = gmtime((time_t*)&runTime);
ltm = gmtime(&runTime);
if (ltm == 0) {
return;
@@ -218,17 +218,17 @@ void NTP_RunEvents(unsigned int newTime, bool bTimeValid) {
}
// old time invalid, but new one ok?
if (ntp_eventsTime == 0) {
ntp_eventsTime = newTime;
ntp_eventsTime = (time_t)newTime;
return;
}
// time went backwards
if (newTime < ntp_eventsTime) {
ntp_eventsTime = newTime;
ntp_eventsTime = (time_t)newTime;
return;
}
if (ntp_events) {
// NTP resynchronization could cause us to skip some seconds in some rare cases?
delta = newTime - ntp_eventsTime;
delta = (unsigned int)((time_t)newTime - ntp_eventsTime);
// a large shift in time is not expected, so limit to a constant number of seconds
if (delta > 100)
delta = 100;
@@ -236,7 +236,7 @@ void NTP_RunEvents(unsigned int newTime, bool bTimeValid) {
NTP_RunEventsForSecond(ntp_eventsTime + i);
}
}
ntp_eventsTime = newTime;
ntp_eventsTime = (time_t)newTime;
}
#if ENABLE_NTP_SUNRISE_SUNSET
@@ -312,7 +312,7 @@ commandResult_t CMD_NTP_AddClockEvent(const void *context, const char *cmd, cons
#if ENABLE_NTP_SUNRISE_SUNSET
uint8_t hour_b, minute_b;
int sunflags = 0;
struct tm *ltm = gmtime((time_t*) &ntp_eventsTime);
struct tm *ltm = gmtime(&ntp_eventsTime);
#endif
Tokenizer_TokenizeString(args, TOKENIZER_ALTERNATE_EXPAND_AT_START);

View File

@@ -653,14 +653,15 @@ static int http_tasmota_json_status_generic(void* request, jsonCb_t printer) {
JSON_PrintKeyValue_String(request, printer, "RestartReason", "HardwareWatchdog", true);
JSON_PrintKeyValue_Int(request, printer, "Uptime", g_secondsElapsed, true);
struct tm* ltm;
int ntpTime = NTP_GetCurrentTime() - g_secondsElapsed;
ltm = gmtime((time_t*)&ntpTime);
time_t ntpTime = 0; // if no NTP_time set, we will not change this value, but just stick to 0 and hence "fake" start of epoch 1970-01-01T00:00:00
if (NTP_GetCurrentTimeWithoutOffset() > g_secondsElapsed) { // would be negative else, leading to unwanted results when converted to (unsigned) time_t
ntpTime = (time_t)NTP_GetCurrentTimeWithoutOffset() - (time_t)g_secondsElapsed;
}
ltm = gmtime(&ntpTime);
if (ltm != 0) {
printer(request, "\"StartupUTC\":\"%04d-%02d-%02dT%02d:%02d:%02d\",", ltm->tm_year + 1900, ltm->tm_mon + 1, ltm->tm_mday, ltm->tm_hour, ltm->tm_min, ltm->tm_sec);
}
else {
}
JSON_PrintKeyValue_Int(request, printer, "Sleep", 50, true);
JSON_PrintKeyValue_Int(request, printer, "CfgHolder", 4617, true);