- 常用时间类型
- time_t
- struct tm
- struct timeval
- struct timespec
- 常用时间函数
- time
- ctime
- gmtime
- localtime
- mktime
- asctime
- difftime
time_t类型在time.h中定义:
#ifndef __TIME_T #define __TIME_T typedef long time_t; #endifstruct tm
tm结构在time.h中定义:
#ifndef _TM_DEFINED
#define _TM_DEFINED
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;
};
#endif
struct timeval
timeval结构体在time.h中定义:
struct tmieval {
time_t tv_sec;
suseconds_t tv_usec;
};
struct timespec
timespec结构体在time.h中定义:
struct timespec {
time_t tv_sec;
long tv_nsec;
};
常用时间函数
time
函数原型如下:
#includetime_t time ( time_t *timer );
函数返回从1970年1月1日00时00分00秒至今所经过的秒数。
如果time_t *timer为非空指针,函数也会将返回值存到timer指向的内存。
#includectime#include int main ( void ) { time_t seconds; seconds = time ( ( time_t * ) NULL ); printf ( "seconds is %ldn", seconds ); }
函数原型如下:
#includechar *ctime ( const time_t *timep );
函数将参数timep指向的time_t时间信息转换成实际所使用的时间日期表示方法,并以字符串形式返回。
#includegmtime#include int main ( void ) { time_t seconds; seconds = time ( NULL ); printf ( "%s", ctime ( &seconds ) ); }
函数原型如下:
#includestruct tm *gmtime ( const time_t *timep );
函数将参数timep指向的time_t时间信息转换成以tm结构体表示的GMT时间信息,并以struct tm *指针返回。
GMT是中央时区,北京在东8区,相差8个小时,所以北京时间 = GMT时间 + 8小时。
#includelocaltime#include int main ( void ) { struct tm *local; time_t t; t = time ( NULL ); local = localtime ( &t ); printf ( "Local hour is: %dn", local->tm_hour ); local = gmtime ( &t ); printf ( "UTC hour is: %dn", local->tm_hour ); return 0; }
函数原型如下:
#includestruct tm *localtime ( const time_t *timep );
该函数将参数timep指向的time_t时间信息转换成以tm结构体表示的本地时区时间。
#includemktime#include int main ( void ) { char *wday[] = { ( char* ) "Sun", ( char* ) "Mon", ( char* ) "Tue", ( char* ) "Wed", ( char* ) "Thu", ( char* ) "Fri", ( char* ) "Sat" }; time_t timep; struct tm *p_tm; timep = time ( NULL ); p_tm = localtime ( &timep ); printf ( "%d-%d-%d ", ( p_tm->tm_year + 1900 ), ( p_tm->tm_mon + 1 ), p_tm->tm_mday ); printf ( "%s %d:%d:%dn", wday[p_tm->tm_wday], p_tm->tm_hour, p_tm->tm_min, p_tm->tm_sec ); return 0; }
函数原型如下:
#includetime_t mktime ( struct tm *p_tm );
该函数将参数p_tm指向的tm结构体数据转换成从1970年1月1日00时00分00秒至今的GMT时间经过的秒数。
#includeasctime#include int main ( void ) { time_t timep; struct tm *p_tm; timep = time ( NULL ); printf ( "time(): %ldn", timep ); p_tm = localtime ( &timep ); timep = mktime ( p_tm ); printf ( "time() -> localtime() -> mktime(): %ldn", timep ); return 0; }
函数原型如下:
#includechar *asctime ( const struct tm *p_tm );
该函数将参数p_tm指向的tm结构体数据转换成实际使用的时间日期表示方法,并以字符串形式返回。
#includedifftime#include int main ( void ) { time_t timep; timep = time ( NULL ); printf ( "%s", asctime ( gmtime ( &timep ) ) ); return 0; }
函数原型如下:
#includedouble difftime ( time_t timep1, time_t timep2 );
该函数比较参数timep1和timep2时间是否相同,并返回之间相差秒数。
#include#include #include int main ( void ) { time_t timep1, timep2; timep1 = time ( NULL ); sleep ( 2 ); timep2 = time ( NULL ); printf ( "the difference is %f secondsn", difftime ( timep1, timep2 ) ); return 0; }



