Timestamp in milliseconds

How do you get the timestamp in milliseconds for #include "time.h"?

The way I do it it fees a bit hacky.


#include "time.h"

int64_t timestamp;
int64_t test;


const char* ntpServer = "pool.ntp.org";


unsigned long getTime() {
  time_t now;
  struct tm timeinfo;
  if (!getLocalTime(&timeinfo)) {
    return(0);
  }
  time(&now);
  return now;
}


void setup(){
  Serial.begin(115200);
 configTime(0, 0, ntpServer);

}

void loop(){
    //Get current timestamp
    timestamp = getTime();
    Serial.print ("time: ");
    Serial.println (timestamp);
    test = timestamp * 1000;
    Serial.print ("test: ");
    Serial.println (test);
}

What microcontroller?
Why do you specify getTime to be a unsigned long but you return the value of now which is of type time_t?

see time_t here:

https://en.cppreference.com/w/c/chrono/time_t

value holding the number of seconds

so yes, multiplied by 1000 will give you some kind of "microseconds"

ESP32

may be you want to check if this can be reused under the Arduino Framework:

Looks like the same hacky way of *1000, thanks

not really, there is a link and a code snippet how to use ยตsec * 1000 ... which might give you milliseconds...

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.