C语⾔的时间处理库
time库中的常用函数
1、函数名称: time
2、函数名称: gmtime
3、函数名称: ctime
4、函数名称: asctime
5、函数名称: localtime
6、函数名称:mktime
7、函数名称:difftime
8、函数名称:gettimeofday
1、time
函数原型:time_t time(time_t * timer)
功能: 获取当前的系统时间,返回的结果是⼀个time_t类型, 其中time_t是⼀ 个⻓整型,⼀般⽤来表示⽤1970年以来的秒数。返回⾃纪元Epoch(1970-01- 01 00:00:00 UTC)起经过的时间秒数,UTC时间也即是格林尼治标准时间 GMT(世界协调时间)。
如果参数timer是⼀个空指针,函数会返回⼀个time_t的⻓整形。如果不是空指 针,函数将结果存放到timer指针指向的内存单元中。
测试1:
#include <stdio.h>
#include <time.h>
int main()
{
time_t t= time(NULL);
printf("time :%ld\n",(long)t);
return 0;
}
附:time也可以用来生产随机数
//例:0~99内随机生成10个数
#include <stdio.h>
#include <time.h>
#include<stdlib.h>
int main(void)
{
int i;
srand((unsigned) time(NULL));
printf("ten random numbers from 0 to 99\n\n");
for(i=0;i<10;i++)
{
printf("%d\n",rand()%100);
}
return 0;
}
2. gmtime
函数原型:struct tm *gmtime(const time_t *timep);
将time_t表示的秒数时间转换为我们⽇常可以理解的时间 。
函数的参数timep是⼀个表示当前时间秒数的指针。
返回值⼀个tm类型的结构体指针。
*注意
1、tm_hour 表示的是UTC的时间,没有经过时区转换
2、tm_mon ⽉份范围是:0-11. 所以打印时需要加⼀。
3、tm_year 年份是从1900年开始的年数,所以打印时需要加1900。
测试2
#include <stdio.h>
#include <time.h>
#include<stdlib.h>
int main()
{
time_t t=time(NULL);
printf("Time:%ld\n",(long)t);//当前时间的秒数
struct tm *gt;
gt=gmtime(&t);
printf("gt->tm_sec:%d \n", gt->tm_sec);
printf("gt->tm_min:%d \n", gt->tm_min);
printf("gt->tm_hour:%d \n", gt->tm_hour);//是UTC时 间,未经时区转换
printf("gt->tm_mday:%d \n", gt->tm_mday);
printf("gt->tm_mon:%d \n", gt->tm_mon+1);//范围是0-11
printf("gt->tm_year:%d \n", gt->tm_sec+1900);//从1900年开始的年数 15 printf("gt->
printf("gt->tm_wday:%d \n", gt->tm_wday);
printf("gt->tm_yday:%d \n", gt->tm_yday);
printf("gt->tm_isdst:%d \n", gt->tm_isdst);
return 0;
}
3、ctime
函数原型:char *ctime(const time_t *timep);
函数功能: 将时间转换成⼀个可以识别的字符串格式
函数返回值:成功返回时间字符串,失败返回NULL.
函数的参数timep是⼀个表示当前时间秒数的指针。
asctime 函数与 ctime 函数返回的字符串格式是相同的。
区别在于输⼊的格式不同:
asctime 函数:输⼊ struct tm 格式的时间。
ctime 函数:输⼊ time_t 格式的时间。
测试3
#include <stdio.h>
#include <time.h>
int main(int agrc,char** argv)
{
time_t t = time(NULL);
char *ct=NULL;
ct=ctime(&t);
printf("Ctime :%s", ct);
return 0;
}
4、asctime
函数原型: char* asctime(struct tm * ptr)
功能:它也是⼀种字符串格式化函数,将tm格式的时间转换为 ⼀个字符串格式。
函数返回: 返回的时间字符串格式为:星期,⽉,⽇,⼩时:分:秒,年
参数说明: 结构指针ptr应通过函数localtime()和gmtime()得到
测试4:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
if(1 != argc)
{
printf("Not Found Param ! \n");
return -1;
}
int param = atoi(argv[1]);
time_t t= time(NULL); //获取当前时间
struct tm *gt;
if(1 == param)
{
gt = gmtime(&t); //把当前时间转换为struct tm 格式 :获取 的是UTC时间:没有经过时区转换
}
else
{
gt = localtime(&t); //把当前时间转换为struct tm 格式 :获取的是本地时间:经过时区转换之后的
}
char *asct;
asct = asctime(gt); //把当前时间转换为字符串格式
printf("asctime :%s",asct);
return 0;
}
上测试中:
1、先使⽤ time 函数获取本地时间,得到 time_t 格式的时间。
2、然后把 time_t 格式的时间作为参数,通过 gmtime 函数或者 localtime 函数,得到 struct tm 格式的时间。
3、然后使⽤ asctime 函数,把 struct tm 格式的时间,转换为字符串。
参数为1,表示使⽤UTC时间:显示的上午07点钟。
参数不为1,表示使⽤本地时间:显示的是下午15点钟。
5、localtime
函数原型: struct tm *localtime(const time_t *timer)
函数功能: 返回⼀个以tm结构表达的机器时间信息(本地时间)
参数说明: timer-使⽤time()函数获得的机器时间
测试5
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main()
{
time_t t=time(NULL);
printf("Time :%ld \n", (long)t);
struct tm *gt;
gt = localtime(&t);
printf("gt->tm_sec:%d \n", gt->tm_sec);
printf("gt->tm_min:%d \n", gt->tm_min);
printf("gt->tm_hour:%d \n", gt->tm_hour);//是本地时间,经过时区转换后的
printf("gt->tm_mday:%d \n", gt->tm_mday);
printf("gt->tm_mon:%d \n", gt->tm_mon+1);//范围 是0-11
printf("gt->tm_year:%d \n", gt->tm_sec+1900);//从1 900年开始的年数
printf("gt->tm_wday:%d \n", gt->tm_wday);
printf("gt->tm_yday:%d \n", gt->tm_yday);
printf("gt->tm_isdst:%d \n", gt->tm_isdst);
return 0;
}
其中gmtime与localtime函数的区别是:
gmtime:获取的是UTC时间,没有进过时区转换。
localtime:获取的是本地时间,进过了时区转换。
6、mktime
函数原型:time_t mktime(struct tm *tm);
功能:将⼀个tm格式时间转换成秒数。
返回值: 成功返回time_t格式的时间,失败返回(time_t)-1。
mktime 函数的作⽤是,把 struct tm 格式的时间,转换为 time_t 格式的时间
测试6
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void Print(struct tm *gt)
{
printf("gt->tm_sec:%d \n", gt->tm_sec);
printf("gt->tm_min:%d \n", gt->tm_min);
printf("gt->tm_hour:%d \n", gt->tm_hour);//是本地时间,经过时区转换后的
printf("gt->tm_mday:%d \n", gt->tm_mday);
printf("gt->tm_mon:%d \n", gt->tm_mon+1);//范围 是0-11
printf("gt->tm_year:%d \n", gt->tm_sec+1900);//从1 900年开始的年数
printf("gt->tm_wday:%d \n", gt->tm_wday);
printf("gt->tm_yday:%d \n", gt->tm_yday);
printf("gt->tm_isdst:%d \n", gt->tm_isdst);
}
int main(int argc, char **argv)
{
if(1 != argc)
{
printf("Not Found Param ! \n");
return -1;
}
int param = atoi(argv[1]); //输?参数
time_t t = time(NULL); //获取time_t 格式的时间 (? 前时间)
printf("time :%ld \n", (long)t);
struct tm *gt; //把 time_t 格式的时间,转换 为 struct tm 格式的时间
if(1 == param)
gt = gmtime(&t);
else
gt = localtime(&t);
Print(gt);
time_t t2;
t2 = mktime(gt); //把 struct tm 格式的时间,转 换为time_t 格式的时间
printf("time2 :%ld \n", (long)t2);
return 0;
}
给 mktime 函数输⼊的 struct tm 参数为本地时间(即 经过时区转换之后的时间)。
从上⾯的截图也可以看出: 输⼊参数不为1时: 输⼊参数为1时:
7、difftime
C语⾔函数difftime() 头⽂件:#include <time.h>
定义函数: double difftime(time_t time2, time_t time1);
函数说明: 返回两个time_t型变量之间的时间间隔,即 计算两个时刻之间的时间差。
测试7
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
time_t t1=time(NULL);
printf("t1 is : %d\n",t1);
sleep(2);
time_t t2=time(NULL);
printf("t2 is : %d\n",t2);
float tinterval=difftime(t2,t1);
printf("the time interval is: %lf\n",tinterval);
return 0;
}
8、gettimeofday
在C语⾔中可以使⽤函数gettimeofday()函数来得到时间。它的精度可以达到微妙 #include<sys/time.h> int gettimeofday(struct timeval*tv,struct timezone *tz )
gettimeofday()会把⽬前的时间⽤tv 结构体返回,当地时区的信息则放到tz所指的结构中
1.timeval struct timeval{ long tv_sec;/*秒*/ long tv_usec;/*微妙*/ };
2.timezone 结构定义为: struct timezone{ int tz_minuteswest;/*和greenwich 时间差了多少分钟*/ int tz_dsttime;/*type of DST correction*/ }
3.在gettimeofday()函数中tv或者tz都可以为空。如果为空则就不返回其对应的结构体。
4.函数执⾏成功后返回0,失败后返回-1,错误代码存于errno中。
测试8
/*time.h 是ISO C99 标准?期时间头文件zhi。 sys/time.h 是Linux 系统的?期时间头文件。
sys/time.h 通常都会包含
include time.h
#include<sys/time.h>
linux下搜索头文件的标准路径有:/usr/include,/usr/local/include
前面的字符串sys表示标准路径下的文件夹名,后?的字符串time表示在linux标准路径下的各件夹下的头文件名 */
#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);
return 0;
}