标准C定义常见几种时间结构,分别用于不同的场合
—— clock_t; /* CPU滴答数来表示计时数 */
—— time_t; /* 描述表示时间 */
—— struct tm; /* 按人的习惯表达计时 */
—— struct timeval; /* 时间戳,对时间要求较高的场合 */
clock_t, time_t 实际上是long int型,主要用于软件时间的计算
tm 主要用于显示时间
timeval 用于对时间要求较高的场合
1
#include <time.h>
typedef long int clock_t;
clock_t clock( void );
函数返回从程序开始到调用clock( )时CPU的滴答数
CLOCKS_PER_SEC 宏表示一个sec CPU的滴答数
程序:
#include <stdio.h>
#include <time.h>
int main( void )
{
int Count = 100000000L;
clock_t clockTime;
double Diff = 0;
while( Count-- );
clockTime = clock( );
Diff = ( double )( clockTime ) / CLOCKS_PER_SEC;
printf( "clock_t = %ld, Time diff = %lf\r\n",
clockTime, Diff );
return 0;
}
运行结果:
clock_t = 640000, Time diff = 0.640000
如下面分析一段代码:
#include <stdio.h>
#include <time.h>
{
clock_t Start, End;
while( 1 )
{
Start = clock( );
printf( "wait...\n" );
sleep( 1 );
End = clock();
printf( "%ld - %ld = %lf \r\n", Start, End, ( double )( End - Start ) / CLOCKS_PER_SEC );
}
return 0;
}
运行结果:
wait...0 - 0 = 0.000000
wait...
0 - 0 = 0.000000
wait...
0 - 0 = 0.000000
wait...
0 - 0 = 0.000000
wait...
由此可以得到一个结果:
clock( )获取的是CPU花在当前程序上的滴答数,sleep的时候CPU用给了别的进程,所以得到的时间不是我们想要的,
所以如果在一个有很多sleep的程序中用clock( )函数是不准确的。
2
time_t相关函数:
time_t time( time_t *t ); /* 返回从1970年1月1日至今的秒数 */
char *ctime( const time_t *timep ); /* 将time_t表示的时间转换成真实世界的时间日期格式,并以字符串形式返回 */
int stime( time_t *t ); /* 将t指针所指的时间设置给系统 */
double difftime( time_t timeS, time_t timeF ); /* 获取两个time_t的时间差值,移植性较强 */
程序:
#include <stdio.h>
#include <time.h>
#include <errno.h>
int main( void )
{
time_t TimeF, TimeS;
char *Sptr = NULL;
TimeF = time( &TimeF );
Sptr = ctime( &TimeF );
printf( "seconds = %ld, Real Time : %s\r\n", TimeF, Sptr );
if( stime( &TimeF ) )
{
printf( "%s, set time failed!\r\n", strerror( errno ) );
}
sleep( 1 );
TimeS = time( &TimeS );
printf( "Time diff = %lf\r\n", difftime( TimeS, TimeF ) );
return 0;
}
运行结果:
seconds = 1370488022, Real Time : Thu Jun 6 11:07:02 2013
Operation not permitted, set time failed!
Time diff = 1.000000
如果要stime成功的话可以切换的root权限,结果为:
seconds = 1370488009, Real Time : Thu Jun 6 11:06:49 2013
Time diff = 1.000000
3
{
int tm_sec; /* 秒——[ 0, 59 ] */
int tm_min; /* 分——[ 0, 59 ] */
int tm_hour; /* 时——[ 0, 23 ] */
int tm_mday; /* 天——[ 1, 31 ] */
int tm_mon; /* 月——[ 0, 11 ] */
int tm_year; /* 年——等于实际年份减去1900 */
int tm_wday; /* 星期——[ 0, 6 ], 0:星期天, 1:周一 */
int tm_yday; /* 每年的1月1日开始的天数——[ 0, 365 ], 0:1月1日 */
int tm_isdst; /* 夏令时标识符,实行夏令时为正, 不实行为0, 不了解情况为负 */
};
函数:
struct tm * gmtime( const time_t *timep ); /* 将time_t 时间转换成tm时间, 是UTC时间 */
struct tm * localtime( const time_t * timep ); /* 将time_t 时间转换成tm时间, 是本地时间 */
char * asctime( const struct tm * timeptr ); /* 将tm转换成字符串形式返回 */
time_t mktime( struct tm * timeptr ); /* 将tm表示的时间转换成time_t */
程序:
#include <stdio.h>
#include <time.h>
#include <errno.h>
int main( void )
{
time_t TimeF, TimeS;
struct tm *Ptr = NULL;
char *TimePtr = NULL;
TimeF = time( &TimeF );
printf( "Seconds = %ld\r\n", TimeF );
Ptr = localtime( &TimeF );
TimePtr = asctime( Ptr );
printf( "Time : %s\r\n", TimePtr );
TimeS = mktime( Ptr );
printf( "Seconds = %ld\r\n", TimeS );
return 0;
}
运行结果:
Seconds = 1370490079
Time : Thu Jun 6 11:41:19 2013
Seconds = 1370490079
4
struct timeval
{
int tv_sec;
int tv_usec;
};
函数:
int gettimeofday( struct timeval * tv, struct timezone * tz );
int settimeofday( const struct timeval * tv, const struct timezone *tz ); /* root权限才允许 */