(1)
time_t time( time_t * t );
此函数会返回从公元1970年1月1日的UTC时间从0时0分0秒算起到现在所经过的秒数。如果t 并非空指针的话,此函数也会将返回值存到t指针所指的内存。
(2)
struct tm *localtime( const time_t *timer );
struct tm* localtime_r( const time_t* timer, struct tm* result );
将time_t*所指的秒数转换得到当前时间的tm结构
(3)
time_t mktime( struct tm* timeptr );
用来将参数timeptr所指的tm结构数据转换成从公元1970年1月1日0时0分0 秒算起至今的UTC时间所经过的秒数。与localtime_r互为相反过程。
(4)
char* ctime( const time_t* timer );
char* ctime_r( const time_t* timer, char* buf );
将time_t*所指的秒数格式化输出。
将参数tm的时间结构依照参数format所指定的字符串格式做转换,转换后的字符串将复制到参数s所指的字符串数组中,该字符串的最大长度为参数max所控制。可实现与ctime相反的操作。
int gettimeofday( struct timeval * when, void * not_used );
获取当前时间。前者使用的数据结构中有一个纳秒级的参数,而后者是微秒,所以可以认为前者的精度比较高。
设置当前时间
对应结构体定义:
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
};
{
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);
}
time_t time( time_t * t );
此函数会返回从公元1970年1月1日的UTC时间从0时0分0秒算起到现在所经过的秒数。如果t 并非空指针的话,此函数也会将返回值存到t指针所指的内存。
(2)
struct tm *localtime( const time_t *timer );
struct tm* localtime_r( const time_t* timer, struct tm* result );
将time_t*所指的秒数转换得到当前时间的tm结构
(3)
time_t mktime( struct tm* timeptr );
用来将参数timeptr所指的tm结构数据转换成从公元1970年1月1日0时0分0 秒算起至今的UTC时间所经过的秒数。与localtime_r互为相反过程。
(4)
char* ctime( const time_t* timer );
char* ctime_r( const time_t* timer, char* buf );
将time_t*所指的秒数格式化输出。
(5)
size_t strftime(char *s, size_t max, const char *format, const struct tm *tm);将参数tm的时间结构依照参数format所指定的字符串格式做转换,转换后的字符串将复制到参数s所指的字符串数组中,该字符串的最大长度为参数max所控制。可实现与ctime相反的操作。
(6)
int clock_gettime( clockid_t clock_id, struct timespec * tp );int gettimeofday( struct timeval * when, void * not_used );
获取当前时间。前者使用的数据结构中有一个纳秒级的参数,而后者是微秒,所以可以认为前者的精度比较高。
(7)
int clock_settime( clockid_t id, const struct timespec * tp );
int settimeofday( const struct timeval *when, void *not_used );设置当前时间
对应结构体定义:
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
};
举例:
(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);
}