c语言获取当前系统日期时间,C语言获取系统当前时间

函数名: time ()

头文件:time.h

函数原型:time_t time(time_t * timer)

功 能: 获取当前的系统时间,返回的结果是一个time_t类型,其实就是一个大整数,其值表示从UTC(Coordinated Universal Time)时间1970年1月1日00:00:00(称为UNIX系统的Epoch时间)到当前时刻的秒数。然后可以调用localtime将time_t所表示的UTC时间转换为本地时间(我们是+8区,比UTC多8个小时)并转成struct tm类型,该类型的各数据成员分别表示年月日时分秒。

补充说明:time函数的原型也可以理解为 long time(long *tloc),因为在time.h这个头文件中time_t 实际上就是:

#ifndef _TIME_T_DEFINED

typedef long time_t; /* time value */

#define _TIME_T_DEFINED /* avoid multiple def's of time_t */

#endif

time_t的数字是按UTC算的,跟时区无关,同一个时刻全球所有计算机上的time(NULL)返回值都相同。

用localtime转换成可显示的格式时才需要考虑时区。

实例:

#include #include int main(int argc, char **argv)

{

time_t tmpcal_ptr;

struct tm *tmp_ptr = NULL;

time(&tmpcal_ptr);

//tmpcal_ptr = time(