Convert NTP timestamp to UNIX timestamp
-
- Posts: 8
- Joined: Wed Sep 19, 2018 10:21 am
Convert NTP timestamp to UNIX timestamp
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
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
Re: Convert NTP timestamp to UNIX timestamp
That's going to be =now*1000
-
- Posts: 8
- Joined: Wed Sep 19, 2018 10:21 am
Re: Convert NTP timestamp to UNIX timestamp
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
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
Re: Convert NTP timestamp to UNIX timestamp
Perhaps something like
Code: Select all
struct timeval tv;
gettimeofday(&tv, NULL);
int64_t milliseconds = tv.tv_sec * 1000LL + tv.tv_usec / 1000LL;
-
- Posts: 8
- Joined: Wed Sep 19, 2018 10:21 am
Re: Convert NTP timestamp to UNIX timestamp
Thank you! I got desired output.
Re: Convert NTP timestamp to UNIX timestamp
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...
I can't manage the addition of time_t and int64_t variables...
-
- Posts: 364
- Joined: Mon Jan 04, 2021 2:06 pm
Re: Convert NTP timestamp to UNIX timestamp
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.
https://docs.espressif.com/projects/esp ... _time.html
If you're still facing issues let us know.
Re: Convert NTP timestamp to UNIX timestamp
Hi, thanks for the reply.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.
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!
-
- Posts: 364
- Joined: Mon Jan 04, 2021 2:06 pm
Re: Convert NTP timestamp to UNIX timestamp
Hi,
Do you have more details about your problem?
I've tested here and I got exactly the expected result.
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: No registered users and 159 guests