当前位置

首页 > 商务英语 > 计算机英语 > c语言中time函数的用法

c语言中time函数的用法

推荐人: 来源: 阅读: 2.75W 次

c语言中time函数的用法的用法你知道吗?下面小编就跟你们详细介绍下c语言中time函数的用法的用法,希望对你们有用。

c语言中time函数的用法

  c语言中time函数的用法的用法如下:

头文件time.h

@函数名称: localtime

函数原型: struct tm *localtime(const time_t *timer)

函数功能: 返回一个以tm结构表达的机器时间信息

函数返回: 以tm结构表达的时间,结构tm定义如下:

[cpp] view plain copy

ct tm{

02. int tm_sec;

03. int tm_min;

04. int tm_hour;

05. int tm_mday;

06. int tm_mon;

07. int tm_year;

08. int tm_wday;

09. int tm_yday;

10. int tm_isdst;

11. };

参数说明: timer-使用time()函数获得的机器时间

[cpp] view plain copy

01.#include <time.h>

02.#include <stdio.h>

03.#include <dos.h>

main() {

05. time_t timer;

06. struct tm *tblock;

07. timer=time(NULL);

08. tblock=localtime(&timer);

09. printf("Local time is: %s",asctime(tblock));

10. return 0;

11.}

@函数名称: asctime

函数原型: char* asctime(struct tm * ptr)

函数功能: 得到机器时间(日期时间转换为ASCII码)

函数返回: 返回的时间字符串格式为:星期,月,日,小时:分:秒,年

参数说明: 结构指针ptr应通过函数localtime()和gmtime()得到

所属文件: <time.h>

[cpp] view plain copy

01.#include <stdio.h>

02.#include <string.h>

03.#include <time.h>

04. int main() {

05. struct tm t;

06. char str[80];

07. _sec=1;

08. _min=3;

09. _hour=7;

10. _mday=22;

11. _mon=11;

12. _year=56;

13. _wday=4;

14. _yday=0;

15. _isdst=0;

16. strcpy(str,asctime(&t));

17. printf("%s",str);

18. return 0;

19.}

@函数名称: ctime

函数原型: char *ctime(long time)

函数功能: 得到日历时间

函数返回: 返回字符串格式:星期,月,日,小时:分:秒,年

参数说明: time-该参数应由函数time获得

所属文件: <time.h>

[cpp] view plain copy

01.#include <stdio.h>

02.#include <time.h>

main() {

04. time_t t;

05. time(&t);

06. printf("Today's date and time: %s",ctime(&t));

07. return 0;

08.}

@函数名称: difftime

函数原型: double difftime(time_t time2, time_t time1)

函数功能: 得到两次机器时间差,单位为秒

函数返回: 时间差,单位为秒

参数说明: time1-机器时间一,time2-机器时间二.该参数应使用time函数获得

所属文件: <time.h>

[cpp] view plain copy

01.#include <time.h>

02.#include <stdio.h>

03.#include <dos.h>

04.#include <conio.h>

main() {

06. time_t first, second;

07. clrscr();

08. first=time(NULL);

09. delay(2000);

10. second=time(NULL);

11. printf("The difference is: %f seconds",difftime(second,first));

12. getch();

13. return 0;

14.}

@函数名称: gmtime

函数原型: struct tm *gmtime(time_t *time)

函数功能: 得到以结构tm表示的时间信息

函数返回: 以结构tm表示的时间信息指针

参数说明: time-用函数time()得到的时间信息

所属文件: <time.h>

[cpp] view plain copy

01.#include <stdio.h>

02.#include <stdlib.h>

03.#include <time.h>

04.#include <dos.h>

*tzstr="TZ=PST8PDT";

main() {

07. time_t t;

08. struct tm *gmt, *area;

09. putenv(tzstr);

10. tzset();

11. t=time(NULL);

12. area=localtime(&t);

13. printf("Local time is:%s", asctime(area));

14. gmt=gmtime(&t);

15. printf("GMT is:%s", asctime(gmt));

16. return 0;

17.}

@函数名称: time

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

函数功能: 得到机器的日历时间或者设置日历时间

函数返回: 机器日历时间

参数说明: timer=NULL时得到机器日历时间,timer=时间数值时,用于设置日历时间,time_t是一个long类型

所属文件: <time.h>

[cpp] view plain copy

01.#include <time.h>

02.#include <stdio.h>

03.#include <dos.h>

main() {

05. time_t t;

06. t=time();

07. printf("The number of seconds since January 1,1970 is %ld",t);

08. return 0;

09.}

@函数名称: tzset

函数原型: void tzset(void)

函数功能: UNIX兼容函数,用于得到时区,在DOS环境下无用途

函数返回:

参数说明:

所属文件: <time.h>

[cpp] view plain copy

01.#include <time.h>

02.#include <stdlib.h>

03.#include <stdio.h>

main() {

05. time_t td;

06. putenv("TZ=PST8PDT");

07. tzset();

08. time(&td);

09. printf("Current time=%s",asctime(localtime(&td)));

10. return 0;

11.}