计算时间间隔
一、函数说明
1.简介:
在C语言中可以使用函数gettimeofday()函数来得到时间。它的精度可以达到微妙
2.函数原型:
#include<sys/time.h>
int gettimeofday(struct timeval*tv,struct timezone *tz )
3.说明:
gettimeofday()会把目前的时间用tv 结构体返回,当地时区的信息则放到tz所指的结构中
4.结构体:
struct timeval{
long tv_sec;/*秒*/
long tv_usec;/*微妙*/
};
struct timezone{
int tz_minuteswest;/*和greenwich 时间差了多少分钟*/
int tz_dsttime;/*type of DST correction*/
}
3>在gettimeofday()函数中tv或者tz都可以为空。如果为空则就不返回其对应的结构体。
4>函数执行成功后返回0,失败后返回-1,错误代码存于errno中。
二、例程
例1
#include<stdio.h>
#include<sys/time.h>
#include<unistd.h>
int main()
{
struct timeval tv;
struct timezone tz;
gettimeofday(&tv,&tz);
printf(“tv_sec:%d\n”,tv.tv_sec);
printf(“tv_usec:%d\n”,tv.tv_usec);
printf(“tz_minuteswest:%d\n”,tz.tz_minuteswest);
printf(“tz_dsttime:%d\n”,tz.tz_dsttime);
}
说明:在使用gettimeofday()函数时,第二个参数一般都为空,因为我们一般都只是为了获得当前时间,而不用获得timezone的数值
例2::了解程序执行所需的时间
测试调用delya()函数所需执行的时间(单位为微妙)
#include<stdio.h>
#include<sys/time.h>
#include<unistd.h>
int delay(int time)
{
int i,j;
for(i =0;i<time;i++)
for(j=0;j<5000;j++)
;
}
int main()
{
struct timeval start;
struct timeval end;
unsigned long diff;
gettimeofday(&start,NULL);
delay(10);
gettimeofday(&end,NULL);
diff = 1000000 * (end.tv_sec-start.tv_sec)+ end.tv_usec-start.tv_usec;
printf(“thedifference is %ld\n”,diff);
return 0;
}
获得当前时间
一、函数说明
用到的主要函数:time、ctime(或使用localtime)
头文件:time.h
函数声明:
time_t time(time_t *t);
char *ctime(const time_t *timep);
struct tm* localtime(const time_t *timep)
函数意义:
time:
此函数会返回从公元1970 年1月1日的UTC时间从0时0分0秒算起到现在所经过的秒数。
如果t并非空指针的话,此函数也会将返回值存到t 指针所指的内存。
ctime:
此函数将参数tmep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,
然后将结果以字符串的形态返回
localtime:
localtime()将参数timep所指的time_t结构中的信息转换成当地时区的时间。
结构体tm的定义如下:
struct tm
{
int tm_sec; // 代表目前秒数, 正常范围为0-59, 但允许至61 秒
int tm_min; // 代表目前分数, 范围0-59
int tm_hour; // 从午夜算起的时数, 范围为0-23
int tm_mday; // 目前月份的日数, 范围01-31
int tm_mon; // 代表目前月份, 从一月算起, 范围从0-11
int tm_year; // 从1900 年算起至今的年数
int tm_wday; // 一星期的日数, 从星期一算起, 范围为0-6
int tm_yday; // 从今年1 月1 日算起至今的天数, 范围为0-365
int tm_isdst; // 日光节约时间的旗标
};
二、例程
例程1:
#include<stdio.h>
#include<sys/time.h>
#include<unistd.h>
int main()
{
char *str_local = NULL;
time_t time_local;
time(&time_local);
str_local = ctime(&time_local);
printf("local time : %s\n",str_local);
return 0;
}
运行结果:
local time : Thu Oct 4 16:39:23 2018
例程2:
#include<stdio.h>
#include<sys/time.h>
#include<unistd.h>
void gettime(char *local_time) {
char Year[6] = {0};
char Month[4] = {0};
char Day[4] = {0};
char Hour[4] = {0};
char Min[4] = {0};
char Sec[4] = {0};
time_t current_time;
struct tm* now_time;
time(¤t_time);
now_time = localtime(¤t_time);
strftime(Year, sizeof(Year), "%Y-", now_time);
strftime(Month, sizeof(Month), "%m-", now_time);
strftime(Day, sizeof(Day), "%d ", now_time);
strftime(Hour, sizeof(Hour), "%H:", now_time);
strftime(Min, sizeof(Min), "%M:", now_time);
strftime(Sec, sizeof(Sec), "%S", now_time);
strncat(local_time, Year, 5);
strncat(local_time, Month, 3);
strncat(local_time, Day, 3);
strncat(local_time, Hour, 3);
strncat(local_time, Min, 3);
strncat(local_time, Sec, 3);
}
int main(int argc,char **argv)
{
char *local_time = (char *)malloc(21*sizeof(char));
gettime(local_time);
printf("local time: %s\n", local_time);
free(local_time);
local_time = NULL;
return 0;
}
运行结果:
local time: 2018-10-04 16:41:01