
函数原型 :
引用
#include <sys/timesh>
clock_t times (struct tms buf );
函数功能 :
获取进程时间。
说明 :
times() 函数返回从过去一个任意的时间点所经过的时钟数。返回值可能会超出 clock_t (一般为 long 型) 的范围(溢出)。如果发生错误,则返回 (clock_t ) -1 类型,然后设置相应的 errno 值。
系统每秒的时钟可以通过 sysconf(_SC_CLK_TCK); 函数获得。关于 sysconf() 函数的详细信息见:http://wwwgroadnet/bbs/readphptid-1485html
上面 _SC_CLK_TCK 的值为 2,因为它在 /usr/include/bits/confnameh 头文件的一个枚举类型里定义。
struct tms 结构体定义在 <sys/timesh> 头文件里,具体定义如下:
引用
/ Structure describing CPU time used by a process and its children /
struct tms
{
clock_t tms_utime ; / User CPU time 用户程序 CPU 时间/
clock_t tms_stime ; / System CPU time 系统调用所耗费的 CPU 时间 /
clock_t tms_cutime ; / User CPU time of dead children 已死掉子进程的 CPU 时间/
clock_t tms_cstime ; / System CPU time of dead children 已死掉子进程所耗费的系统调用 CPU 时间/
};
实例验证 times() 函数
测试代码-1 :
引用
#include <stdioh>
#include <unistdh>
#include <stdlibh>
#include <sys/timesh>
int main ()
{
struct tms time_buf_head , time_buf_end ;
long tck = 0 ;
clock_t time_head , time_end ;
tck = sysconf (_SC_CLK_TCK ); /获取系统时钟(1秒里有多少个)/
time_head = times ( & time_buf_head ); /进程运行到此时的系统时钟数(总的)/
printf ("head_time is : %f \n " , time_head / (double )tck ); /此时进程所处的时间点(单位为秒)/
//system ("/time_testexe");
system ("sleep 2" ); /睡眠2秒/
time_end = times ( & time_buf_end ); /进程到此时的系统时钟数/
printf ("end_time is : %f \n " , time_end / (double )tck ); /此时进程所处的时间点(单位为秒)/
printf ("user time is : %f \n " , ((time_buf_end tms_utime - time_buf_head tms_utime ) / double )tck )); /打印出用户进程到此所经历时间/
printf ("systime time is : %f \n " , ((time_buf_end tms_stime - time_buf_head tms_stime ) / double )tck ));
printf ("child user time is : %f \n " , ((time_buf_end tms_cutime - time_buf_head tms_cutime ) / (double )tck ));
printf ("child sys time is : %f \n " , ((time_buf_end tms_cstime - time_buf_head tms_cstime ) / (double )tck ));
return (0 );
} ( (
运行输出 :
引用
beyes@beyes-groad:~$ /timeexe
head_time is : 17236892770000
end_time is : 17236894790000
user time is : 0000000
systime time is : 0000000
child user time is : 0000000
child sys time is : 0000000
上面蓝色部分的时间间隔刚好是 2s 。从上面看到,下面的时间值都是 0。为了验证问题,现在修改一下源程序:
引用
#include <sys/typesh>
#include <sys/stath>
#include <stdioh>
#include <unistdh>
#include <stdlibh>
#include <fcntlh>
#include <sys/timesh>
int main ()
{
struct tms time_buf_head , time_buf_end ;
long tck = 0 ;
clock_t time_head , time_end ;
int i ;
int j ;
tck = sysconf (_SC_CLK_TCK );
time_head = times ( & time_buf_head );
printf ("head_time is : %f \n " , time_head / (double )tck );
/延迟测试用/
for (i = 0 ; i < 20000 ; i ++ )
for (j = 0 ; j < 20000 ; j ++ ) {
;
}
time_end = times ( & time_buf_end );
printf ("end_time is : %f \n " , time_end / (double )tck );
printf ("user time is : %f \n " , ((time_buf_end tms_utime - time_buf_head tms_utime ) / double )tck )); /用户进程所耗费的时间/
printf ("systime time is : %f \n " , ((time_buf_end tms_stime - time_buf_head tms_stime ) / (double )tck ));
printf ("child user time is : %f \n " , ((time_buf_end tms_cutime - time_buf_head tms_cutime ) / (double )tck ));
printf ("child sys time is : %f \n " , ((time_buf_end tms_cstime - time_buf_head tms_cstime ) / (double )tck ));
return (0 );
} (
再次运行输出:
引用
beyes@beyes-groad:~$ /timeexe
head_time is : 17184643070000
end_time is : 17184644280000
user time is : 1200000
systime time is : 0000000
child user time is : 0000000
child sys time is : 0000000
由于使用了大量的延迟,这时可以看到 user time 里耗费了 12s ,而 end_time 和 head_time 的时间间隔为 121s 约等于 12s 。
再来修改一下代码:
引用
#include <sys/typesh>
#include <sys/stath>
#include <stdioh>
#include <unistdh>
#include <stdlibh>
#include <fcntlh>
#include <sys/timesh>
int main ()
{
struct tms time_buf_head , time_buf_end ;
long tck = 0 ;
clock_t time_head , time_end ;
int i ;
int j ;
tck = sysconf (_SC_CLK_TCK );
time_head = times ( & time_buf_head );
printf ("head_time is : %f \n " , time_head / (double )tck );
for (i = 0 ; i < 1000 ; i ++ )
for (j = 0 ; j < 1000 ; j ++ ) {
open ("Cannon-1txt" , O_RDONLY );
}
time_end = times ( & time_buf_end );
printf ("end_time is : %f \n " , time_end / (double )tck );
printf ("user time is : %f \n " , ((time_buf_end tms_utime - time_buf_head tms_utime ) / double )tck ));
printf ("systime time is : %f \n " , ((time_buf_end tms_stime - time_buf_head tms_stime ) / (double )tck ));
printf ("child user time is : %f \n " , ((time_buf_end tms_cutime - time_buf_head tms_cutime ) / (double )tck ));
printf ("child sys time is : %f \n " , ((time_buf_end tms_cstime - time_buf_head tms_cstime ) / (double )tck ));
return (0 );
}
(
运行输出:
引用
beyes@beyes-groad:~$ /timeexe
head_time is : 17189923210000
end_time is : 17189923650000
user time is : 0160000
systime time is : 0280000
child user time is : 0000000
child sys time is : 0000000
在上面的输出中可以看到,systime time 这时不再为 0,这是因为程序中使用了 open() 这个系统调用的结果,它在两个 for 循环里一共被调用了 10001000次,总耗时为028s ,而执行这两个 for 的时间为 016s ,两个时间加起来的时间间隔正好为 end_time - head_time = 044s 。
下面测试子进程的时间,也就是 child user time 和 child sys time 两个。这里,需要另外一个程序,它的主要作用就是和上面一样的调用 open() 打开一个在本目录下的一文本文件,代码如下:
引用
#include <stdioh>
#include <sys/typesh>
#include <sys/stath>
#include <fcntlh>
int main ()
{
int i ;
int j ;
for (i = 0 ; i < 1000 ; i ++ )
for (j = 0 ; j < 1000 ; j ++ ) {
open ("Cannon-1txt" , O_RDONLY );
}
return (0 );
}
上面的程序命名为 time_testexe ,它会在主程序里被 system() 函数调用到,修改主程序如下:
引用
#include <sys/typesh>
#include <sys/stath>
#include <stdioh>
#include <unistdh>
#include <stdlibh>
#include <fcntlh>
#include <sys/timesh>
int main ()
{
struct tms time_buf_head , time_buf_end ;
long tck = 0 ;
clock_t time_head , time_end ;
int i ;
int j ;
tck = sysconf (_SC_CLK_TCK );
time_head = times ( & time_buf_head );
printf ("head_time is : %f \n " , time_head / (double )tck );
system ("/time_testexe" );
time_end = times ( & time_buf_end );
printf ("end_time is : %f \n " , time_end / (double )tck );
printf ("user time is : %f \n " , ((time_buf_end tms_utime - time_buf_head tms_utime ) / double )tck ));
printf ("systime time is : %f \n " , ((time_buf_end tms_stime - time_buf_head tms_stime ) / (double )tck ));
printf ("child user time is : %f \n " , ((time_buf_end tms_cutime - time_buf_head tms_cutime ) / (double )tck ));
printf ("child sys time is : %f \n " , ((time_buf_end tms_cstime - time_buf_head tms_cstime ) / (double )tck ));
return (0 );
} (
运行输出:
引用
beyes@beyes-groad:~$ /timeexe
head_time is : 17190766590000
end_time is : 17190767060000
user time is : 0000000
systime time is : 0000000
child user time is : 0140000
child sys time is : 0300000
由上可见,child user time 和 child sys time 两者的时间也是为 044s,这和上面是一样的,这是因为程序的内容相同。
http://blogcsdnnet/jinan1861/archive/2010/10/08/5927677aspx
long currentsec() /计算当前秒的函数/
{ tm p; / tm 是在timeh 中定义的时间结构体类型/
int h,m,s;
time_t timep;
time (&timep);
p=gmtime(&timep);
h=p->tm_hour;
m=p->tm_min;
s=p->tm_sec;
return h3600+m60+s;
}
代码如下:
#include <stdioh>#include <stdlibh>
int max(int a, int b)
{
return a > b a : b;
}
int main()
{
int i, m, n;
scanf("%d", &m);
for (i = 1; i < 8; i++) {
scanf("%d", &n);
m = max(m, n);
}
printf("最大数:%d\n", m);
system("pause");
return 0;
}
运行结果:
C语言的标准库函数包括一系列日期和时间处理函数,它们都在头文件中说明。
在头文件中定义了三种类型:time_t,struct tm和clock_t。
下面列出了这些函数。
time_t time(time_t timer);
double difftime(time_t time1,time_t time2);
struct tm gmtime(const time_t timer);
struct tm localtime(const time_t timer);
char asctime(const struct tm timeptr);
char ctime(const time_t timer);
size_t strftime(char s,size_t maxsize,const char format,const struct tm timeptr);
time_t mktime(struct tm timeptr);
clock_t clock(void);
具体应用举例
asctime(将时间和日期以字符串格式表示)
相关函数
time,ctime,gmtime,localtime
表头文件
#i nclude
定义函数
char asctime(const struct tm timeptr);
函数说明
asctime()将参数timeptr所指的tm结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果以字符串形态返回。
此函数已经由时区转换成当地时间,字符串格式为:"Wed Jun 30 21:49:08 1993\n"
返回值
若再调用相关的时间日期函数,此字符串可能会被破坏。此函数与ctime不同处在于传入的参数是不同的结构。
附加说明
返回一字符串表示目前当地的时间日期。
范例
#i nclude
main()
{
time_t timep;
time (&timep);
printf("%s",asctime(gmtime(&timep)));
}
执行
Sat Oct 28 02:10:06 2000
ctime(将时间和日期以字符串格式表示)
相关函数
time,asctime,gmtime,localtime
表头文件
#i nclude
定义函数
char ctime(const time_t timep);
函数说明
ctime ()将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果以字符串形态返回。
此函数已经由时区转换成当地时间,字符串格式为"Wed Jun 30 21 :49 :08 1993\n"。若再调用相关的时间日期函数,此字符串可能会被破坏。
返回值
返回一字符串表示目前当地的时间日期。
范例
#i nclude
main()
{
time_t timep;
time (&timep);
printf("%s",ctime(&timep));
}
执行
Sat Oct 28 10 : 12 : 05 2000
gettimeofday(取得目前的时间)
相关函数
time,ctime,ftime,settimeofday
表头文件
#i nclude
#i nclude
定义函数
int gettimeofday ( struct timeval tv , struct timezone tz )
函数说明
gettimeofday()会把目前的时间有tv所指的结构返回,当地时区的信息则放到tz所指的结构中。
timeval结构定义为:
struct timeval{
long tv_sec; /秒/
long tv_usec; /微秒/
};
timezone 结构定义为:
struct timezone{
int tz_minuteswest; /和Greenwich 时间差了多少分钟/
int tz_dsttime; /日光节约时间的状态/
};
上述两个结构都定义在/usr/include/sys/timeh。tz_dsttime 所代表的状态如下
DST_NONE /不使用/
DST_USA /美国/
DST_AUST /澳洲/
DST_WET /西欧/
DST_MET /中欧/
DST_EET /东欧/
DST_CAN /加拿大/
DST_GB /大不列颠/
DST_RUM /罗马尼亚/
DST_TUR /土耳其/
DST_AUSTALT /澳洲(1986年以后)/
返回值
成功则返回0,失败返回-1,错误代码存于errno。附加说明EFAULT指针tv和tz所指的内存空间超出存取权限。
范例
#i nclude
#i nclude
main(){
struct timeval tv;
struct timezone tz;
gettimeofday (&tv , &tz);
printf("tv_sec; %d\n", tv,tv_sec) ;
printf("tv_usec; %d\n",tvtv_usec);
printf("tz_minuteswest; %d\n", tztz_minuteswest);
printf("tz_dsttime, %d\n",tztz_dsttime);
}
执行
tv_sec: 974857339
tv_usec:136996
tz_minuteswest:-540
tz_dsttime:0
gmtime(取得目前时间和日期)
相关函数
time,asctime,ctime,localtime
表头文件
#i nclude
定义函数
struct tmgmtime(const time_ttimep);
函数说明
gmtime()将参数timep 所指的time_t 结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构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;
};
int tm_sec 代表目前秒数,正常范围为0-59,但允许至61秒
int tm_min 代表目前分数,范围0-59
int tm_hour 从午夜算起的时数,范围为0-23
int tm_mday 目前月份的日数,范围01-31
int tm_mon 代表目前月份,从一月算起,范围从0-11
int tm_year 从1900 年算起至今的年数
int tm_wday 一星期的日数,从星期一算起,范围为0-6
int tm_yday 从今年1月1日算起至今的天数,范围为0-365
int tm_isdst 日光节约时间的旗标
此函数返回的时间日期未经时区转换,而是UTC时间。
返回值
返回结构tm代表目前UTC 时间
范例
#i nclude
main(){
char wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
time_t timep;
struct tm p;
time(&timep);
p=gmtime(&timep);
printf("%d%d%d",(1900+p->tm_year), (1+p->tm_mon),p->tm_mday);
printf("%s%d;%d;%d\n", wday[p->tm_wday], p->tm_hour, p->tm_min, p->tm_sec);
}
执行
2000/10/28 Sat 8:15:38
localtime(取得当地目前时间和日期)
相关函数
time, asctime, ctime, gmtime
表头文件
#i nclude
定义函数
struct tm localtime(const time_t timep);
函数说明
localtime()将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构tm返回。
结构tm的定义请参考gmtime()。此函
数返回的时间日期已经转换成当地时区。
返回值
返回结构tm代表目前的当地时间。
范例
#i nclude
main(){
char wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
time_t timep;
struct tm p;
time(&timep);
p=localtime(&timep); /取得当地时间/
printf ("%d%d%d ", (1900+p->tm_year),( l+p->tm_mon), p->tm_mday);
printf("%s%d:%d:%d\n", wday[p->tm_wday],p->tm_hour, p->tm_min, p->tm_sec);
}
执行
2000/10/28 Sat 11:12:22
mktime(将时间结构数据转换成经过的秒数)
相关函数
time,asctime,gmtime,localtime
表头文件
#i nclude
定义函数
time_t mktime(strcut tm timeptr);
函数说明
mktime()用来将参数timeptr所指的tm结构数据转换成从公元1970年1月1日0时0分0 秒算起至今的UTC时间所经过的秒数。
返回值
返回经过的秒数。
范例
/ 用time()取得时间(秒数),利用localtime()
转换成struct tm 再利用mktine()将struct tm转换成原来的秒数/
#i nclude
main()
{
time_t timep;
strcut tm p;
time(&timep);
printf("time() : %d \n",timep);
p=localtime(&timep);
timep = mktime(p);
printf("time()->localtime()->mktime():%d\n",timep);
}
执行
time():974943297
time()->localtime()->mktime():974943297
settimeofday(设置目前时间)
相关函数
time,ctime,ftime,gettimeofday
表头文件
#i nclude
#i nclude
定义函数
int settimeofday ( const struct timeval tv,const struct timezone tz);
函数说明
settimeofday()会把目前时间设成由tv所指的结构信息,当地时区信息则设成tz所指的结构。详细的说明请参考gettimeofday()。
注意,只有root权限才能使用此函数修改时间。
返回值
成功则返回0,失败返回-1,错误代码存于errno。
错误代码
EPERM 并非由root权限调用settimeofday(),权限不够。
EINVAL 时区或某个数据是不正确的,无法正确设置时间。
time(取得目前的时间)
相关函数
ctime,ftime,gettimeofday
表头文件
#i nclude
定义函数
time_t time(time_t t);
函数说明
此函数会返回从公元1970年1月1日的UTC时间从0时0分0秒算起到现在所经过的秒数。如果t 并非空指针的话,
此函数也会将返回值存到t指针所指的内存。
返回值
成功则返回秒数,失败则返回((time_t)-1)值,错误原因存于errno中。
范例
#i nclude
mian()
{
int seconds= time((time_t)NULL);
printf("%d\n",seconds);
}
Date and Time Functions: <timeh>
The header <timeh> declares types and functions for manipulating date and time Some functions process local time,
which may differ from calendar time, for example because of time zone clock_t and time_t are arithmetic types
representing times, and struct tm holds the components
of a calendar time:
int tm_sec; seconds after the minute (0,61)
int tm_min; minutes after the hour (0,59)
int tm_hour; hours since midnight (0,23)
int tm_mday; day of the month (1,31)
int tm_mon; months since January (0,11)
int tm_year; years since 1900
int tm_wday; days since Sunday (0,6)
int tm_yday; days since January 1 (0,365)
int tm_isdst; Daylight Saving Time flag
tm_isdst is positive if Daylight Saving Time is in effect, zero if not, and negative if the information is not available
clock_t clock(void)
clock returns the processor time used by the program since the beginning of execution, or -1 if unavailable
clock()/CLK_PER_SEC is a time in
seconds
time_t time(time_t tp)
time returns the current calendar time or -1 if the time is not available If tp is not NULL,
the return value is also assigned to tp
double difftime(time_t time2, time_t time1)
difftime returns time2-time1 expressed in seconds
time_t mktime(struct tm tp)
mktime converts the local time in the structure tp into calendar time in the same representation used by time
The components will have values
in the ranges shown mktime returns the calendar time or -1 if it cannot be represented
The next four functions return pointers to static objects that may be overwritten by other calls
char asctime(const struct tm tp)
asctimetp into a string of the form
Sun Jan 3 15:14:13 1988\n\0
char ctime(const time_t tp)
ctime converts the calendar time tp to local time; it is equivalent to
asctime(localtime(tp))
struct tm gmtime(const time_t tp)
gmtime converts the calendar time tp into Coordinated Universal Time (UTC) It returns NULL if UTC is not available
The name gmtime has historical significance
struct tm localtime(const time_t tp)
localtime converts the calendar time tp into local time
size_t strftime(char s, size_t smax, const char fmt, const struct tm tp)
strftime formats date and time information from tp into s according to fmt, which is analogous to a printf format
Ordinary characters (including the terminating '\0') are copied into s Each %c is replaced as described below,
using values appropriate for the local environment
No more than smax characters are placed into s strftime returns the number of characters, excluding the '\0',
or zero if more than smax characters were produced
%a abbreviated weekday name
%A full weekday name
%b abbreviated month name
%B full month name
%c local date and time representation
%d day of the month (01-31)
%H hour (24-hour clock) (00-23)
%I hour (12-hour clock) (01-12)
%j day of the year (001-366)
%m month (01-12)
%M minute (00-59)
%p local equivalent of AM or PM
%S second (00-61)
%U week number of the year (Sunday as 1st day of week) (00-53)
%w weekday (0-6, Sunday is 0)
%W week number of the year (Monday as 1st day of week) (00-53)
%x local date representation
%X local time representation
%y year without century (00-99)
%Y year with century
%Z time zone name, if any
%% %
time_t t; /定义一个time_t型(在timeh中有typedef long time_t;语句,由此可知,time_t类型也就是long类型)的变量/
time(&t); /将当前的日历时间(即从1970-1-1到执行此语句时所经历的秒数)保存到t中/
printf("%s/n", ctime(&t)); /ctime(&t)将把t所指向的日历时间转换为系统所提供的一个字符串,这个函数将返回这个字符串的基址,然后由printf语句将这个字符串输出,从而得到现在的时刻/
来源http://zhidaobaiducom/question/32925135htmlsi=1
以前实际上用过,很想对C语言中的时间函数了解多一点,趁着这个寒假,查了些资料,大概把我现在能用到的关于时间的 *** 作在此记录下来。通过几个函数来熟悉C语言中对时间的 *** 作。(注:以下程序均在VS2010上编译通过。)①time()函数。可以通过time()函数来获得日历时间。其原型为:time_ttime(time_ttimer);一般参数为空,返回值类型time_t是一个长整型数,函数将返回现在的日历时间,即从一个时间点(所有不同版本的VisualC++都是从1970年1月1日0时0分0秒)到现在的经过的秒数。例子程序:#include<stdioh>#include<timeh>voidmain(){time_tlt;lt=time(NULL);printf("1970年1月1日0时0分0秒到现在经历了%ld秒\n",lt);}运行结果(结果与程序运行的时间有关,贴出我此时运行出的结果):1970年1月1日0时0分0秒到现在经历了1326975564秒请按任意键继续②clock()函数。C语言中的计时函数。函数原型为:clock_tclock(void);clock()函数返回从“开启这个程序进程”到“程序中调用clock()函数”时之间的CPU时钟计时单元数。返回值类型clock_t也是一个长整型数。在timeh头文件中定义了一个符号常量CLOCKS_PER_SEC,表示一秒钟会有多少个计时单元。所以通过简单的数学知识,可以知道在程序中用clock()/CLOCKS_PER_SEC来表示程序从开始到调用clock()函数时用了多少秒。例子程序:#include<stdioh>#include<timeh>voidmain(){clock_tlt;for(inti=0;i<1000000000;i++);lt=clock();printf("循环执行1000000000个空 *** 作需要%f秒\n",(double)lt/CLOCKS_PER_SEC);}运行结果(在不同的机器上运行的结果可能不一样,下面是在我自己的笔记本上运行的结果):循环执行1000000000个空 *** 作需要3484000秒请按任意键继续③使用C库函数来显示日期和时间。首先要介绍一下C语言中的一个日期的结构体类型,tm类型。其在timeh中的定义如下:#ifndef_TM_DEFINEDstructtm{inttm_sec;inttm_min;inttm_hour;inttm_mday;inttm_mon;inttm_year;inttm_wday;inttm_yday;inttm_isdst;};#define_TM_DEFINED#endif然后可以介绍有关的函数了。timeh提供了两种不同的函数将日历时间(一个长整型数)转换成我们平时看到的把年月日时分秒分开的时间格式:structtmgmtime(consttime_ttimer);structtmlocaltime(consttime_ttimer);其中gmtime()函数是将日历时间转换为世界标准时间(即格林尼治时间),并返回一个tm结构体来保存这个时间,而localtime()函数是将日历时间转换为本地时间(在中国地区获得的本地时间会比世界标准时间晚8个小时)。例子程序:#include<stdioh>#include<timeh>voidmain(){structtmlocal;time_tt;t=time(NULL);local=localtime(&t);printf("现在北京时间是%d点\n",local->tm_hour);local=gmtime(&t);printf("世界标准时间是%d点\n",local->tm_hour);}运行结果(运行结果与运行的时间有关,我是在早上9点多钟运行这个程序的):现在北京时间是9点世界标准时间是1点请按任意键继续这样子我们就可以完全才输出此刻的年月日时分秒了,当然需要逐个来输出。其实C库函数还提供了一个很有用的以固定的时间格式来输出年月日时分秒的函数。这两个函数原型如下:charasctime(conststructtmtimeptr);charctime(consttime_ttimer);asctime()函数是通过tm结构来生成具有固定格式的保存时间信息的字符串,而ctime()是通过日历时间来生成时间字符串。这样下面的例子程序就容易理解了:#include<stdioh>#include<timeh>voidmain(){structtmlocal;time_tt;t=time(NULL);local=localtime(&t);printf(asctime(local));local=gmtime(&t);printf(asctime(local));printf(ctime(&t));}运行结果(我是在早上9点多运行这个程序的):FriJan2009:55:562012FriJan2001:55:562012FriJan2009:55:562012请按任意键继续C语言还可以以我们规定的各种形式来规定输出时间的格式。要用到时可以查阅相关的资料,限于篇幅,就介绍到这里。④这里介绍计算持续的时间长度的函数。之前已经介绍了使用clock()函数的例子,它可以精确到毫秒级。其实我们也可以使用difftime()函数,但它只精确到秒。该函数的定义如下:doubledifftime(time_ttime1,time_ttime0);例子程序:#include<stdioh>#include<timeh>#include<stdlibh>voidmain(){time_tstart,end;start=time(NULL);for(inti=0;i<1000000000;i++);end=time(NULL);printf("这个循-环用了%f秒\n",difftime(end,start));}运行结果:这个循环用了3000000秒请按任意键继续⑤最后介绍mktime()函数。原型如下:time_tmktime(structtmtimer);可以使用函数将用tm结构表示的时间转换为日历时间。其返回值就是转换后的日历时间。这样我们就可以先制定一个分解时间,然后对这个时间进行 *** 作。下面的例子用来计算2012年1月20日是星期几:#include<timeh>#include<stdioh>#include<stdlibh>intmain(void){structtmt;time_tt_of_day;ttm_year=2012-1900;ttm_mon=0;ttm_mday=20;ttm_hour=0;ttm_min=12;ttm_sec=1;ttm_isdst=1;t_of_day=mktime(&t);printf(ctime(&t_of_day));return0;}运行结果:FriJan2000:12:012012请按任意键继续
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)