【已解决】IDF4.4 的SNTP接口是否有获取毫秒级别时间的方法

kangzy4
Posts: 6
Joined: Wed Jan 11, 2023 7:18 am

【已解决】IDF4.4 的SNTP接口是否有获取毫秒级别时间的方法

Postby kangzy4 » Fri Apr 21, 2023 9:32 am

基于例程中SNTP接口实现获取网络时间戳后,发现精度只能到秒级,SNTP是否有获取毫秒级时间的办法呢?
// update 'now' variable with current time
time(&now);
// Set timezone to China Standard Time
setenv("TZ", "CST-8", 1);
tzset();
localtime_r(&now, &timeinfo);
strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo);
ESP_LOGI(TAG, "The current date/time in Shanghai is: %s", strftime_buf);
ESP_LOGI(TAG, "timestamp = %ld", now);

ESP_LiuH
Posts: 42
Joined: Fri Feb 10, 2023 7:20 am

Re: IDF4.4 的SNTP接口是否有获取毫秒级别时间的方法

Postby ESP_LiuH » Tue Apr 25, 2023 9:13 am

long long platform_tick_get_ms(void)
{
struct timeval te;
gettimeofday(&te, NULL); // get current time
long long milliseconds = te.tv_sec * 1000LL + te.tv_usec / 1000; // calculate milliseconds
// printf("milliseconds: %lld\n", milliseconds);
return milliseconds;
}

kangzy4
Posts: 6
Joined: Wed Jan 11, 2023 7:18 am

Re: IDF4.4 的SNTP接口是否有获取毫秒级别时间的方法

Postby kangzy4 » Fri May 19, 2023 6:38 am

感谢,已解决,原来是类型长度没定义好。

Who is online

Users browsing this forum: No registered users and 44 guests