C/C++判断传入的UTC时间是否在今天
在项目中经常会显示一个时间,如果这个时间在今日内就显示为时分秒,否则显示为年月日。
这里先给出一个正确的版本:
#include <iostream>
#include <time.h> using namespace std; bool IsInToday(long utc_time){ time_t timeCur = time(NULL);
struct tm curDate = *localtime(&timeCur); struct tm argsDate = *localtime(&utc_time); if (argsDate.tm_year == curDate.tm_year &&
argsDate.tm_mon == curDate.tm_mon &&
argsDate.tm_mday == curDate.tm_mday){
return true;
} return false;
} std::string GetStringDate(long utc_time){ struct tm *local = localtime(&utc_time);
char strTime[50];
sprintf(strTime,"%*.*d年%*.*d月%*.*d日",
4,4,local->tm_year+1900,
2,2,local->tm_mon+1,
2,2,local->tm_mday); return strTime;
} std::string GetStringTime(long utc_time){ struct tm local = *localtime(&utc_time);
char strTime[50];
sprintf(strTime,"%*.*d:%*.*d:%*.*d",
2,2,local.tm_hour,
2,2,local.tm_min,
2,2,local.tm_sec);
return strTime;
} void ShowTime(long utc_time){ if (IsInToday(utc_time)){
printf("%s\n",GetStringTime(utc_time).c_str());
}else{
printf("%s\n",GetStringDate(utc_time).c_str());
} } int main(){ ShowTime(1389998142);
ShowTime(time(NULL)); return 0; }
在函数中struct tm *localtime(const time_t *);中返回的指针指向的是一个全局变量,如果把函数bool IsInToday(long utc_time);写成样,会有什么问题呢?
bool IsInToday(long utc_time){
time_t timeCur = time(NULL);
struct tm* curDate = localtime(&timeCur);
struct tm* argsDate = localtime(&utc_time);
if (argsDate->tm_year == curDate->tm_year &&
argsDate->tm_mon == curDate->tm_mon &&
argsDate->tm_mday == curDate->tm_mday){
return true;
}
return false;
}
这里把curDate和argsDate声明成了指针。运行程序就会发现,这个函数返回的总是ture,为什么呢?
因为在
struct
tm
* curDate =
localtime
(&timeCur);中返回的指针指向的是一个全局变量,即curDate也指向这个全局变量。
在
struct
tm
* argsDate =
localtime
(&utc_time);中返回额指针也指向的这个全局变量,所以argsDate和cur指向的是同一个全局变量,他们的内存区域是同一块。
在第二次调用localtime()时,修改了全局变量的值,curDate也随之改变,因此curDate == argsDate;所以返回的结果衡等于true。
C/C++判断传入的UTC时间是否在今天的更多相关文章
- C#实现多级子目录Zip压缩解压实例 NET4.6下的UTC时间转换 [译]ASP.NET Core Web API 中使用Oracle数据库和Dapper看这篇就够了 asp.Net Core免费开源分布式异常日志收集框架Exceptionless安装配置以及简单使用图文教程 asp.net core异步进行新增操作并且需要判断某些字段是否重复的三种解决方案 .NET Core开发日志
C#实现多级子目录Zip压缩解压实例 参考 https://blog.csdn.net/lki_suidongdong/article/details/20942977 重点: 实现多级子目录的压缩, ...
- Linux与Windows中的UTC时间
Linux与Windows中的UTC时间 先介绍几个术语 UTC 协调世界时,又称世界标准时间或世界协调时间,简称UTC(从英文“Coordinated Universal Time”/法文“Temp ...
- 关于UTC时间和本地时间
收藏了个类Publics 可以实现本地时间和UTC时间的转换 UCT时间=本地时间-8 本地时间比UTC时间快8小时 element-ui的日期选择器上 选择的时间显示的是本地时间 但实 ...
- QDateTime 本地时间和UTC时间转换问题
先说一下UTC,搜索360百科: 协调世界时,又称世界统一时间.世界标准时间.国际协调时间,简称UTC,是以原子时秒长为基础,在时刻上尽量接近于世界时的一种时间计量系统.1979年12月3日在内瓦举行 ...
- 关于时间:UTC时间、GMT时间、本地时间、Unix时间戳
1.UTC时间 与 GMT时间我们可以认为格林威治时间就是时间协调时间(GMT=UTC),格林威治时间和UTC时间均用秒数来计算的. 2.UTC时间 与 本地时UTC + 时区差 = 本地时间时区差东 ...
- Python: 处理mongodb文档,怎么让UTC时间转换为本地时间?
存储数据到MongoDB数据库时,一般我们会加一个更新数据的时间update_time.这时在python代码中 会用到datetime模块以便获取当前系统时间,但是存入到MongoDB数据库时,存储 ...
- Js 日期转换函数(UTC时间转换及日期想加减)
IOS上Js日期转换中new Date("yyyy-mm-dd")不能正常工作,必须使用new Date("yyyy/MM/dd"); 日期相加减: Date. ...
- JS将指定的时间戳转为UTC时间
Js中获取时间戳可用var dayMiliseconds = parseInt(new Date().valueOf());Js的时间戳单位为毫秒(1s = 1000 ms),下面是一个将制定的格式转 ...
- CloudStack中云主机的UTC时间转为本地时间
CloudStack项目中使用的时间是UTC时间,具体什么是UTC时间大家可以百度,但是我们需要的时间是正常的时间,所以在国泰君安开发测试云中,同步资源管理中虚拟机的同步管理,需要对虚拟机的时间格式化 ...
随机推荐
- ADC分类及参数
ADC分类 直接转换模拟数字转换器(Direct-conversion ADC),或称Flash模拟数字转换器(Flash ADC) 循续渐近式模拟数字转换器(Successive approxima ...
- arcgis pro行列转换
行转列 列转行
- [Algorithm] Calculate Pow(x,n) using recursion
Asking you to implement the Math.pow method The navie implemenation can be: // O(N) const pow1 = (x, ...
- 求标准分sql
if object_id('tempdb..#tempTable') is not null Begin drop table #tempTable End [校区],[学年],[考试年级],[考试类 ...
- SVN的Branch和Tag管理
dev:开发主线 branch: 部分特殊客户的定制化版本 tag: 主线的某个发布版本 release: 主线的里程碑式的发布版本(相比上一里程碑版本,改动非常大,并且当前已经很稳定的) 你可以在b ...
- 使用robot_pose_ekf对传感器信息融合
robot_pose_ekf是ROS Navigation stack中的一个包,通过扩展卡尔曼滤波器对imu.里程计odom.视觉里程计vo的数据进行融合,来估计平面移动机器人的真实位置姿态,输出o ...
- excel随机函数
=D7+RAND()*(8000-4250) 含义: 1.在D7数值的基础上,随机加一个数值,该数值的随机范围为4250——8000. 2.注意8000和4250要反着写
- 在mysql中如何写注释语句
//在mysql中如何写注释语句 mysql; # 这个注释直到该行结束 mysql; -- 这个注释直到该行结束 mysql ; mysql+ /* 这是一个 多行注释的形式 */ ;
- fork failed because of Out Of Memory
Maybe virtual memory over commit is prevented in your system. If it is prevented, then the virtual m ...
- qeephp 记录下
百度百科: https://baike.baidu.com/item/qeephp/8328612?fr=aladdin 官方地址: http://www.qeephp.cn/app/index.ph ...