diff --git a/src/esp_date/date.cpp b/src/esp_date/date.cpp index 725b7e9..8deeb2a 100644 --- a/src/esp_date/date.cpp +++ b/src/esp_date/date.cpp @@ -32,6 +32,13 @@ #warning "ESPDate detected 32-bit time_t; dates beyond 2038 may overflow." #endif +#ifdef ESPDATE_FORCE_32BIT_EPOCH +#warning "ESPDate ESPDATE_FORCE_32BIT_EPOCH enabled; dates beyond 2038 may overflow." +// some implementations of time_t do not clear the upper bits when time_t is 64-bit and +// leave the upper bits garbage, which causes issues when the value is cast back to int64_t. +// NOTE: enableing this fix reintroduces the year 2038 problem. +#endif + using Utils = ESPDateUtils; namespace { @@ -69,6 +76,11 @@ void ESPDate::handleSntpSync(struct timeval *tv) { if (tv) { syncedEpoch = static_cast(tv->tv_sec); } + +#ifdef ESPDATE_FORCE_32BIT_EPOCH + syncedEpoch &= std::numeric_limits::max(); +#endif + const DateTime syncedAtUtc{syncedEpoch}; if (activeNtpSyncOwner_) { activeNtpSyncOwner_->lastNtpSync_ = syncedAtUtc; @@ -354,7 +366,13 @@ bool ESPDate::applyNtpConfig() const { } DateTime ESPDate::now() const { - return DateTime{static_cast(time(nullptr))}; + auto epoch = static_cast(time(nullptr)); + +#ifdef ESPDATE_FORCE_32BIT_EPOCH + epoch &= std::numeric_limits::max(); +#endif + + return DateTime{epoch}; } DateTime ESPDate::nowUtc() const {