time_t tm timeval 和 时间字符串的转换方法
1、常用的时间存储方式
1)time_t类型,这本质上是一个长整数,表示从1970-01-0100:00:00到目前计时时间的秒数,如果需要更精确一点的,可以使用timeval精确到毫秒。
2)tm结构,这本质上是一个结构体,里面包含了各时间字段
structtm{
inttm_sec;/*secondsaftertheminute-[0,59]*/
inttm_min;/*minutesafterthehour-[0,59]*/
inttm_hour;/*hourssincemidnight-[0,23]*/
inttm_mday;/*dayofthemonth-[1,31]*/
inttm_mon;/*monthssinceJanuary-[0,11]*/
inttm_year;/*yearssince1900*/
inttm_wday;/*dayssinceSunday-[0,6]*/
inttm_yday;/*dayssinceJanuary1-[0,365]*/
inttm_isdst;/*daylightsavingstimeflag*/
};
其中tm_year表示从1900年到目前计时时间间隔多少年,如果是手动设置值的话,tm_isdst通常取值-1。
3)structtimeval结构体在time.h中的定义为
structtimeval{
time_ttv_sec;/*seconds*/
suseconds_ttv_usec;/*microseconds*/
};
2、常用的时间函数
time_ttime(time_t*t);//取得从1970年1月1日至今的秒数 char*asctime(conststructtm*tm);//将结构中的信息转换为真实世界的时间,以字符串的形式显示 char*ctime(consttime_t*timep);//将timep转换为真是世界的时间,以字符串显示,它和asctime不同就在于传入的参数形式不一样 structtm*gmtime(consttime_t*timep);//将time_t表示的时间转换为没有经过时区转换的UTC时间,是一个structtm结构指针 structtm*localtime(consttime_t*timep);//和gmtime类似,但是它是经过时区转换的时间。 time_tmktime(structtm*tm);//将structtm结构的时间转换为从1970年至今的秒数 intgettimeofday(structtimeval*tv,structtimezone*tz);//返回当前距离1970年的秒数和微妙数,后面的tz是时区,一般不用 doubledifftime(time_ttime1,time_ttime2);//返回两个时间相差的秒数
3、时间与字符串的转换
需要包含的头文件如下
#include<iostream>
#include<time.h>
#include<stdlib.h>
#include<string.h>
1)unix/windows下时间转字符串参考代码
time_tt;//秒时间
tm*local;//本地时间
tm*gmt;//格林威治时间
charbuf[128]={0};
t=time(NULL);//或者time(&t);//获取目前秒时间
local=localtime(&t);//转为本地时间
strftime(buf,64,"%Y-%m-%d%H:%M:%S",local);
std::cout<<buf<<std::endl;
gmt=gmtime(&t);//转为格林威治时间
strftime(buf,64,"%Y-%m-%d%H:%M:%S",gmt);
std::cout<<buf<<std::endl;
2)unix字符串转时间参考代码
tmtm_;
time_tt_;
charbuf[128]={0};
strcpy(buf,"2012-01-0114:00:00");
strptime(buf,"%Y-%m-%d%H:%M:%S",&tm_);//将字符串转换为tm时间
tm_.tm_isdst=-1;
t_=mktime(&tm_);//将tm时间转换为秒时间
t_+=3600;//秒数加3600
tm_=*localtime(&t_);//输出时间
strftime(buf,64,"%Y-%m-%d%H:%M:%S",&tm_);
std::cout<<buf<<std::endl;
3)由于windows下没有strptime函数,所以可以使用scanf来格式化
time_tStringToDatetime(char*str)
{
tmtm_;
intyear,month,day,hour,minute,second;
sscanf(str,"%d-%d-%d%d:%d:%d",&year,&month,&day,&hour,&minute,&second);
tm_.tm_year=year-1900;
tm_.tm_mon=month-1;
tm_.tm_mday=day;
tm_.tm_hour=hour;
tm_.tm_min=minute;
tm_.tm_sec=second;
tm_.tm_isdst=0;
time_tt_=mktime(&tm_);//已经减了8个时区
returnt_;//秒时间
}
4)timeval获取时间示例:
structtimevalstart_time,over_time,consume_time;
gettimeofday(&over_time,NULL);//getthecurrenttime
start_time=over_time;
dosomething.....
gettimeofday(&over_time,NULL);
timeval_subtract(&consume_time,&start_time,&over_time);//计算时间差104./**
*计算两个时间的间隔,得到时间差
*@paramstructtimeval*resule返回计算出来的时间
*@paramstructtimeval*x需要计算的前一个时间
*@paramstructtimeval*y需要计算的后一个时间
*return-1failure,0success
**/
timeval_subtract(structtimeval*result,structtimeval*x,structtimeval*y)
{
if(x->tv_sec>y->tv_sec)
return-1;
if((x->tv_sec==y->tv_sec)&&(x->tv_usec>y->tv_usec))
return-1;
result->tv_sec=(y->tv_sec-x->tv_sec);
result->tv_usec=(y->tv_usec-x->tv_usec);
if(result->tv_usec<0)
{
result->tv_sec--;
result->tv_usec+=1000000;
}
return0;
}
4、关于localtime与localtime_r的区别
structtm*localtime(consttime_t*timep);
这个函数在返回的时候,返回的是一个指针,实际的内存是localtime内部通过static申请的静态内存,所以通过localtime调用后的返回值不及时使用的话,很有可能被其他线程localtime调用所覆盖掉
structtm*localtime_r(consttime_t*timep,structtm*result);
localtime_r则是由调用者在第二个参数传入一个structtmresult指针,该函数会把结果填充到这个传入的指针所指内存里面;成功的返回值指针也就是structtmresult。
多线程应用里面,应该用localtime_r函数替代localtime函数,因为localtime_r是线程安全的。
其他的时间函数,如asctime,asctime_r;ctime,ctime_r;gmtime,gmtime_r都是类似的,所以,<strong>时间函数的_r版本都是线程安全的。</strong>
</span>
以上这篇time_ttmtimeval和时间字符串的转换方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。