获取ntp时间(以毫秒为单位)>

问题描述 投票:0回答:1

我正在使用ESP32模块

,并且我试图以毫秒来获取NTP时间。我设法使用struct tm和函数getLocalTime()以秒为单位获取时间,没有任何问题。

我在论坛和互联网上阅读,我必须使用struct timeval

和功能gettimeofday()来实现此目的。所以我在代码中相应地替换了结构和函数,但是现在我已经没时间了...

我的代码如下:

void printLocalTime()
{
  //When using struct tm and getLocalTime() I can get the time without poblem in seconds
  struct timeval tv;
  if (!gettimeofday(&tv, NULL)) {
    Serial.println("Failed to obtain time");
    return;
  }
  long int sec = tv.tv_sec*1000LL;
  Serial.println(sec);
  long int temp = tv.tv_usec/1000LL;
  Serial.println(temp);
}

[运行此命令时,我得到的只是“无法获取时间” ...

PS:我正在使用arduino IDE,并且已包含sys / time.h

有人可以帮我吗?

非常感谢

我正在使用ESP32模块,我正在尝试获取NTP时间(以毫秒为单位)。我设法使用struct tm和函数getLocalTime()以秒为单位获取时间,没有任何问题。我读...

arduino-ide esp32 ntp milliseconds
1个回答
0
投票

由于(原始)POSIX命令具有以下结构

© www.soinside.com 2019 - 2024. All rights reserved.