Skip to content

Provide epoch in milliseconds #79

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

Open
iogbole opened this issue Oct 20, 2019 · 10 comments
Open

Provide epoch in milliseconds #79

iogbole opened this issue Oct 20, 2019 · 10 comments
Labels
topic: code Related to content of the project itself type: enhancement Proposed improvement

Comments

@iogbole
Copy link

iogbole commented Oct 20, 2019

Additional context

Additional requests

@Majubs

This comment has been minimized.

@LPena10814

This comment has been minimized.

@MHotchin
Copy link

MHotchin commented Dec 7, 2019

It wouldn't be possible without changing the data type returned by getEpochTime(). Right now the unix time is 1,575,693,824. Multiplying by 1000 will overflow the maximum value that an unsigned int can hold.

Even if you did change the data type, the simple synchronization code used will never be accurate to the millisecond level - the answer would be more precise, but it would not much be more accurate. So, there's really no point.

@WhymustIhaveaname
Copy link

Need not change the datatype. Just add another float _current_epoc_dec to record the decimal part of the current time from the Epoch.

I solved this in Pull Request #102. I add a new function named get_millis() which will return a float in [0,1000) recording the ms number of this second.

@fededim
Copy link

fededim commented May 13, 2020

@WhymustIhaveaname It would be useful to add a new method called getEpochTimeMs which returns a uint64_t containing the number of ms since 1-1-1970 because having 2 different calls (one getEpochTime and the other get_millis) could generate some drift if the second between the 2 calls changes (e.g. getepochtime returns 13/05/2020 08:41:59(.999) and get_millis returns 000 (meaning 13/05/2020 08:42:00.000)

@WhymustIhaveaname
Copy link

@fededim Thanks for your suggestion. I will add this function if my PR gets merged. But for now, you can add this function manually yourself.

unsigned long NTPClient::getEpochTimeMs() const {
  return 1000*this->_timeOffset + // User offset
         1000*this->_currentEpoc + // Epoc returned by the NTP server
         (millis() - this->_lastUpdate + this->_current_epoc_dec*1000); // Time since last update
}

@fededim

This comment has been minimized.

@WhymustIhaveaname

This comment has been minimized.

@fededim

This comment has been minimized.

@sylvanoMTL
Copy link

sylvanoMTL commented Jan 2, 2021

I confirm the program from @WhymustIhaveaname works well. Here is a screenshot of 2 esp32s flashed using his NTPClient.h and NTPclient.cpp and a loop running at 10ms. Button were release at the the nearly same time (witch is tricky to clear the consoles and press tiny buttons).

NTPClient_on_two different_esp32s

The main loop contain the following code:

void loop() {
  timeClient.update();
  Serial.print(timeClient.getFormattedTime());Serial.print(".");
    int ms = timeClient.get_millis();
    if(ms<10){
        Serial.print("00");Serial.println(ms);
    }
    else if(ms>=10 && ms<100){
        Serial.print("0");Serial.println(ms);
    }
    else{
      Serial.println(ms);
    }
  delay(10);
}

@per1234 per1234 changed the title Question: Is it possible to return the epoch time in milliseconds? Provide epoch in milliseconds Mar 20, 2025
@per1234 per1234 added type: enhancement Proposed improvement topic: code Related to content of the project itself labels Mar 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: code Related to content of the project itself type: enhancement Proposed improvement
Projects
None yet
Development

No branches or pull requests

8 participants