time_t最大时间是哪年哪月哪日?

baiyunyiyi33 2015-10-02 09:26:43
先摘录百度知道一个答案: time_t实际上就是长整型long int; 如假包换!! 他用来保存从1970年1月1日0时0分0秒到现在时刻的秒数! 用time()这个函数获取! 对time_t数据类型的值来说,它所表示的时间不能晚于2038年1月18日19时14分07秒。 Visual C++里有个__time64_t数据类型来保存日历时间,并通过_time64()函数来获得日历时间,这样就可以通过该数据类型保存3001年1月1日0时0分0秒之前的时间。 附: time_t包含在time.h里 #ifndef __TIME_T #define __TIME_T typedef long time_t; /* 时间值time_t 为长整型long int(这里缺省类型就是int哈,可以省略不写)的别名*/ #endif ———————————————————————————— 这里的最大时间是2038 01 18 19 14 07; 但是我写了一个小程序输出这个时间对应的time_t,发现是0x7FFF8F7F. 怎么回事?不应该是0xFFFFFFFF吗?
...全文
622 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2015-10-08
  • 打赏
  • 举报
回复
File: "C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\time.h":
      86: #ifndef _TIME_T_DEFINED
      87: #ifdef _USE_32BIT_TIME_T
      88: typedef __time32_t time_t;      /* time value */
      89: #else  /* _USE_32BIT_TIME_T */
      90: typedef __time64_t time_t;      /* time value */
      91: #endif  /* _USE_32BIT_TIME_T */
      92: #define _TIME_T_DEFINED         /* avoid multiple def's of time_t */
      93: #endif  /* _TIME_T_DEFINED */
mymtom 2015-10-05
  • 打赏
  • 举报
回复
引用 楼主 baiyunyiyi33 的回复:
先摘录百度知道一个答案: time_t实际上就是长整型long int; 如假包换!! 他用来保存从1970年1月1日0时0分0秒到现在时刻的秒数! 用time()这个函数获取! 对time_t数据类型的值来说,它所表示的时间不能晚于2038年1月18日19时14分07秒。 Visual C++里有个__time64_t数据类型来保存日历时间,并通过_time64()函数来获得日历时间,这样就可以通过该数据类型保存3001年1月1日0时0分0秒之前的时间。 附: time_t包含在time.h里 #ifndef __TIME_T #define __TIME_T typedef long time_t; /* 时间值time_t 为长整型long int(这里缺省类型就是int哈,可以省略不写)的别名*/ #endif ———————————————————————————— 这里的最大时间是2038 01 18 19 14 07; 但是我写了一个小程序输出这个时间对应的time_t,发现是0x7FFF8F7F. 怎么回事?不应该是0xFFFFFFFF吗?
网上的东西不一定对的,32位的time_t表示的最大时间是UTC 2038-01-19 03:14:07

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


int
main(int argc, char *argv[])
{
    time_t tks;
    struct tm tms;
    char buf[] = "1970-01-01 00:00:00";

    tks = 0;

    tms = *gmtime(&tks);
    strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &tms);
    printf("UTC %s\n", buf);

    tms = *localtime(&tks);
    strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &tms);
    printf("CST %s\n", buf);

    tks = 0x7FFFFFFF;

    tms = *gmtime(&tks);
    strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &tms);
    printf("UTC %s\n", buf);

    tms = *localtime(&tks);
    strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &tms);
    printf("CST %s\n", buf);

    tks = 0x7FFF8F7F;

    tms = *gmtime(&tks);
    strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &tms);
    printf("UTC %s\n", buf);

    tms = *localtime(&tks);
    strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &tms);
    printf("CST %s\n", buf);

    tks = time(NULL);

    tms = *gmtime(&tks);
    strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &tms);
    printf("UTC %s\n", buf);

    tms = *localtime(&tks);
    strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &tms);
    printf("CST %s\n", buf);

    return 0;
}
baiyunyiyi33 2015-10-02
  • 打赏
  • 举报
回复
引用 2 楼 lm_whales的回复:
求出这个时间 2038 01 18 19 14 07 - 1970 01 01 00 00 00 = 多少秒看看吧
哦,我看看试验一下是多少。
lm_whales 2015-10-02
  • 打赏
  • 举报
回复
求出这个时间 2038 01 18 19 14 07 - 1970 01 01 00 00 00 = 多少秒看看吧
羽飞 2015-10-02
  • 打赏
  • 举报
回复
他是一个秒数,并不是203801xxxx啊 最大数字也最多是个0x7FFFxxxx,0xFFFFFxxx是个负数,long int是有符号的数字

70,024

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧