时间和日期例程

来源:互联网 发布:java jdk版本查看 编辑:程序博客网 时间:2024/05/01 08:45
由unix内核提供的基本时间服务是计算自国际标准时间公元1970年1月1日00:00:00以来经过的秒数。

time函数返回当前时间和日期。

#include <time.h>

time_t time(time_t *calptr);

时间值总是作为函数值返回。如果参数不为空,则时间值也存放在由calptr指向的单元内。


gettimeofday提供了更高的分辨率(最高为微秒级)

int gettimeofday(struct timeval *restrict tp, void *restrict tzp);


localtime和gmtime可将日历时间转换成以年、月、日、时、分、秒、周日表示的时间。

struct tm *gmtime(const time_t *calptr);

struct tm *localtime(const time_t *calptr);

localtime将日历时间转换成本地时间,而gmtime则将日历时间转换成国际标准时间(0时区)。


mktime以本地时间的年、月、日等作为参数,将其转换成time_t值。

time_t mktime(strcut tm *tmptr);


asctime和ctime函数产生26字节的字符串,如Tue Feb 10 18:27:38 2004\n\0

char *asctime(const struct tm *tmptr);

char *ctime(const time_t *calptr);


strftime是非常复杂的类似于printf的时间值函数。

size_t strftime(char *restrict buf, size_t maxsize, const char *restrict format, const struct tm *restrict tmptr);

0 0
原创粉丝点击