簡體   English   中英

C / C ++ time.h,錯誤時間打印輸出,時間比較

[英]C/C++ time.h, wrong time print out, comparison of times

好吧,我正在嘗試使用time.h,我試圖做的是創建本地時間並將其與設置的時間(triggerTime)進行比較。 聽起來很簡單,但不是真的,我不確定我沒有正確初始化time_t trigTime,因為當我調用printf(“在Tone時,時間將是:%s”,ctime(&trigTime)); 它返回的是Wen 1969年12月31日,而不是我設定的日期。 有什么問題 更進一步,我該如何使用triggerTime並使用它初始化結構* tm,然后再將其與localTimeInfo進行比較?

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

int main() 
{//start main

/****************CURRENT TIME**************************************************/
struct tm *localTimeInfo;//structure to be used to format Local Time
time_t localTime;//create a local time
time(&localTime);//Get the value of local time
localTimeInfo = localtime(&localTime);//store the value of localTime to localTimeInfo

printf("At the Tone, the time will be: %s", asctime(localTimeInfo));
/*****************END CURRENT TIME*********************************************/


/*****************TRIGGER TIME*************************************************/
char *triggerTime = "23 2:30:00 2015";

struct tm *triggerTimeInfo;
time_t trigTime = *triggerTime;

printf("At the Tone, the time will be: %s", ctime(&trigTime));



/*********************END TRIGGER TIME*****************************************/


 return 0;

 }//end main

*triggerTime'2' *triggerTime字符串的第一個字符。 這確實是一個很小的值,因此,您所獲得的日期非常接近time_t紀元也就不足為奇了。 C並不是真正的類型安全,因此它不會捕獲此類錯誤。 有關如何正確執行操作的想法,請參閱samgak的評論。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM