0

I have this test code:

  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 }

The output is:

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

Lines 7 and 8 are actually timestamps (seconds since Epoch) as unsigned long passed from a calling function (I just hardcoded it here).

Lines 10 and 11 is my concern. I need to obtain the struct tm info of the two timestamps i and j. Basically, I need to get the month of info1 and compare it against the month of info2, etc.

Doing the prints on Lines 13 to 25, info1 and info2 returns the same value (i.e same seconds, same minutes, same hours, etc).

Two questions:

  1. Why do they have the same values?
  2. How should I obtain different values of info1 and info2?
4

2 回答 2

1

文档清楚地说明了原因:

四个函数 asctime()、ctime()、gmtime() 和 localtime() 返回指向静态数据的指针,因此不是线程安全的。

换句话说,您在每次调用时都会获得一个指向标准库所拥有的相同静态结构的指针。您需要复制数据以防止这种情况发生。

于 2013-04-18T09:04:01.167 回答
1

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;
  }
于 2013-04-18T09:10:36.737 回答