简体   繁体   English

Char 到 time_t 的转换总是返回 Wed Dec 31 23:59:59 1969

[英]Char to time_t conversion always returns Wed Dec 31 23:59:59 1969

I am working on some code for my ESP8266 in Arduino.我正在 Arduino 中为我的 ESP8266 编写一些代码。 I want to send a timestamp to another ESP via UDP.我想通过 UDP 向另一个 ESP 发送时间戳。

In the setup(){ I initialize the time usingsetup(){我初始化时间使用

configTime(3 * 3600, 0, "pool.ntp.org", "time.nist.gov"); 

and in the loop(){并在loop(){

time_t timestamp = time(nullptr);

to get the time.得到时间。 I then send it to another ESP然后我将它发送到另一个 ESP

Udp.write(ctime(&timestamp));

Where I need to make a time_t again to compare it to the actual time.我需要再次制作 time_t 以将其与实际时间进行比较。

But for testing purposes, I send time as a char through terminal in UDP using echo -n "Fri Feb 28 14:44:11 2020" >/dev/udp/192.168.43.248/4210 from my MacBook Pro (MacOS Mojave).但出于测试目的,我使用echo -n "Fri Feb 28 14:44:11 2020" >/dev/udp/192.168.43.248/4210从我的 MacBook Pro (MacOS Mojave) 通过 UDP 中的终端发送时间作为字符。

The Serial Monitor shows the in- and output is the following:串行监视器显示输入和输出如下:

UDP packet contents: Fri Feb 28 14:44:11 2020
Wed Dec 31 23:59:59 1969

My Code is:我的代码是:

time_t convertStringToTime_t(char timestamp []) {
  time_t result = time(NULL);
  const char *timestampString = timestamp;
  int weekDay, month, dd, hh, mm, ss, yyyy = 0;
  struct tm timestampFromString = {0};

  //input     Fri Feb 28 16:40:11 2020
  //output    Wed Dec 31 23:59:59 1969 TODO always same output?!

  sscanf(timestampString, "%s %s %d %d:%d:%d %d", &weekDay, &month, &dd, &hh, &mm, &ss, &yyyy);

  timestampFromString.tm_year = yyyy - 1900; //years since 1900
  timestampFromString.tm_mon = month - 1;
  timestampFromString.tm_wday = weekDay;
  timestampFromString.tm_mday = dd;
  timestampFromString.tm_hour = hh;
  timestampFromString.tm_min = mm;
  timestampFromString.tm_sec = ss;
  timestampFromString.tm_isdst = -1;

  result = mktime(&timestampFromString);
  // mktime   Convert tm structure to time_t
  return result;
}

I saw this thread but it doesn't help me at all :/ I also tried using sscanf but no difference.我看到了这个线程,但它根本没有帮助我:/我也尝试过使用sscanf但没有区别。 I think I do something wrong in scanf ?我想我在scanf做错了什么?

solved: Thanks to @thomachan I realized I tried to put the Month + Day chars into int's (I could solve that by converting the chars to ints).解决了:感谢@thomachan,我意识到我试图将月 + 日字符放入 int 中(我可以通过将字符转换为 int 来解决这个问题)。

But I listened to @Maxim Egorushkin and used the simpler approach using the time_t value:但是我听了@Maxim Egorushkin 并使用了使用 time_t 值的更简单的方法:

char *buffer;
time_t i = strtoul(incomingPacket, &buffer, 10);

If you have the choice, I suggest you pass a ISO-8601 timestamp instead.如果您有选择,我建议您改为传递ISO-8601 时间戳 That looks like那看起来像

2020-03-03T10:28:59

and is trivially parsed with sscanf (which gives you components) or strptime (which fills in a struct tm ).并使用sscanf (它为您提供组件)或strptime (填充struct tm )进行简单解析。 If your platform does not provide a strptime implementation, you can use one from BSD.如果您的平台不提供strptime实现,您可以使用 BSD 中的一个。

If you don't require human-readable time, you can send the value of time_t - the number of seconds since Unix epoch.如果您不需要人类可读的时间,您可以发送time_t的值 - 自 Unix 纪元以来的秒数。

date +%s > /dev/udp/192.168.43.248/4210

The receiver can parse it from string to time_t with std::strtoul .接收者可以使用std::strtoul将其从字符串解析为time_t

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM