c - 对 strftime() 的输出结果感到困惑

标签 c date time

仔细查看 time.h 多次后,我编写了以下函数:

void output_date ( int day, int month, int year ) {
    char buffer[64] = "";
    struct tm *e_time = calloc( (size_t) 1, sizeof(struct tm) );

    e_time->tm_year = year - 1900;
    e_time->tm_mon = month - 1;
    e_time->tm_mday = day;
    e_time->tm_hour = 0;
    e_time->tm_min = 0;
    e_time->tm_sec = 0;
    e_time->tm_isdst = -1;

    /* strftime ( buffer, 64, (char *)0, e_time ); */
    strftime ( buffer, 64, "%a %b %e %H:%M:%S %Z (%z) %Y", e_time );

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

    free(e_time);
    e_time = NULL;

}

然后我用一系列可能的输入调用这个函数,除了 像这样奇怪的输出:

Sun Jul  8 00:00:00  () 2013
Sun Jul  9 00:00:00  () 2013
Sun Jul 10 00:00:00  () 2013
Sun Jul 11 00:00:00  () 2013
Sun Jul 12 00:00:00  () 2013

当我将格式字符串测试为 strftime 时,我看到了不错的结果:

$ date -u "+%a %b %e %H:%M:%S %Z (%z) %Y"
Sat Oct 12 00:40:05 GMT (+0000) 2013

我什至通过调试器单步执行并看到了相同的结果 奇怪的结果:

stopped in main at line 27 in file "flight.c"
   27                       output_date ( day+1, month+1, year );
(dbx) print day+1, month+1, year
day+1 = 1
month+1 = 1
year = 1977

(dbx) step                      
stopped in output_date at line 43 in file "flight.c"
   43       char buffer[64] = "";
(dbx) step
stopped in output_date at line 44 in file "flight.c"
   44       struct tm *e_time = calloc( (size_t) 1, sizeof(struct tm) );
(dbx) step
stopped in output_date at line 46 in file "flight.c"
   46       e_time->tm_year = year - 1900;
(dbx) print e_time
e_time = 0x100101640
(dbx) step        
stopped in output_date at line 47 in file "flight.c"
   47       e_time->tm_mon = month - 1;
(dbx) step
stopped in output_date at line 48 in file "flight.c"
   48       e_time->tm_mday = day;
(dbx) step
stopped in output_date at line 49 in file "flight.c"
   49       e_time->tm_hour = 0;
(dbx) step
stopped in output_date at line 50 in file "flight.c"
   50       e_time->tm_min = 0;
(dbx) step
stopped in output_date at line 51 in file "flight.c"
   51       e_time->tm_sec = 0;
(dbx) step
stopped in output_date at line 52 in file "flight.c"
   52       e_time->tm_isdst = -1;
(dbx) step
stopped in output_date at line 55 in file "flight.c"
   55       strftime ( buffer, 64, "%a %b %e %H:%M:%S %Z (%z) %Y", e_time );
(dbx) print *e_time
*e_time = {
    tm_sec   = 0
    tm_min   = 0
    tm_hour  = 0
    tm_mday  = 1
    tm_mon   = 0
    tm_year  = 77
    tm_wday  = 0
    tm_yday  = 0
    tm_isdst = -1
}
(dbx) step        
stopped in output_date at line 57 in file "flight.c"
   57       printf ( "%s\n", buffer );
(dbx) print buffer
buffer = "Sun Jan  1 00:00:00  () 1977"
(dbx) quit    

事实上,我得到的只是一周中的某一天是星期日和正确的月份 正确的日期和年份。没有多少其他的似乎是正确的。

我是否遗漏了一些明显的东西?

最佳答案

你给字段tm_isdst赋了-1的值,这意味着夏令时不可用,把它改成1如果夏令时有效,如果不是,则为 0

正如@Paul Griffiths 指出的那样,调用mktime(e_time) 是更好的选择,mktime 函数将填充分解的e_time,因为它也会修复 tm_wdaytm_day 等文件。对于tm_isdst字段,遵循这样的规则:

a positive or zero value for tm_isdst causes the mktime function to presume initially that Daylight Saving Time, respectively, is or is not in effect for the specified time. A negative value causes it to attempt to determine whether Daylight Saving Time is in effect for the specified time.

关于c - 对 strftime() 的输出结果感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19329279/

相关文章:

Javascript Date 使用 UTC 时间来校正 GMT 时间

c - 在 C 中定义结构体

c++ - 当中断到来时如何退出 sleep()?

C: 当我将值放在括号中时,打印 %c 不起作用

bash - 如何获取上周四(含)?

go - time.Location()返回“Local”,即使/etc/localtime是正确的

将字符串输入与另一个字符串进行比较

r - 日期作为因子的错误转换为日期

excel - 获取最后一个值的月份的公式

PHP - 将日期转换为 YYYY-MM-DDTHH :MM:SS