新闻资讯
看你所看,想你所想

time.h

time.h

time.h

time.h是C/C++中的日期和时间头档案。用于需要时间方面的函式。

基本介绍

  • 中文名:c语言时间头档案
  • 外文名:time.h
  • 类别:头档案
  • 作用:从系统时钟获取时间方式
  • 函式名称:localtime

代码示例

#include<stdio.h>#include<time.h>int main(){    time_t timer = time(NULL);    printf("ctime is %s\n", ctime(&timer));//得到日曆时间    return 0;}

获取时间方式

time_t time(time_t* timer)
得到从标準计时点(一般是1970年1月1日午夜)到当前时间的秒数。
clock_t clock(void)
得到从程式启动到此次函式调用时累计的毫秒数。

time函式

函式名称: localtime
函式原型: struct tm *localtime(const time_t *timer)
函式功能: 返回一个以tm结构表达的机器时间信息
函式返回: 以tm结构表达的时间,结构tm定义如下:
#ifndef _TM_DEFINED
struct tm {
int tm_sec; /* 秒 – 取值区间为[0,59] */
int tm_min; /* 分 - 取值区间为[0,59] */
int tm_hour; /* 时 - 取值区间为[0,23] */
int tm_mday; /* 一个月中的日期 - 取值区间为[1,31] */
int tm_mon; /* 月份(从一月开始,0代表一月) - 取值区间为[0,11] */
int tm_year; /* 年份,其值等于实际年份减去1900 */
int tm_wday; /* 星期 – 取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推 */
int tm_yday; /* 从每年的1月1日开始的天数 – 取值区间为[0,365],其中0代表1月1日,1代表1月2日,以此类推 */
int tm_isdst; /* 夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的进候,tm_isdst为0;不了解情况时,tm_isdst()为负。*/
};
#define _TM_DEFINED
#endif
参数说明: timer-使用time()函式获得的机器时间
所属档案: <time.h>
#include<time.h>#include<stdio.h>#include<dos.h>int main(){    time_t timer;    struct tm *tblock;    timer = time(NULL);    tblock = localtime(&timer);    printf("Local time is: %s", asctime(tblock));    return 0;}
函式名称: asctime
函式原型: char* asctime(struct tm * ptr)
函式功能: 得到机器时间(日期时间转换为ASCII码)
函式返回: 返回的时间字元串格式为:星期,月,日,小时:分:秒,年
参数说明: 结构指针ptr应通过函式localtime()和gmtime()得到
函式名称: ctime
函式原型: char *ctime(const time_t *time)
函式功能: 得到日曆时间
函式返回: 返回字元串格式:星期,月,日,小时:分:秒,年
参数说明: time-该参数应由函式time获得
所属档案: <time.h>
#include<stdio.h>#include<time.h>int main(){    time_t t;    time(&t);    printf("Today's date and time: %s", ctime(&t));    return 0;}
函式名称: difftime
函式原型: double difftime(time_t time2, time_t time1)
函式功能: 得到两次机器时间差,单位为秒
函式返回: 时间差,单位为秒
参数说明: time1-机器时间一,time2-机器时间二.该参数应使用time函式获得
所属档案: <time.h>
#include<time.h>#include<stdio.h>#include<windows.h>int main(){    time_t start, end;    system("cls");//清屏    time(&start);    Sleep(5000);//等待5秒,Sleep()函式包含在windows.h的头档案里,以毫秒为单位    time(&end);    printf("The difference is: %f seconds", difftime(end, start));    return 0;}
函式名称: gmtime
函式原型: struct tm *gmtime(time_t *time)
函式功能: 得到以结构tm表示的时间信息
函式返回: 以结构tm表示的时间信息指针
参数说明: time-用函式time()得到的时间信息
所属档案: <time.h>
#include<stdio.h>#include<stdlib.h>#include<time.h>#include<dos.h>char*tzstr="TZ=PST8PDT";int main(){    time_t t;    struct tm *gmt,*area;    putenv(tzstr);    tzset();    t = time(NULL);    area = localtime(&t);    printf("Local time is: %s", asctime(area));    gmt = gmtime(&t);    printf("GMT is: %s", asctime(gmt));    return 0;}
函式名称: time
函式原型: time_t time(time_t *timer)
函式功能: 得到系统当前的日曆时间
函式返回: 系统当前日曆时间,如果不能获得当前日曆时间,则返回-1
参数说明: timer=NULL时得到机器日曆时间,timer为有效指针时,更新timer为系统当前时间,time_t是一个long类型
所属档案: <time.h>
#include<time.h>#include<stdio.h>#include<dos.h>int main(){    time_t t;    t = time(NULL);//默认1970-1-1    printf("The number of seconds since January1, 1970 is %ld", t);    return 0;}
函式名称: tzset
函式原型: void tzset(void)
函式功能: UNIX兼容函式,用于得到时区,在DOS环境下无用途
函式返回:
参数说明:
所属档案: <time.h>
#include<time.h>#include<stdlib.h>#include<stdio.h>int main(){    time_t td;    putenv("TZ=PST8PDT");    tzset();    time(&td);    printf("Current time = %s", asctime(localtime(&td)));    return 0;}

相关推荐

声明:此文信息来源于网络,登载此文只为提供信息参考,并不用于任何商业目的。如有侵权,请及时联系我们:yongganaa@126.com