关于c语言中时间函数的一些归纳

头文件#include <time.h>

概述:
ctime(),gmtime(),localtime()函数都带有一个代表日历时间的time_t类型的参数。当要描述为一个绝对的值时,它代表从新纪元(1970年1月1日凌晨)开始到现在所流逝的秒数。
asctime()和mktime()函数带有一个参数代表修正时间(分解时间),这是一个代表值分解成年、月、日等等。
休正时间(broken-down time)被存储在结构体tm中,这个结构体定义在<time.h>中。
结构体如下:
struct tm {
               int tm_sec;         /* seconds */
               int tm_min;         /* minutes */
               int tm_hour;        /* hours */
               int tm_mday;        /* day of the month */
               int tm_mon;         /* month */
               int tm_year;        /* year */
               int tm_wday;        /* day of the week */
               int tm_yday;        /* day in the year */
               int tm_isdst;       /* daylight saving time */
           };

char *ctime(const time_t *timep);
ctime() 等于asctime(localtime(t)).它将日历时间t转换为零终止字符串,字符串类型如下:“Wed Jun 30 21:49:08 1993\n”

struct tm *gmtime(const time_t *timep);
gmtime()函数转换日历时间为分解时间。格林尼治时间

struct tm *localtime(const time_t *timep);
localtime()函数转换日历时间为分解时间。当地时间

char *asctime(const struct tm *tm);
asctime()函数转换分解时间为零终止字符串(同ctime())

time_t mktime(struct tm *tm);
mktime()函数转换分解时间结构体(表示为当地时间)为日历时间。
举例:
#include <stdio.h>
#include <time.h>

int main(int argc, const char *argv[])
{
     time_t biggest = 0x7fffffff;
     time_t result;

     result = time(NULL);
     printf("%s",asctime(localtime(&result)));//当地现在时间
     printf("biggest = %s",ctime(&biggest));//给出最大的当地最大时间

     return 0;
}

输出:
Thu Nov 21 12:15:49 2013
biggest = Tue Jan 19 11:14:07 2038
posted @ 2013-11-21 12:33  vonyao  阅读(437)  评论(0编辑  收藏  举报