c - 为什么 C 语言中年份返回 116 而不是 2016?

标签 c date time.h

我有这个代码:

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

int main(void) {

    time_t rawtime = time(NULL);
    struct tm *ptm = localtime(&rawtime);
    printf("The time is: %02d:%02d:%02d\n", ptm->tm_hour, 
           ptm->tm_min, ptm->tm_sec);
    printf("The date is: %02d:%02d:%04d\n.", ptm->tm_mday, ptm->tm_mon, ptm->tm_year);
    return 0;
}

当我运行它时,它返回的 tm_year 值是 116,而不是 2016。谁能告诉我为什么?

最佳答案

tm_year 字段表示为自 1900 年以来的年份:https://linux.die.net/man/3/localtime

关于c - 为什么 C 语言中年份返回 116 而不是 2016?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41320298/

相关文章:

C : Including a source file instead of a header

php - 根据一天中的时间运行代码

c - Ncurses 和 realtime(在 C、unix 中实现)

我可以在同一个文件中使用来自 time.h 的两个 tm 结构吗?

c - 当我在 linux 下的 c 套接字中使用 read() 和 write() 时尺寸缩小

c - 为什么 -2147483648 可以放入 int 时自动提升为 long?

java - 日期验证不起作用

mysql - SQL 选择一月中的所有日期

c - strptime 给出 "implicit declaration"和 "undefined reference"

Java - 如何管理 JNI C 代码中的内存?