1、常用的时间记忆方式
1 ) time_t型。 这本质上是一个长整数,表示从1970-01-0100336003360000到当前计时时间的秒数。 如果需要更准确的时间,可以使用timeval精确到毫秒。
2 ) tm结构,它本质上是一个结构,其中包含每个时间场
struct tm {
int tm_sec;/* secondsaftertheminute-[ 0,59 ] * /
int tm_min;/* minutesafterthehour-[ 0,59 ] * /
int tm_hour;/* hourssincemidnight-[ 0,23 ] * /
int tm_mday;/* dayo fthe month-[ 1,31 ] * /
int tm_mon;/* monthssincejanuary-[ 0,11 ] * /
int tm_year;/*年单1900 * /
int tm_wday;/* dayssincesunday-[ 0,6 ] * /
int tm_yday;/* dayssincejanuary1- [ 0,365 ] * /
int tm_isdst; /* daylight savings time flag */
(;
这里,tm_year表示从1900年到现在的计时时间间隔是几年,在手动设定值的情况下,tm_isdst通常取-1的值。
3 ) time.h中struct timeval结构体的定义
struct timeval {
time_t tv_sec; /* seconds */
suseconds_t tv_usec; /* microseconds */
(;
2、常用时间函数
time_ttime(time_t*t ); 获取从1970年1月1日到现在的秒数
char*Asctime(conststructTM*TM ); //将结构内的信息转换为现实世界的时间,显示为字符串
char*ctime(consttime_t*timep ); 将timep转换为真实世界的时间,并以字符串形式显示。 与asctime不同的是,传递的参数的形式不同
struct TM * gmtime (const time _ t * timep ); struct tm结构指针,用于将time_t表示的时间转换为未转换为时区的UTC时间
struct TM * local time (const time _ t * timep ); 与gmtime类似,但经过时区转换的时间。
time_tMkTime(structTM*TM ); 将struct tm结构的时间转换为从1970年到现在的秒数
int gettime of day (struct timeval * TV,struct timezone *tz ); //返回当前距离1970年的秒数和微妙数,后面的tz是时区,一般不使用
doubledifftime(time_ttime1,time_t time2);//返回两个时间不同的秒数
3、时间和字符串转换
要包括的头文件如下
#包含
#包含
#包含
#包含
1 ) unix/windows上的时间转换字符串引用代码
time_t t; //秒时间
tm* local; //本地时间
tm* gmt; //格林威治标准时间
char buf[128]={0};
t=time (空; //或Time(T ); //获取当前的秒时间
local=localtime(t; //转移到当地时间
strftime(buf,64,' %Y-%m-%d %H:%M:%S ',local );
STD : coutbufstd : endl;
GMT=GMTime(t; //转换到格林威治标准时间
strftime(buf,64,' %Y-%m-%d %H:%M:%S ',gmt );
STD : coutbufstd : endl;
2 ) unix字符串旋转时间参考代码
tm tm_;
time_t t_;
char buf[128]={0};
strcpy(buf,' 2012-01-01 14: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_t StringToDatetime(char *str)
{
tm tm_;
int year, 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_t t_ = mktime(&tm_); //已经减了8个时区
return t_; //秒时间
}
4)timeval获取时间示例:
struct timeval start_time, over_time, consume_time;
gettimeofday(&over_time, NULL);//get the current time
start_time = over_time;
do something.....
gettimeofday(&over_time, NULL);
timeval_subtract(&consume_time, &start_time, &over_time);//计算时间差104./**
* 计算两个时间的间隔,得到时间差
* @param struct timeval* resule 返回计算出来的时间
* @param struct timeval* x 需要计算的前一个时间
* @param struct timeval* y 需要计算的后一个时间
* return -1 failure ,0 success
**/
timeval_subtract(struct timeval* result, struct timeval* x, struct timeval* 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;
}
return 0;
}
4、关于localtime与localtime_r的区别
struct tm *localtime(const time_t *timep);
这个函数在返回的时候,返回的是一个指针,实际的内存是localtime内部通过static申请的静态内存,所以通过localtime调用后的返回值不及时使用的话,很有可能被其他线程localtime调用所覆盖掉
struct tm *localtime_r(const time_t *timep, struct tm *result);
localtime_r则是由调用者在第二个参数传入一个struct tm result指针,该函数会把结果填充到这个传入的指针所指内存里面;成功的返回值指针也就是struct tm result。
多线程应用里面,应该用localtime_r函数替代localtime函数,因为localtime_r是线程安全的。
其他的时间函数,如asctime,asctime_r;ctime,ctime_r;gmtime,gmtime_r都是类似的,所以,时间函数的 _r 版本都是线程安全的。
以上这篇time_t tm timeval 和 时间字符串的转换方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。