NTPClient icon indicating copy to clipboard operation
NTPClient copied to clipboard

Can we have a setEpochTime() method?

Open sheffieldnikki opened this issue 7 years ago • 3 comments

Could we have a way of manually setting the internal time stored by NTPClient please? e.g., a setEpochTime(t) to match the existing getEpochTime().

My use case is reading the NTP time over the network no more than once a day, between which the ESP8266 spends much of its time in deep sleep mode, so I'm storing the predicted wake time/date in RTC memory, and I'd like to write that into NTPClient when REASON_DEEP_SLEEP_AWAKE (but not for other reset methods like power on).

Thanks

sheffieldnikki avatar Mar 21 '17 22:03 sheffieldnikki

Why not wrap that into a function that reads your cached time and if you don't have a cached time, it reads from NTPClient?

TyIsI avatar Apr 22 '17 23:04 TyIsI

Sure, but that is just adding an extra layer. NTPClient already has a private variable to store that timestamp, and functions that use it.

sheffieldnikki avatar Apr 22 '17 23:04 sheffieldnikki

I've added the obvious code, in case anyone wants to use it.

NTPClient.h:

/**
 * Replace the NTP-fetched time with seconds since Jan. 1, 1970
 */
void setEpochTime(unsigned long secs);

NTPClient.cpp:

void NTPClient::setEpochTime(unsigned long secs) {
  this->_currentEpoc = secs;
}

sheffieldnikki avatar Apr 24 '17 21:04 sheffieldnikki