【发布时间】:2015-06-24 02:52:00
【问题描述】:
好吧,我正在尝试使用 time.h,我试图做的是创建本地时间并将其与设定的时间(触发时间)进行比较。听起来很简单,但不是真的,我确定我没有正确初始化我的 time_t trigTime,因为当我调用 printf("At the Tone, the time will be: %s", ctime(&trigTime));它返回 Wen Dec 31 1969 而不是我设置的日期。问题是什么?此外,我怎样才能获取 triggerTime 并用它初始化 struct *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
【问题讨论】:
-
有关如何将字符串转换为 time_t 的示例,请参阅此问题的答案:stackoverflow.com/questions/7101433/…