日历时间

  C语言标准库的 time_t time(time_t *tp) 函数返回当前日历时间。怎么理解日历时间呢?

  关于“时间”的理解,可以参考 "The GNU C Library" 的文档:
  http://www.gnu.org/software/libc/manual/html_node/Date-and-Time.html#Date-and-Time

  关于“日历时间”的理解,可以参考:
  http://www.gnu.org/software/libc/manual/html_node/Time-Basics.html#Time-Basics
  http://www.gnu.org/software/libc/manual/html_node/Calendar-Time.html#Calendar-Time

  time_t 用“从一个标准时间点到此时的时间经过的秒数”来表示日历时间,即为Simple Calendar Time。这个标准时间点对不同的编译器来说会有所不同(通常是00:00:00 on January 1, 1970, Coordinated Universal Time),但对一个编译系统来说,这个标准时间点是不变的,该编译系统中的时间对应的Simple Calendar Time都通过该标准时间点来衡量,所以可以说Simple Calendar Time是“相对时间”,但是无论你在哪一个时区,在同一时刻对同一个标准时间点来说,Simple Calendar Time都是一样的。

  举个例子,就是如果你在 东八区 2012年1月1日8时0分0秒 这个时刻调用 time 函数,那么返回的是 东八区 1970年1月1日8时0分0秒 到 东八区 2012年1月1日8时0分0秒 经过的秒数。相当于在 零时区 2012年1月1日0时0分0秒 这个时刻调用 time 函数,返回的是 零时区 1970年1月1日0时0分0秒 到 零时区 2012年1月1日0时0分0秒 经过的秒数。

  再举个例子,运行程序:
#include <time.h>
#include <stdio.h>
main()
{
    time_t t = time(NULL);
    printf("%lld\n", t);
}
  记录打印结果,然后修改机器的时区设置(可以修改为任意时区,但只修改时区不修改时间)再运行程序并记录打印结果。可以看到在同一时刻在任何时区调用 time 函数,返回的结果都是相同的(在同一个编译系统的条件下)。

C标准库的<time.h>中的函数用法参考:http://c.chinaitlab.com/cc/basic/200906/786410.html

原文地址:https://www.cnblogs.com/codingthings/p/2753207.html