1.时间类型
1) time_t是一个长整型,一般用来表示用1970年以来的秒数。
2)struct timeval有两个成员,一个是秒,一个是微妙。
struct timeval
{
long tv_sec;
long tv_usec;
};
3) struct timespec有两个成员,一个是秒,一个是纳秒。
struct timespec
{
time_t tv_sec;
long tv_nsec;
};
4) struct 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;
};
5)
struct timeb
{
time_t time; //从1970来经过的秒数
unsigned short millitm; //毫秒
short timezone; //时区
short dstflag;//为日光节约时间的修正值,如果为非0代表启用日光节约修正
};
6)
struct timezone
{
int tz_minuteswest;//和格林尼治时间相差多少分
int tz_dsttime;//日光节约时间的状态
};
日光节约时间:夏时制。
2.时间函数
1) char *asctime(const struct tm *timeptr)
将时间和日期以字符串格式显示。
2)clock_t clock(void)
取得进程占用cpu的大约时间。
3)char *ctime(const time_t *timep)
将时间和日期以字符串格式显示。
4) double difftime(time_t time1, time_t time0)
计算时间time1和time0间的差距。
5) int ftime(struct timeb *tp)
取得目前的时间和日期。
6) int gettimeofday(struct timeval *tv, struct timezone *tz)
取得目前的时间。
7)strcut tm *gmtime(const time_t *timep)
time_t结构时间转tm结构时间,tm为UTC时区。
8) struct tm *localtime(const time_t *timep)
将time_t结构时间转tm结构时间,tm是当地时区。
9)time_t mktime(struct tm *timeptr)
将tm结构时间转换为time_t。
10) int settimeofday(const struct timeval *tv, const struct timezone *tz)
设置时间为tv,设置时区为tz。
11) size_t strftime(char *s, size_t max, const char *format, const struct tm *tm)
将参数tm的时间结构依照参数format所指定的字符串格式做转换,转换后的字符串将复制到参数s所指的字符串数组中,该字符串的最大长度为参数max所控制。
12) time_t time(time_t *t)
取得目前的时间,时间按照UTC。
13) void tzset(void)
从环境变量TZ取得目前当地的时间。
3.延迟函数
主要的延迟函数有:sleep(),nap(),napms(),usleep(),delay(),nanosleep(),select(), pselect().
1) unsigned int sleep(unsigned int seconds)
延时seconds秒。
2) unsigned int nap( unsigned int milli_seconds );
int napms( int milli_seconds );
延时 milli_seconds 毫妙
3) int usleep( useconds_t micro_seconds);
unsigned int delay( unsigned int micro_seconds);
延时 micro_seconds微妙
4) int nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
延时的时间为rqtp,如果nansleep被信号中断,且rmtp不为NULL,则rmtp指定的位置上包含的就是剩余时间。
5) int select(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
如果将readfds, writefds, exceptfds置为NULL,timeout为非零,则延时timeout。
5) int pselect(int maxfdp1, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, const struct timespec tsptr, const sigset_t *sigmask)
如果将readfds, writefds, exceptfds置为NULL,tsptr为非零,则延时tsptr。
struct tm
{
int tm_sec; //seconds after the minute -- [0,61]
int tm_min; //minutes after the hour -- [0,59]
int tm_hour; //hours after 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 Savings Time flag
long int tm_gmtoff; //Offset from gmt
const char *tm_zone; //String for zone name
};
struct timespec
{
time_t tv_sec;//The number of seconds since 1970
long tv_nsec;//The number of nanoseconds
}
struct timeval
{
int tv_sec; //seconds
int tv_usec; //microseconds
};
struct itimerspec
{
struct timespec it_interval; //timer period
struct timespec it_value; //timer expiration
};
struct itimerval
{
struct timeval it_interval; //timer interval
struct timeval it_value; //current value
};
time_t time( time_t * t );
此函数会返回从公元1970年1月1日的UTC时间从0时0分0秒算起到现在所经过的秒数。如果t 并非空指针的话,此函数也会将返回值存到t指针所指的内存。
struct tm *localtime( const time_t *timer );
struct tm* localtime_r( const time_t* timer, struct tm* result );
将time_t*所指的秒数转换得到当前时间的tm结构
time_t mktime( struct tm* timeptr );
用来将参数timeptr所指的tm结构数据转换成从公元1970年1月1日0时0分0 秒算起至今的UTC时间所经过的秒数。与localtime_r互为相反过程。
char* ctime( const time_t* timer );
char* ctime_r( const time_t* timer, char* buf );
将time_t*所指的秒数格式化输出。
size_t strftime(char *s, size_t max, const char *format, const struct tm *tm);
将参数tm的时间结构依照参数format所指定的字符串格式做转换,转换后的字符串将复制到参数s所指的字符串数组中,该字符串的最大长度为参数max所控制。可实现与ctime相反的操作。
int clock_gettime( clockid_t clock_id, struct timespec * tp );
int gettimeofday( struct timeval * when, void * not_used );
获取当前时间。前者使用的数据结构中有一个纳秒级的参数,而后者是微秒,所以可以认为前者的精度比较高。
int clock_settime( clockid_t id, const struct timespec * tp );
int settimeofday( const struct timeval *when, void *not_used );
设置当前时间
例1:
typedef struct
{
unsigned short Year; //year;
unsigned char Month; //month;
unsigned char Day; //day;
unsigned char Hour; //hour;
unsigned char Minute; //minute;
unsigned char Sec; //second;
unsigned char Week;
}TS;
//获取日期
void TSGet(TS *ts) {
struct tm tmp_tm;
time_t time_of_day;
time_of_day = time(NULL);
localtime_r(&time_of_day, &tmp_tm);
ts->Year = tmp_tm.tm_year + 1900;
ts->Month = tmp_tm.tm_mon + 1;
ts->Day = tmp_tm.tm_mday;
ts->Hour = tmp_tm.tm_hour;
ts->Minute = tmp_tm.tm_min;
ts->Sec = tmp_tm.tm_sec;
ts->Week = tmp_tm.tm_wday;
}
//设置时间
void TSSet(TS ts) {
struct tm tmpsec;
struct timespec rtime;
struct timespec cur_time;
tmpsec.tm_sec = ts.Sec;
tmpsec.tm_min = ts.Minute;
tmpsec.tm_hour = ts.Hour;
tmpsec.tm_mday = ts.Day;
tmpsec.tm_mon = ts.Month - 1;
tmpsec.tm_year = ts.Year - 1900;
rtime.tv_sec = mktime(&tmpsec);
clock_gettime(CLOCK_REALTIME, &cur_time);
rtime.tv_nsec = cur_time.tv_nsec;
clock_settime(CLOCK_REALTIME, &rtime);
}
例2:
int main(int argc, char *argv[])
{
struct tm tmp_tm;
time_t time_of_day;//1970/1/1 0:0:0至今的秒数
struct timespec spec;
char buf[30];
time_of_day = time(NULL);
printf("time_of_day = %ld/n", time_of_day);
localtime_r(&time_of_day, &tmp_tm);
printf("%d-%d-%d, %d:%d:%d/n", tmp_tm.tm_year,tmp_tm.tm_mon,tmp_tm.tm_mday,
tmp_tm.tm_hour,tmp_tm.tm_min,tmp_tm.tm_sec);
spec.tv_sec = mktime(&tmp_tm);
printf("spec = %ld/n", spec.tv_sec);
ctime_r(&spec.tv_sec, buf);
printf("%s/n",buf);
}
结果:
time_of_day = 1276169161
110-5-10, 11:26:1
spec = 1276169161
Thu Jun 10 11:26:01 2010
例3:
void function()
{
unsigned int i;
sleep(3);
for(i=0;i<100;i++ )
printf("i = %d/n",i);
}
int main(int argc, char *argv[])
{
struct timeval tpstart,tpend;
float timeuse=0;
gettimeofday(&tpstart,NULL);
function();
gettimeofday(&tpend,NULL);
timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec);
timeuse=timeuse+tpend.tv_usec-tpstart.tv_usec;
timeuse/=1000000;
printf("Used Time:%f/n",timeuse);
}
转自:http://blog.sina.com.cn/s/blog_590be5290100j0e0.html