Convert NTP timestamp to UNIX timestamp

vinayakk28
Posts: 5
Joined: Wed Sep 19, 2018 10:21 am

Convert NTP timestamp to UNIX timestamp

Postby vinayakk28 » Wed Sep 19, 2018 11:40 am

Hi,
I am working on ESP32-WROOM-32D running sntp example.
After I receive time from NTP server, I need to convert it to millisecond or UNIX timestamp.
Can you help me how to convert NTP server timestamp to UNIX timestamp?
Code:
char strftime_buf[64];
// Set timezone to india Standard Time
setenv("TZ", "CST-5:30", 1);
tzset();
localtime_r(&now, &timeinfo);
strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo);
ESP_LOGI(TAG, "The current date/time in India is: %s", strftime_buf);


o/p : example: The current date/time in India is: Wed Sep 19 16:13:07 2018

I need in UNIX timestamp 1537357107069

Help is appreciated!

Regards
Vinayak

ESP_igrr
Posts: 1144
Joined: Tue Dec 01, 2015 8:37 am

Re: Convert NTP timestamp to UNIX timestamp

Postby ESP_igrr » Wed Sep 19, 2018 2:59 pm

That's going to be =now*1000

vinayakk28
Posts: 5
Joined: Wed Sep 19, 2018 10:21 am

Re: Convert NTP timestamp to UNIX timestamp

Postby vinayakk28 » Thu Sep 20, 2018 5:50 am

Hi,
I am able to print time in UNIX.
Can you please help me, how to add milliseconds after UNIX timestamp? 1537422385231 ?

code: time(&now);
printf("%ld\n", now);

o/p:
I (3130) example: Initializing SNTP
I (3130) example: Waiting for system time to be set... (1/10)
1537422385

Thanks

ESP_igrr
Posts: 1144
Joined: Tue Dec 01, 2015 8:37 am

Re: Convert NTP timestamp to UNIX timestamp

Postby ESP_igrr » Thu Sep 20, 2018 7:33 am

Perhaps something like

Code: Select all

struct timeval tv;
gettimeofday(&tv, NULL);
int64_t milliseconds = tv.tv_sec * 1000LL + tv.tv_usec / 1000LL;

vinayakk28
Posts: 5
Joined: Wed Sep 19, 2018 10:21 am

Re: Convert NTP timestamp to UNIX timestamp

Postby vinayakk28 » Thu Sep 20, 2018 9:09 am

Thank you! I got desired output.

Who is online

Users browsing this forum: DerSpatz and 10 guests