linux关于时间处理

一、时间类型

1、time_t

在<time.h>中,typedef long time_t;  所以time_t是一个长整型。其值表示为从UTC(coordinated universal time)时间1970年1月1日00时00分00秒(也称为Linux系统的Epoch时间)到当前时刻的秒数。由于time_t类型长度的限制,它所表示的时间不能晚于2038年1月19日03时14分07秒(UTC)。为了能够表示更久远的时间,可用64位或更长的整形数来保存日历时间。

2、struct timeval 和struct timespec

#include<sys/time.h>


struct timeval {
    long    tv_sec;         /* seconds */
    long    tv_usec;        /* microseconds */
};
 
struct timespec {
    long    tv_sec;         /* seconds */
    long    tv_nsec;        /* nanoseconds */
};

3、struct tm

#ifndef _TM_DEFINED
struct tm {
          int tm_sec;       /* 秒 – 取值区间为[0,59] */
          int tm_min;       /* 分 - 取值区间为[0,59] */
          int tm_hour;      /* 时 - 取值区间为[0,23] */
          int tm_mday;      /* 一个月中的日期 - 取值区间为[1,31] */
          int tm_mon;       /* 月份(从一月开始,0代表一月) - 取值区间为[0,11] */
          int tm_year;      /* 年份,其值等于实际年份减去1900 */
          int tm_wday;      /* 星期 – 取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推 */
          int tm_yday;      /* 从每年的1月1日开始的天数 – 取值区间为[0,365],其中0代表1月1日,1                    代表1月2日,以此类推 */
          int tm_isdst;     /* 夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的进候,tm_isdst为0;不了解情况时,tm_isdst()为负。*/
          };
#define _TM_DEFINED
#endif

二、时间处理

1、time_t time(time_t *timer)

获取从1970年1月1好0时0分0秒到当前的秒数

,该函数通过返回值和传入的参数均可获得时间。

#include<stdio.h>
#include<time.h>

int main(int argc,char* argv[])
{
	time_t ret_seconds = 0;
	time_t par_seconds = 0;

	ret_seconds = time((time_t *)NULL);
	time(&par_seconds);
	
	printf("函数返回的秒数:%ld\n",ret_seconds);
	printf("通过参数返回的秒数:%ld\n",par_seconds);
	return 0;
}

执行结果为:

函数返回的秒数:1598754934
通过参数返回的秒数:1598754934

2、struct tm* localtime_r( const time_t* timer, struct tm* result )

loctime_r函数相似的还有loctime,但localtime函数不是线程安全的。如果在多线程里调用localtime函数,很可能会出现问题。struct tm *localtime(const time_t *clock);这个函数在返回的时候,返回的是一个指针,实际的内存是localtime内部通过static申请的静态内存,所以通过localtime调用后的返回值不及时使用的话,很有可能被其他线程localtime调用所覆盖掉,多线程应用里面,应该用localtime_r函数替代localtime函数,因为localtime_r是线程安全的。

#include<stdio.h>
#include<time.h>
/*
函数原型为struct tm *localtime_r(const time_t *timep, struct tm *result);
*/
int main(int argc,char* argv[])
{
	time_t ret_seconds = 0;
	struct tm ptm ;
	char content[64] = {0};
	
	ret_seconds = time((time_t *)NULL);
    localtime_r(&ret_seconds,&ptm);
 
    printf("当前时间为%04d-%02d-%02d-%02d:%02d:%02d\n", ptm.tm_year + 1900, ptm.tm_mon + 1,
        ptm.tm_mday, ptm.tm_hour, ptm.tm_min, ptm.tm_sec);
	snprintf(content,sizeof(content),"%04d-%02d-%02d-%02d:%02d:%02d", ptm.tm_year + 1900, ptm.tm_mon + 1,
        ptm.tm_mday, ptm.tm_hour, ptm.tm_min, ptm.tm_sec);
	printf("当前时间为-%s\n",content);
	return 0;
}

执行结果为:

当前时间为2020-08-30-11:44:15
当前时间为-2020-08-30-11:44:15


3、time_t mktime(struct tm *timeptr)

mktime()用来将参数timeptr所指的tm结构数据转换成从公元1970年1月1日0时0分0秒算起至今的UTC时间所经过的秒数。

#include<stdio.h>
#include<time.h>

int main(int argc,char* argv[])
{
	time_t ret_seconds = 0;
	struct tm ptm ;
	char content[64] = {0};
	
	ret_seconds = time((time_t *)NULL);
	printf("当前秒数%ld\n",ret_seconds);
    localtime_r(&ret_seconds,&ptm);
	ptm.tm_hour += 1;
	ret_seconds = mktime(&ptm);
	printf("1小时后的秒数%ld\n",ret_seconds);
	return 0;
}

执行结果:

当前秒数1598759782
1小时后的秒数1598763382
 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值