Linux下如果时间戳转换日期需要比较则采用的函数

如果时间戳转换日期需要比较则采用函数:struct tm *localtime_r(const time_t *timep, struct tm *result);

112511_xZzw_2375158.png

localtime函数的区别:localtime_r可重复调用,给用户的是一个临时结果buffer,const time_t *timep参数不同,返回结果也不同;localtime不管调用多少次,给用户的是一个static结果buffer,结果以最后一次调用为准;


例子:

void TestTimeFun_Localtime_r()
{
	time_t tm1;

	time(&tm1);//可以为任意时间戳

	while(1)
	{
		time_t tm2;
		tm2 = time(NULL);

		struct tm tm1p, tm2p;
		struct tm* tm1pp, *tm2pp;
		tm1pp = &tm1p;
		tm2pp = &tm2p;

		localtime_r(&tm1, tm1pp);
		localtime_r(&tm2, tm2pp);

		std::cout<<"tm1 min:"<<tm1p.tm_min<<std::endl;
		std::cout<<"tm2 min:"<<tm2p.tm_min<<std::endl;

		sleep(60);
	}

}

//结果:
//tm1 min:1
//tm2 min:1
//tm1 min:1
//tm2 min:2
//tm1 min:1
//tm2 min:3

void TestTimeFun_Localtime()
{
	time_t tm1;

	time(&tm1);

	while(1)
	{
		time_t tm2;
		tm2 = time(NULL);

		struct tm* tm1pp, *tm2pp;

		tm1pp = localtime(&tm1);
		tm2pp = localtime(&tm2);//以最后一次为准,如果是传tm1为参,则输出始终为起始值

		std::cout<<"tm1 min:"<<tm1pp->tm_min<<std::endl;
		std::cout<<"tm2 min:"<<tm2pp->tm_min<<std::endl;

		sleep(60);
	}

}

//结果:
//tm1 min:1
//tm2 min:1
//tm1 min:2
//tm2 min:2
//tm1 min:3
//tm2 min:3


转载于:https://my.oschina.net/u/2375158/blog/525707

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值