C 时间函数总结

头文件 time.h

处理器时间函数

clock_t clock(void)

处理器的处理时间,如可以在 main开始的地方 使用这个函数,然后再 完毕后 调用这个函数 并 减去 之前的返回值,为了 把 这个值转换为秒,应该将它除以常量 CLOCKS_PER_SEC

当天时间

1.time 函数返回当前的日期 和 时间 :

time_t time(time_t * returned_value)

一般的 ,返回值 在 returned_value指针中存储,该时间是 1970年元月1日,零时 到 当前的 秒数。
2.操纵函数

char* ctime (time_t const *time_value)

参数 是 time_t 类型的指针,返回一个char* 字符串
形如:
Sun Jul 4 04:02:48 1976

difftime ,

计算两个time_t 参数的 时间差,返回double类型

struct tm *gmtime(time_t const * time)

struct tm *localtime(time_t const *time)

这两个函数可以将time_t 值 转换为struct tm 类型的值。前者是UTC时间 ,后者 是 当地时间。

struct tm结构体的成员 ,如下:
int tm_sec 0-61 //会出现闰秒
int tm_min 0-59
int tm_hour 0-23
int tm_mday 1-31
int tm_mon 0-11
int tm_year 0-?? //从1900年开始
int tm_wday 0-6
int tm_yday 0-365

得到struct tm后 ,有如下 可能的调用

char* asctime(struct tm *tm_ptr)

size_t strftime(char * string,size_t maxsize,char const * format,struct tm const *tm_ptr )

time_t mktime(struct tm *tm_prt)

asctime 返回的值 形式 与 ctime一致
strftime 可以将tm转换为想要的时间 显示格式,非常灵活。
mktime将 tm格式转换为time_t类型

关于strftime 参见[C++中strftime()的详细说明]http://www.cnblogs.com/Stultz-Lee/p/6699992.html

原文地址:https://www.cnblogs.com/Stultz-Lee/p/6706203.html