关于 c:tm struct time.h 未规范化

tm struct time.h not normalizing

我正在向我的 tm 结构的时间(小时、分钟、秒)成员添加值,即使我正在使用 mktime(),它们也没有更新/规范化这是代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
struct tm timeStruct;
char buffer[80];

timeStruct.tm_year = 2016 - 1900;
timeStruct.tm_mon = 3;
timeStruct.tm_mday = 32;
timeStruct.tm_hour = 23;
timeStruct.tm_min = 59;
timeStruct.tm_sec = 59;
timeStruct.tm_isdst = -1;

printf("Date before adding interval: \
"
);
mktime(&timeStruct);
strftime(buffer, sizeof(buffer),"%c", &timeStruct);
printf(buffer);

printf("\
the year is %d\
"
, timeStruct.tm_year );
printf("the month is %d\
"
, timeStruct.tm_mon );
printf("the day is %d\
"
, timeStruct.tm_mday );
printf("the hours are %d\
"
, timeStruct.tm_hour );
printf("the minutes are %d\
"
, timeStruct.tm_min );
printf("the seconds are %d\
"
, timeStruct.tm_sec );

/*
 * Add intervals to time
 */

timeStruct.tm_sec += 2;
timeStruct.tm_min += 2;
timeStruct.tm_hour += 5;

printf("Date after adding interval: \
"
);
strftime(buffer, sizeof(buffer),"%c", &timeStruct);
printf(buffer);

printf("\
the year is %d\
"
, timeStruct.tm_year );
printf("the month is %d\
"
, timeStruct.tm_mon );
printf("the day is %d\
"
, timeStruct.tm_mday );
printf("the hours are %d\
"
, timeStruct.tm_hour );
printf("the minutes are %d\
"
, timeStruct.tm_min );
printf("the seconds are %d\
"
, timeStruct.tm_sec );

控制台输出:1

这是控制台输出的打印输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Date before adding interval:
Mon May  2 23:59:59 2016
the year is 116
the month is 4
the day is 2
the hours are 23
the minutes are 59
the seconds are 59
Date after adding interval:
Mon May  2 28:61:61 2016
the year is 116
the month is 4
the day is 2
the hours are 28
the minutes are 61
the seconds are 61

我在 Windows 7 机器上使用 Eclipse,用 Cygwin 编译。


在您的代码中,仅在添加间隔之前调用 mktime()。您需要在添加间隔后调用它(以便它可以标准化更新的 timeStruct):

1
2
3
4
printf("Date after adding interval: \
"
);
mktime(&timeStruct);    // <--- here
strftime(buffer, sizeof(buffer),"%c", &timeStruct);

输出:

1
2
3
4
5
6
7
Tue May  3 05:02:01 2016
the year is 116
the month is 4
the day is 3
the hours are 5
the minutes are 2
the seconds are 1


这里是更正后的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <stdio.h>
#include <time.h>

struct tm timeStruct;
char buffer[80];

int main( void )
{
    timeStruct.tm_year = 2016 - 1900;
    timeStruct.tm_mon = 3;
    timeStruct.tm_mday = 32;
    timeStruct.tm_hour = 23;
    timeStruct.tm_min = 59;
    timeStruct.tm_sec = 59;
    timeStruct.tm_isdst = -1;

    printf("Date before adding interval: \
"
);
    if( (time_t)-1 == mktime(&timeStruct) )
    {
        perror("first call to mktime failed due to:");
    }

    if( 0 == strftime(buffer, sizeof(buffer),"%c", &timeStruct) )
    {
        perror("first call to strftime failed due to:");
    }

    printf("\
\
%s\
"
, buffer);

    printf("\
the year is %d\
"
, timeStruct.tm_year );
    printf("the month is %d\
"
, timeStruct.tm_mon );
    printf("the day is %d\
"
, timeStruct.tm_mday );
    printf("the hours are %d\
"
, timeStruct.tm_hour );
    printf("the minutes are %d\
"
, timeStruct.tm_min );
    printf("the seconds are %d\
"
, timeStruct.tm_sec );

    /*
     * Add intervals to time
     */

    timeStruct.tm_sec += 2;
    timeStruct.tm_min += 2;
    timeStruct.tm_hour += 5;
    timeStruct.tm_year = 2016 - 1900;
    timeStruct.tm_mon = 3;
    timeStruct.tm_mday = 32;
    timeStruct.tm_hour = 23;
    timeStruct.tm_min = 59;
    timeStruct.tm_sec = 59;
    timeStruct.tm_isdst = -1;

    printf("Date after adding first interval: \
"
);
    if( (time_t)-1 == mktime(&timeStruct) )
    {
        perror("second call to mktime failed due to:");
    }

    if( 0 == strftime(buffer, sizeof(buffer),"%c", &timeStruct) )
    {
        perror("second call to strftime failed due to:");
    }

    printf("\
\
%s\
"
, buffer);

    printf("\
the year is %d\
"
, timeStruct.tm_year );
    printf("the month is %d\
"
, timeStruct.tm_mon );
    printf("the day is %d\
"
, timeStruct.tm_mday );
    printf("the hours are %d\
"
, timeStruct.tm_hour );
    printf("the minutes are %d\
"
, timeStruct.tm_min );
    printf("the seconds are %d\
"
, timeStruct.tm_sec );

    /*
     * Add intervals to time
     */

    timeStruct.tm_sec += 2;
    timeStruct.tm_min += 2;
    timeStruct.tm_hour += 5;

    printf("Date after adding second interval: \
"
);
    if( (time_t)-1 == mktime(&timeStruct) )
    {
        perror("third call to mktime failed due to:");
    }

    if( 0 == strftime(buffer, sizeof(buffer),"%c", &timeStruct) )
    {
        perror("third call to strftime failed due to:");
    }

    printf("\
\
%s\
"
, buffer);

    printf("\
the year is %d\
"
, timeStruct.tm_year );
    printf("the month is %d\
"
, timeStruct.tm_mon );
    printf("the day is %d\
"
, timeStruct.tm_mday );
    printf("the hours are %d\
"
, timeStruct.tm_hour );
    printf("the minutes are %d\
"
, timeStruct.tm_min );
    printf("the seconds are %d\
"
, timeStruct.tm_sec );
}

这是当前/更正后的代码输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
Date before adding interval:


Mon May  2 23:59:59 2016

the year is 116
the month is 4
the day is 2
the hours are 23
the minutes are 59
the seconds are 59
Date after adding first interval:


Mon May  2 23:59:59 2016

the year is 116
the month is 4
the day is 2
the hours are 23
the minutes are 59
the seconds are 59
Date after adding second interval:


Tue May  3 05:02:01 2016

the year is 116
the month is 4
the day is 3
the hours are 5
the minutes are 2
the seconds are 1