Convert NTP timestamp to UNIX timestamp

vinayakk28
Posts: 8
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: 2067
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: 8
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: 2067
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: 8
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.

q220085
Posts: 3
Joined: Tue Jan 05, 2021 12:15 am

Re: Convert NTP timestamp to UNIX timestamp

Postby q220085 » Tue Jan 05, 2021 12:19 am

Hi, could you please share how exactly to add milliseconds after the Unix timestamps?
I can't manage the addition of time_t and int64_t variables...

ESP_Minatel
Posts: 361
Joined: Mon Jan 04, 2021 2:06 pm

Re: Convert NTP timestamp to UNIX timestamp

Postby ESP_Minatel » Tue Jan 05, 2021 9:20 am

For future reference to this question, please see the official Espressif documentation on this topic:

https://docs.espressif.com/projects/esp ... _time.html

If you're still facing issues let us know.

q220085
Posts: 3
Joined: Tue Jan 05, 2021 12:15 am

Re: Convert NTP timestamp to UNIX timestamp

Postby q220085 » Tue Jan 05, 2021 9:40 am

ESP_Minatel wrote: For future reference to this question, please see the official Espressif documentation on this topic:

https://docs.espressif.com/projects/esp ... _time.html

If you're still facing issues let us know.
Hi, thanks for the reply.
I did read this document but still can't figure out how to get the current time up to "millisecond" accuracy in either Unix time format or human-readable format.

e.g.,
1609839512"321"
2021-01-05 10:39:55".321"

I will appreciate any help!

ESP_Minatel
Posts: 361
Joined: Mon Jan 04, 2021 2:06 pm

Re: Convert NTP timestamp to UNIX timestamp

Postby ESP_Minatel » Tue Jan 05, 2021 11:42 am

Hi,

Do you have more details about your problem?

I've tested here and I got exactly the expected result.
I (9692) example: UNIX time in mseconds: 1609846783365
I (9702) example: UNIX time in useconds: 1609846783365102

Code: Select all

    struct timeval tv_now;
    gettimeofday(&tv_now, NULL);

    int64_t time_ms = (int64_t)tv_now.tv_sec * 1000LL + (int64_t)tv_now.tv_usec / 1000LL;
    int64_t time_us = (int64_t)tv_now.tv_sec * 1000000L + (int64_t)tv_now.tv_usec;

    ESP_LOGI(TAG, "UNIX time in mseconds: %lld", time_ms);
    ESP_LOGI(TAG, "UNIX time in useconds: %lld", time_us);

Who is online

Users browsing this forum: Google [Bot] and 114 guests