资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

c语言时间函数怎么用,C语言时间

C语言的时间函数怎么调用啊,要把他单独写成函数

// 定义一个延时xms毫秒的延时函数void delay(unsigned int xms) // xms代表需要延时的毫秒数{ unsigned int x,y; for(x=xms;x0;x--) for(y=110;y0;y--);}

创新互联建站是一家专注于成都网站制作、网站设计、外贸网站建设与策划设计,资溪网站建设哪家好?创新互联建站做网站,专注于网站建设十多年,网设计领域的专业建站公司;建站业务涵盖:资溪等地区。资溪做网站价格咨询:18980820575

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语言时间函数time_t

1、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

2、time函数使用示例

#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;

}

c语言调用时间函数

time_t t; /*定义一个time_t型(在time.h中有typedef long time_t;语句,由此可知,time_t类型也就是long类型)的变量*/

time(t); /*将当前的日历时间(即从1970-1-1到执行此语句时所经历的秒数)保存到t中*/

printf("%s/n", ctime(t)); /*ctime(t)将把t所指向的日历时间转换为系统所提供的一个字符串,这个函数将返回这个字符串的基址,然后由printf语句将这个字符串输出,从而得到现在的时刻*/

来源

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://cdkjz.cn/article/hegpdp.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220