一、时间获取
#include <time.h>
time_t time(time_t * tloc)
功能:获取日历时间,既从1970年1月1日0点到现在所经历的秒数
例子:
二、时间转化
struct tm * gmtime(const time_t * timeep)
功能:将日历时间转化为格林威治标准时间,并保存至TM结构
struct tm * localtime(const time_t * timep)
功能:将日历时间转化为本地时间,并保存至TM结构
TM结构:
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)
功能:将tm格式的时间转化为字符串
char * ctime(const time_t * timep)
功能:将日历时间转化为本地时间的字符串形式
四、获取时间
int gettimeofday(struct timeval * tv, struct timezone * tz)
功能:获取从今日凌晨到现在的时间差,常用于计算事件耗时
struct timeval{
int tv_sec; //秒数
int tv_usec; //微秒数
}
五、延时执行
unsigned int sleep(unsigned int seconds)
功能:是程序睡眠seconds秒
void usleep(unsigned long usec)
功能:使程序睡眠usec微秒
#include <time.h>
time_t time(time_t * tloc)
功能:获取日历时间,既从1970年1月1日0点到现在所经历的秒数
例子:
#include <time.h>
#include <stdio.h>
int main(){
struct tm * local;
time_t t;
t = time(NULL);
local = localtime(&t);
printf("Local hour is:%d %d\n", local->tm_hour, local->tm_min);
local = gmtime(&t);
printf("UTC hour is:%d\n", local->tm_hour);
return 0;
}
二、时间转化
struct tm * gmtime(const time_t * timeep)
功能:将日历时间转化为格林威治标准时间,并保存至TM结构
struct tm * localtime(const time_t * timep)
功能:将日历时间转化为本地时间,并保存至TM结构
TM结构:
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)
功能:将tm格式的时间转化为字符串
char * ctime(const time_t * timep)
功能:将日历时间转化为本地时间的字符串形式
#include <time.h>
#include <stdio.h>
int main(){
struct tm * ptr;
time_t lt;
lt = time(NULL);
ptr = gmtime(<);
printf("%s", asctime(ptr));
printf("%s", ctime(<));
return 0;
}
四、获取时间
int gettimeofday(struct timeval * tv, struct timezone * tz)
功能:获取从今日凌晨到现在的时间差,常用于计算事件耗时
struct timeval{
int tv_sec; //秒数
int tv_usec; //微秒数
}
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void function(){
unsigned int i, j;
double y;
for(i = 0; i < 1000; i++){
for(j = 0; j < 1000; j++){
y++;
}
}
}
int main(){
struct timeval tpstart, tpend;
float timeuse;
gettimeofday(&tpstart, NULL);
function();
gettimeofday(&tpend, NULL);
timeuse = 1000000 * (tpend.tv_sec - tpstart.tv_sec) + tpend.tv_usec - tpstart.tv_usec;
timeuse /= 1000000;
printf("Used Time:%f\n", timeuse);
exit(0);
return 0;
}
五、延时执行
unsigned int sleep(unsigned int seconds)
功能:是程序睡眠seconds秒
void usleep(unsigned long usec)
功能:使程序睡眠usec微秒