C/C++判斷傳入的UTC時(shí)間是否當(dāng)天的實(shí)現(xiàn)方法
這里先給出一個(gè)正確的版本:
#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;
}
在函數(shù)中struct tm *localtime(const time_t *);中返回的指針指向的是一個(gè)全局變量,如果把函數(shù)bool IsInToday(long utc_time);寫成樣,會(huì)有什么問(wèn)題呢?
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聲明成了指針。運(yùn)行程序就會(huì)發(fā)現(xiàn),這個(gè)函數(shù)返回的總是ture,為什么呢?
因?yàn)樵趕truct tm* curDate = localtime(&timeCur);中返回的指針指向的是一個(gè)全局變量,即curDate也指向這個(gè)全局變量。
在struct tm* argsDate = localtime(&utc_time);中返回額指針也指向的這個(gè)全局變量,所以argsDate和cur指向的是同一個(gè)全局變量,他們的內(nèi)存區(qū)域是同一塊。
在第二次調(diào)用localtime()時(shí),修改了全局變量的值,curDate也隨之改變,因此curDate == argsDate;所以返回的結(jié)果衡等于true。
相關(guān)文章
C++之實(shí)現(xiàn)快速清空vector以及釋放vector內(nèi)存
這篇文章主要介紹了C++之實(shí)現(xiàn)快速清空vector以及釋放vector內(nèi)存方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
Qt圖形圖像開發(fā)之曲線圖表庫(kù)QChart編譯安裝詳細(xì)方法與使用實(shí)例
這篇文章主要介紹了Qt圖形圖像開發(fā)之曲線圖表庫(kù)QChart編譯安裝詳細(xì)方法與使用實(shí)例,需要的朋友可以參考下2020-03-03
基于C語(yǔ)言編寫一個(gè)簡(jiǎn)單的抽卡小游戲
這篇文章主要為大家介紹了如何利用C語(yǔ)言實(shí)現(xiàn)原神抽卡的小游戲,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-04-04
C++實(shí)現(xiàn)LeetCode(5.最長(zhǎng)回文子串)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(5.最長(zhǎng)回文子串),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
C語(yǔ)言實(shí)現(xiàn)猜數(shù)字游戲
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)猜數(shù)字游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11
構(gòu)造函數(shù)定義為private或者protected的好處
從語(yǔ)法上來(lái)講,一個(gè)函數(shù)被聲明為protected或者private,那么這個(gè)函數(shù)就不能從“外部”直接被調(diào)用了。對(duì)于protected的函數(shù),子類的“內(nèi)部”的其他函數(shù)可以調(diào)用之。而對(duì)于private的函數(shù),只能被本類“內(nèi)部”的其他函數(shù)說(shuō)調(diào)用2013-10-10
關(guān)于C語(yǔ)言函數(shù)strstr()的分析以及實(shí)現(xiàn)
以下是對(duì)C語(yǔ)言中strstr()函數(shù)的使用進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以參考下2013-07-07

