Skip to content

Commit ef15084

Browse files
rishabhvaishRAR
authored andcommitted
refactor: skip time update on implausible delta instead of capping
Per reviewer feedback, instead of capping the delta to 1 hour (which still causes a potentially large time jump), skip the update entirely when the delta exceeds a plausible threshold. The clock will resync naturally on the next setTime() call from the CSMS. This ensures Clock::now() never produces time jumps, maintaining consistency for transaction timestamps and other time-sensitive operations.
1 parent ceb14cc commit ef15084

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

src/MicroOcpp/Core/Time.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -361,11 +361,14 @@ const Timestamp &Clock::now() {
361361

362362
// Guard against implausible time jumps caused by overflow or
363363
// concurrency issues (see https://github.com/matth-x/MicroOcpp/issues/421).
364-
// Cap delta to 1 hour; if the real elapsed time exceeds this, the clock
365-
// will catch up incrementally on subsequent calls.
366-
const decltype(delta) MAX_DELTA_MS = 3600UL * 1000UL; // 1 hour
367-
if (delta > MAX_DELTA_MS) {
368-
delta = MAX_DELTA_MS;
364+
// If the delta exceeds a plausible threshold, skip the update entirely
365+
// rather than applying a large time jump. The clock will resync on the
366+
// next setTime() call from the CSMS.
367+
const decltype(delta) MAX_PLAUSIBLE_DELTA_MS = 3600UL * 1000UL; // 1 hour
368+
if (delta > MAX_PLAUSIBLE_DELTA_MS) {
369+
// Reset lastUpdate so the next call starts fresh from here
370+
lastUpdate = tReading;
371+
return currentTime;
369372
}
370373

371374
#if MO_ENABLE_TIMESTAMP_MILLISECONDS

0 commit comments

Comments
 (0)