gpt4 book ai didi

c - 由单独的本地时间调用的两个 struct tm info 返回相同的值

转载 作者:太空宇宙 更新时间:2023-11-04 08:57:50 32 4
gpt4 key购买 nike

我有这个测试代码:

  1 #include <stdio.h>
2 #include <time.h>
3
4 int main() {
5 struct tm *info1;
6 struct tm *info2;
7 unsigned long i = 100000000;
8 unsigned long j = 200000000;
9
10 info1 = localtime((time_t *) &i);
11 info2 = localtime((time_t *) &j);
12
13 printf("%s(): info1->tm_sec = %d\n", __func__, info1->tm_sec);
14 printf("%s(): info1->tm_min = %d\n", __func__, info1->tm_min);
15 printf("%s(): info1->tm_hour = %d\n", __func__, info1->tm_hour);
16 printf("%s(): info1->tm_mday = %d\n", __func__, info1->tm_mday);
17 printf("%s(): info1->tm_mon = %d\n", __func__, info1->tm_mon);
18 printf("%s(): info1->tm_year = %d\n", __func__, info1->tm_year);
19
20 printf("%s(): info2->tm_sec = %d\n", __func__, info2->tm_sec);
21 printf("%s(): info2->tm_min = %d\n", __func__, info2->tm_min);
22 printf("%s(): info2->tm_hour = %d\n", __func__, info2->tm_hour);
23 printf("%s(): info2->tm_mday = %d\n", __func__, info2->tm_mday);
24 printf("%s(): info2->tm_mon = %d\n", __func__, info2->tm_mon);
25 printf("%s(): info2->tm_year = %d\n", __func__, info2->tm_year);
26
27
28
29 return 0;
30 }

输出是:

main(): info1->tm_sec = 20
main(): info1->tm_min = 33
main(): info1->tm_hour = 3
main(): info1->tm_mday = 4
main(): info1->tm_mon = 4
main(): info1->tm_year = 76
main(): info2->tm_sec = 20
main(): info2->tm_min = 33
main(): info2->tm_hour = 3
main(): info2->tm_mday = 4
main(): info2->tm_mon = 4
main(): info2->tm_year = 76

第 7 行和第 8 行实际上是时间戳(自 Epoch 以来的秒数),作为从调用函数传递的 unsigned long(我只是在这里硬编码)。

第 10 行和第 11 行是我关心的问题。我需要获取两个时间戳 ijstruct tm 信息。基本上,我需要获取 info1 的月份并将其与 info2 的月份等进行比较。

在第 13 到 25 行打印,info1info2 返回相同的值(即相同的秒数、相同的分钟数、相同的小时数等)。

两个问题:

  1. 为什么他们有相同的值(value)观?
  2. 如何获取info1info2的不同值?

最佳答案

localtime() -> 它的返回值指向一个静态分配的结构,该结构可能会被后续调用任何日期和时间函数覆盖。所以使用 localtime_r() 将数据存储在用户提供的结构中。

#include <stdio.h>
#include <time.h>

int main() {
struct tm info1;
struct tm info2;
unsigned long i = 100000000;
unsigned long j = 200000000;

localtime_r((time_t *) &i,&info1);
localtime_r((time_t *) &j,&info2);

printf("%s(): info1->tm_sec = %d\n", __func__, info1.tm_sec);
printf("%s(): info1->tm_min = %d\n", __func__, info1.tm_min);
printf("%s(): info1->tm_hour = %d\n", __func__, info1.tm_hour);
printf("%s(): info1->tm_mday = %d\n", __func__, info1.tm_mday);
printf("%s(): info1->tm_mon = %d\n", __func__, info1.tm_mon);
printf("%s(): info1->tm_year = %d\n", __func__, info1.tm_year);

printf("%s(): info2->tm_sec = %d\n", __func__, info2.tm_sec);
printf("%s(): info2->tm_min = %d\n", __func__, info2.tm_min);
printf("%s(): info2->tm_hour = %d\n", __func__, info2.tm_hour);
printf("%s(): info2->tm_mday = %d\n", __func__, info2.tm_mday);
printf("%s(): info2->tm_mon = %d\n", __func__, info2.tm_mon);
printf("%s(): info2->tm_year = %d\n", __func__, info2.tm_year);



return 0;
}

关于c - 由单独的本地时间调用的两个 struct tm info 返回相同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16078627/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com