Skip to content

update() returns true when update was not performed #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
KentWalker opened this issue Sep 13, 2018 · 9 comments
Closed

update() returns true when update was not performed #55

KentWalker opened this issue Sep 13, 2018 · 9 comments
Labels
conclusion: resolved Issue was resolved topic: code Related to content of the project itself type: imperfection Perceived defect in any part of project

Comments

@KentWalker
Copy link

Was there any particular reason for update() to return true when an actual update hadn't been performed (ie returning without a call to forceUpdate() )?

@bitsy
Copy link
Contributor

bitsy commented Sep 17, 2018

I have the same exact question. I have been wondering that myself and have already edited my local copy of the library to only return true if a successful update actually occurs:

bool NTPClient::update() {
  if ((millis() - this->_lastUpdate >= this->_updateInterval)     // Update after _updateInterval
    || this->_lastUpdate == 0) {                                // Update if there was no update yet.
    if (!this->_udpSetup) this->begin();                         // setup the UDP client if needed
    return this->forceUpdate();
  }
  return false;  // I changed this from true to false in NTPClient.cpp
}

Making the above edit allows the following code to be valid (which I currently use to sync my RTC with an NTP server):

// Only when timeClient successfully updates, adjust (sync) RTC time.
    if (timeClient.update()) {
        RTC.adjust(DateTime(timeClient.getEpochTime()));
    }

(RTC Library is RTClib from Adafruit.)

@bitsy
Copy link
Contributor

bitsy commented Sep 17, 2018

I submitted a pull request with the above change to NTPClient.cpp.

@jbrown123
Copy link
Contributor

It returns false only when an actual update happens and that update fails. I believe this is the appropriate behavior since getEpochTime() uses millis() to adjust for the elapsed time since the reply was received.

If you want to force an update to occur, then call forceUpdate() which also returns true/false based on whether it actually received a time update or not.

@bperkins011
Copy link

bperkins011 commented Mar 31, 2019

I agree with bitsy above - update() should return false if it does nothing and true when it actually performs an update - which really just ends up in a call to forceUpdate(). Once the NTP time is reset - I can then set another time with it - but I don't want to do it continuously for no reason in loop()

@bitsy
Copy link
Contributor

bitsy commented Sep 17, 2019

@jbrown123, I see your point: knowing when an update fails could be potentially useful information. However, knowing when an update occurs successfully is also useful (e.g. syncing an RTC only when an update occurs, as I show above).

If it is important to maintain an indication of failure, why not return an 8-bit integer value instead of a boolean? Such that, a failed update returns 0, a “non-update”* returns 1, and a successful update returns 2. It essentially maintains the current boolean behavior to avoid breaking other people’s implementations of the library yet also provides additional information to those who desire to know whether an update occurred at all.

*“Non-update”: when update() does not call forceUpdate() because the interval requirement has not been met

@jbrown123
Copy link
Contributor

jbrown123 commented Sep 17, 2019 via email

@aentinger
Copy link
Contributor

Fixed in #56 - therefore closing this issue.

@jbrown123
Copy link
Contributor

Note that 56 does something different than what bitsy suggested in his most recent post. The change in 56 absolutely does not distinguish between a failed update and no update.

@aentinger
Copy link
Contributor

Agreed, however the initial question/topic of this answer has been resolved - there is still #75 open which would satisfy the desire for further error return value refinement.

@per1234 per1234 changed the title Question: Is there a reason why update() returns true? update() returns true when update was not performed May 2, 2025
@per1234 per1234 added type: imperfection Perceived defect in any part of project conclusion: resolved Issue was resolved topic: code Related to content of the project itself labels May 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
conclusion: resolved Issue was resolved topic: code Related to content of the project itself type: imperfection Perceived defect in any part of project
Projects
None yet
Development

No branches or pull requests

6 participants