Alright so im experimenting with time.h, what im trying to do is create localtime and compare it to a set time(triggerTime). Simple sounding, but not really, im sure i didnt initialize my time_t trigTime properly, because when i call printf("At the Tone, the time will be: %s", ctime(&trigTime)); it returns Wen Dec 31 1969 rather then the date i set. What is the issue? Further more what how can i take triggerTime and initialize the struct *tm with it and later compare it to 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
is '2'
- the first character of the string. That's indeed a very small value, so it's no surprise you get a date very near to the time_t epoch. C isn't really typesafe, so it won't catch bugs like this. See samgak's comment for ideas how to do it properly.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.