// 定义一个延时xms毫秒的延时函数void delay(unsigned int xms) // xms代表需要延时的毫秒数{ unsigned int x,y; for(x=xms;x0;x--) for(y=110;y0;y--);}
头文件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 string.h
struct tm t;
t.tm_sec=1;
t.tm_mon=11;
t.tm_yday=0;
t.tm_isdst=0;
strcpy(str,asctime(t));
printf("%s",str);
@函数名称: ctime
函数原型: char *ctime(long time)
函数功能: 得到日历时间
函数返回: 返回字符串格式:星期,月,日,小时:分:秒,年
参数说明: time-该参数应由函数time获得
time_t t;
time(t);
printf("Today's date and time: %s",ctime(t));
@函数名称: difftime
函数功能: 得到两次机器时间差,单位为秒
函数返回: 时间差,单位为秒
#include conio.h
time_t first, second;
clrscr();
first=time(NULL);
second=time(NULL);
printf("The difference is: %f seconds",difftime(second,first));
getch();
@函数名称: gmtime
函数原型: struct tm *gmtime(time_t *time)
函数功能: 得到以结构tm表示的时间信息
函数返回: 以结构tm表示的时间信息指针
参数说明: time-用函数time()得到的时间信息
#include stdlib.h
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));
@函数名称: time
函数原型: time_t time(time_t *timer)
函数功能: 得到机器的日历时间或者设置日历时间
函数返回: 机器日历时间
参数说明: timer=NULL时得到机器日历时间,timer=时间数值时,用于设置日历时间,time_t是一个long类型
t=time();
@函数名称: tzset
函数原型: void tzset(void)
函数功能: UNIX兼容函数,用于得到时区,在DOS环境下无用途
函数返回:
参数说明:
time_t td;
time(td);
printf("Current time=%s",asctime(localtime(td)));
①.、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;?
}?
localtime ( rawtime ); //转为当地时间,tm 时间结构?
asctime() // 转为标准ASCII时间格式:?
#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 t; /*定义一个time_t型(在time.h中有typedef long time_t;语句,由此可知,time_t类型也就是long类型)的变量*/
printf("%s/n", ctime(t)); /*ctime(t)将把t所指向的日历时间转换为系统所提供的一个字符串,这个函数将返回这个字符串的基址,然后由printf语句将这个字符串输出,从而得到现在的时刻*/
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
#endif
很明显,clock_t是一个长整形数.在time.h文件中,还定义了一个常量CLOCKS_PER_SEC,它用来表示一秒钟会有多少个时钟计时单元,其定义如下:
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
stdlib.h
time.h
int
main(void)
i
=
①.0000000L;
start,
finish;
double
duration;
/*
测量一个事件持续的时间*/
printf(
"Time
to
do
%ld
empty
loops
is
",
i)
start
clock();
while(
i--
);
finish
duration
(double)(finish
-
start)
/
CLOCKS_PER_SEC;
"%f
seconds\n",
system("pause");
在笔者的机器上,运行结果如下:
Time
①.0000000
seconds
上面我们看到时钟计时单元的长度为1毫秒,那么计时的精度也为1毫秒,那么我们可不可以通过改变CLOCKS_PER_SEC的定义,通过把它定义的大一些,从而使计时精度更高呢?通过尝试,你会发现这样是不行的.在标准C/C◆◆中,最小的计时单位是一毫秒.
time_t
time(
*timer
用long型接就可以了
参数也是同样意义
如
time_s
0;
NULL
//
或者
*
NULL;
time(time_s);
要计算前后一段时间的话之前取一次time,之后取一次相减就知道用了多少秒了
以上就是土嘎嘎小编为大家整理的c语言时间函数怎么用相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!