When writing a C-language application, to get or print some time-related information, we often use some time functions that come with the C language, such as times, LocalTime, CTime, Mktime, and Asctime. But you may not notice that there are some interesting phenomena in this area, first of all, let's take a look at an example:
1 #include <stdio.h>
2 #include <time.h>
3
4 int main ()
5 {
6
7 time_t time_1, time_2;
8 struct tm *tm_1, *tm_2, *tm_3;
9 struct tm tm_4, tm_5;
10
11 printf("-------------------- PART I -------------------\n");
12
13 time_1 = time(NULL);
14 sleep(3);
15 time_2 = time(NULL);
16 printf("time1:%d time2:%d\n",time_1,time_2);
17
18 tm_1 = (struct tm*)localtime(&time_1);
19 tm_2 = (struct tm*)localtime(&time_2);
20 tm_3 = (struct tm*)localtime(&time_1);
21
22 printf("tm_1 ptr:%p tm_2 ptr:%p tm_3 ptr:%p\n",tm_1,tm_2,tm_3);
23 printf("asctime(tm_1):%s",asctime(tm_1));
24 printf("asctime(tm_2):%s",asctime(tm_2));
25 printf("asctime(tm_3):%s",asctime(tm_3));
26 }
Before looking at the output of this code, ask you two questions:
(1) Line 22nd, what is the relationship between the values of the struct TM structure tm_1, tm_2, and tm_3?
(2) 第23-26 line of output results, tm_2 time is really 3 seconds later than tm_1?
Next, let's take a look at the output of this piece of code:
--------------------Part I-------------------
time1:1340256774 time2:1340256777
Tm_1 ptr:0xfec6f48 tm_2 ptr:0xfec6f48 tm_3 ptr:0xfec6f48
Asctime (tm_1): Thu June 21 01:32:54 2012
Asctime (tm_2): Thu June 21 01:32:54 2012
Asctime (tm_3): Thu June 21 01:32:54 2012
is the print here as you expected? Yes, the tm_1, tm_2 and tm_3 in line 22nd actually point to the same address. In the realization of localtime function, a static internal struct TM structure is used to store the corresponding time information. Each call to the LocalTime function modifies the internal struct TM structure, which means that the struct will only hold the most recent call results. Therefore, the struct TM structure that localtime returns each time will also be the same, that is to point to the same address. This means that the subsequent 23rd to line 25th call to Asctime is actually passing in the same struct (a pointer to the same address), and as a result, it's not surprising that the time they hit is the same.
Let's look at the following code:
1 #include <stdio.h>
2 #include <time.h>
3
4 int Main ()
5 {
6
7 time_t time_1, time_2;
8 struct TM *tm_1, *tm_2, *tm_3;
9 struct TM Tm_4, tm_5;
10
printf ("--------------------part I-------------------\ n");
12
Time_1 = time (NULL);
Sleep (3);
time_2 = time (NULL);
printf ("time1:%d time2:%d\n", time_1,time_2);
17
Tm_1 = (struct tm*) localtime (&time_1);
Tm_2 = (struct tm*) localtime (&time_2);
Tm_3 = (struct tm*) localtime (&time_1);
21st
printf ("Tm_1 ptr:%p tm_2 ptr:%p tm_3 ptr:%p\n", tm_1,tm_2,tm_3);
printf ("Asctime (tm_1):%s", Asctime (tm_1));
printf ("Asctime (tm_2):%s", Asctime (tm_2));
printf ("Asctime (tm_3):%s", Asctime (Tm_3));
26
27
printf ("--------------------part II-------------------\ n");
29
Time_1 = time (NULL);
Sleep (3);
time_2 = time (NULL);
printf ("time1:%d time2:%d\n", time_1,time_2);
34
tm_4 = * (struct tm*) localtime (&time_1));
tm_5 = * (struct tm*) localtime (&time_2));
37
printf ("Tm_4 ptr:%p tm_5 ptr:%p\n", &tm_4,&tm_5);
Tm_4 ("sec:%d tm_5 sec:%d\n", tm_4.tm_sec,tm_5.tm_sec);
40
printf ("Asctime (&tm_4):%sasctime (&tm_5):%s", Asctime (&tm_4), Asctime (&tm_5));
printf ("Asctime (&tm_4) ptr:%p asctime (&tm_5) ptr:%p\n", Asctime (&tm_4), Asctime (&tm_5));
43
printf ("Asctime (&tm_4):%s", Asctime (&tm_4));
-printf ("Asctime (&tm_5):%s", Asctime (&tm_5));