c语言时间函数的用法,c语言中的时间函数

c语言时间函数!!

time_t nowtime; -- 声明变量 nowtime(现在时间) 为 time_t 型

10年积累的网站设计制作、网站制作经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站策划后付款的网站建设流程,更有湘阴免费网站建设让你可以放心的选择与我们合作。

struct tm *timeinfo; -- 声明变量timeinfo(时间信息)为 tm 型 结构 指针。

time_t , tm 都是 time.h 头文件里定义 的 类型。

time( nowtime ); -- 调系统函数 time(), 获得 现在时间 (1970年起多少个“滴答”,世界标准时间)

timeinfo = localtime( nowtime ); -- 调系统函数, 获得 当地 现在时间 (例如 东8 区,北京时间)。时间数据是 tm 型 结构。

int hour; -- 声明变量 hour (小时),整型。

hour = timeinfo-tm_hour+1 ; -- 结构 timeinfo的成员tm_hour 是时间值,+1 得 hour(小时)。

tm_hour -- 数值范围 0-23。

c语言里面时间函数如何用?

#include time.h

#include stdio.h

#include dos.h

int main(void)

{

time_t timer;

struct tm *tblock;

timer = time(NULL);

tblock = localtime(timer);

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

return 0;

}

tm结构定义如下:

struct tm

{

int tm_sec;

int tm_min;

int tm_hour;

int tm_mday;

int tm_mon;

int tm_year;

int tm_wday;

int tm_yday;

int tm_isdst;

};

c语言中time函数怎么用?

头文件time.h

@函数名称: localtime

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

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

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

struct tm{

int tm_sec;

int tm_min;

int tm_hour;

int tm_mday;

int tm_mon;

int tm_year;

int tm_wday;

int tm_yday;

int tm_isdst;

};

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

#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()得到

所属文件: time.h

#include stdio.h

#include string.h

#include time.h

int main()

{

struct tm t;

char str[80];

t.tm_sec=1;

t.tm_min=3;

t.tm_hour=7;

t.tm_mday=22;

t.tm_mon=11;

t.tm_year=56;

t.tm_wday=4;

t.tm_yday=0;

t.tm_isdst=0;

strcpy(str,asctime(t));

printf("%s",str);

return 0;

}

@函数名称: ctime

函数原型: char *ctime(long 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 dos.h

#include conio.h

int main()

{

time_t first, second;

clrscr();

first=time(NULL);

delay(2000);

second=time(NULL);

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

getch();

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)

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

函数返回: 机器日历时间

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

所属文件: time.h

#include time.h

#include stdio.h

#include dos.h

int main()

{

time_t t;

t=time();

printf("The number of seconds since January 1,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;

}

c语言时间函数的具体使用方法,时间的加减

#include stdio.h

#include time.h 

int main()

time_t rawtime; 

struct tm * timeinfo; 

time ( rawtime ); 

timeinfo = localtime ( rawtime ); 

printf ( "The current date/time is: %s", asctime (timeinfo) ); 

return 0;

}

说明:

time_t // 时间类型(time.h 定义) 

struct tm { // 时间结构,time.h 定义如下: 

int tm_sec; 

int tm_min; 

int tm_hour; 

int tm_mday; 

int tm_mon; 

int tm_year; 

int tm_wday; 

int tm_yday; 

int tm_isdst; 

time ( rawtime ); // 获取时间,以秒计,从1970年1月一日起算,存于rawtime 

localtime ( rawtime ); //转为当地时间,tm 时间结构 

asctime() // 转为标准ASCII时间格式: 

//就是直接打印tm,tm_year 从1900年计算,所以要加1900,月tm_mon,从0计算,所以要加1

请问C语言中clock()函数该怎么用?

clock()是C/C++中的计时函数,而与其相关的数据类型是clock_t。

它的具体功能是返回处理器调用某个进程或函数所花费的时间。函数返回从“开启这个程序进程”到“程序中调用clock()函数”时之间的CPU时钟计时单元(clock tick)数,其中clock_t是用来保存时间的数据类型。

在time.h文件中,我们可以找到对它的定义:

#ifndef _CLOCK_T_DEFINED

typedef long clock_t;

#define _CLOCK_T_DEFINED

#endif

clock_t其实就是long,即长整形。该函数返回值是硬件滴答数,要换算成秒或者毫秒,需要除以CLK_TCK或者 CLK_TCK CLOCKS_PER_SEC。比如,在VC++6.0下,这两个量的值都是1000,这表示硬件滴答1000下是1秒,因此要计算一个进程的时间,用clock()除以1000即可。

clock的返回值一直是0的原因:

1、编译器优化,for循环实际根本没执行,直接跳过去了,所以时间为0。

2、clock计算的是程序占用cpu的时间,如果你的程序执行的动作很少,那么clock算出的时间也很少。

3、建议使用time gettimeofday函数来计时。

扩展资料:

C语言中clock()函数的程序例1:(TC下运行通过)

#include stdio.h

#include time.h

int main(void)

{

clock_t start, end;

start = clock();

delay(2000);

end = clock();

printf("The time was: %f\n", (double)(end - start) / CLK_TCK);

return 0;

}

说明:CLK_TCK 定义在TC中的time.h中:#define CLK_TCK18.2。

在VC6.0中也有关于CLK_TCK的宏定义,不过其值不再是18.2,而是1000。

实际上在VC6.0中CLK_TCK已完全等同CLOCKS_PER_SEC。

参考资料来源:百度百科-clock()

c语言 时间函数

CLOCK()函数:

clock()是C/C++中的计时函数,而与其相关的数据类型是clock_t。在MSDN中,查得对clock函数定义如下:

clock_t

clock(void)

;

这个函数返回从“开启这个程序进程”到“程序中调用clock()函数”时之间的CPU时钟计时单元(clock

tick)数,在MSDN中称之为挂钟时间(wal-clock);若挂钟时间不可取,则返回-1。其中clock_t是用来保存时间的数据类型,在time.h文件中,我们可以找到对它的定义:

#ifndef

_CLOCK_T_DEFINED

typedef

long

clock_t;

#define

_CLOCK_T_DEFINED

#endif

很明显,clock_t是一个长整形数。在time.h文件中,还定义了一个常量CLOCKS_PER_SEC,它用来表示一秒钟会有多少个时钟计时单元,其定义如下:

#define

CLOCKS_PER_SEC

((clock_t)1000)

可以看到每过千分之一秒(1毫秒),调用clock()函数返回的值就加1。下面举个例子,你可以使用公式clock()/CLOCKS_PER_SEC来计算一个进程自身的运行时间:

void

elapsed_time()

{

printf("Elapsed

time:%u

secs.\n",clock()/CLOCKS_PER_SEC);

}

当然,你也可以用clock函数来计算你的机器运行一个循环或者处理其它事件到底花了多少时间:

#include

stdio.h

#include

stdlib.h

#include

time.h

int

main(void)

{

long

i

=

10000000L;

clock_t

start,

finish;

double

duration;

/*

测量一个事件持续的时间*/

printf(

"Time

to

do

%ld

empty

loops

is

",

i)

;

start

=

clock();

while(

i--

);

finish

=

clock();

duration

=

(double)(finish

-

start)

/

CLOCKS_PER_SEC;

printf(

"%f

seconds\n",

duration

);

system("pause");

}

在笔者的机器上,运行结果如下:

Time

to

do

10000000

empty

loops

is

0.03000

seconds

上面我们看到时钟计时单元的长度为1毫秒,那么计时的精度也为1毫秒,那么我们可不可以通过改变CLOCKS_PER_SEC的定义,通过把它定义的大一些,从而使计时精度更高呢?通过尝试,你会发现这样是不行的。在标准C/C++中,最小的计时单位是一毫秒。

time_t

time(

time_t

*timer

);

返回值是1970年到现在的秒数

用long型接就可以了

参数也是同样意义

long

time_s

=

0;

time_s

=

time(

NULL

);

//

time_s就是1970年到现在的秒数

或者

long

*

time_s

=

NULL;

time(time_s);

//

*time_s就是1970年到现在的秒数

要计算前后一段时间的话之前取一次time,之后取一次相减就知道用了多少秒了


分享题目:c语言时间函数的用法,c语言中的时间函数
网址分享:http://ybzwz.com/article/dsgiici.html