日历时间: 从1970年1月1日0时到现在的描述
头文件: time.h
1.获取日历时间
返回值和参数的指向time_t都是日历时间
例如:
2.时间转化
转化为格林威治时间(Greenwich mean time)
转化为本地时间
转化为格林威治时间(Greenwich mean time)
转化为字符串
Sat Jul 30 08:23:33 2005
转化为本地字符串
3. 获取凌晨到现在的时间差
常用两次gettimeofday计算程序运行的时间
4.延时执行
睡眠seconds秒
睡眠usec微秒
头文件: time.h
1.获取日历时间
time_t time(time_t *tloc)
返回值和参数的指向time_t都是日历时间
例如:
time_t t=time(NULL);
2.时间转化
转化为格林威治时间(Greenwich mean time)
struct tm *gmtime(const time_t *timep)
转化为本地时间
struct tm *localtime(const time_t *timep)
转化为格林威治时间(Greenwich mean time)
struct tm{
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;//本月第几天
int tm_mon;
int tm_year;//tm_year+1900=公元
int tm_wday;//本周第几天
int tm_yday;//本年第几日
int tm_isdst;//日光节约时间
};
转化为字符串
char *asctime(const struct tm *tm)
Sat Jul 30 08:23:33 2005
转化为本地字符串
char *ctime(const time_t *timep)
3. 获取凌晨到现在的时间差
int gettimeofday(struct timeval *tv, struct timezone *tz)
struct timeval{
int tv_sec;//秒数
int tv_usec;//微秒数
}
常用两次gettimeofday计算程序运行的时间
struct timeval start, end;
float timeuse;
gettimeofday(&start, NULL);
function();
gettimeofday(&end, NULL);
timeuse=1000000 *(end.tv_sec - start.tv_sec)+end.tv_usec-start.tv_usec;
timeuse/=1000000l
printf("Used time: %f\n", timeuse);
4.延时执行
睡眠seconds秒
unsigned int sleep(unsigned int seconds)
睡眠usec微秒
void sleep(unsigned long usec)