ESP32-Time

Florent
Posts: 2
Joined: Mon Jul 13, 2020 12:49 pm

ESP32-Time

Postby Florent » Wed Jul 15, 2020 3:47 pm

Hi,

I want to get the time in milliseconds notation for example like this: HH:MM:SS:ms. How would I get this type of ouput?
I get the current time once via NTP-Client, this runs in the setup method:

Code: Select all

	configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
	printLocalTime();
and then I loop the printLocalTime() method:

Code: Select all

void printLocalTime(){
  struct tm timeinfo;
  if(!getLocalTime(&timeinfo)){
    Serial.println("Failed to obtain time");
    return;
  }
  char now[100];
  strftime(now, sizeof(now), "%Y-%m-%dT%H:%M:%S", &timeinfo);
  Serial.println(now);
}
Now since I am using the struct tm, I cant get the time in milliseconds precision. The getLocalTime has a second parameter that has something to do with milliseconds but I cant imagine that I can do something with this, maybe you guys know more.
What would be a good way of achieving this?
Thank you.

lbernstone
Posts: 666
Joined: Mon Jul 22, 2019 3:20 pm

Re: ESP32-Time

Postby lbernstone » Sat Jul 18, 2020 3:42 am

The toolchain includes a lot of stdlib time functions, including gettimeofday:

Code: Select all

  struct timeval tv;
  gettimeofday(&tv, NULL);  
  Serial.printf("microseconds: %d\n", tv.tv_usec);
The second parameter for getLocalTime is the length of time to wait for a valid result, so not relevant here.

Who is online

Users browsing this forum: Bing [Bot] and 47 guests