#include <stdio.h>
#include <time.h>
time_t get_caltime(char *tstr);
main()
{
time_t cal_time, now_time, inter, prev;
char buff[100];
int day, hour, min, sec;
struct tm
*tm_now;
printf("YYYYMMDDHHMMSS : ");
scanf("%s", buff);
cal_time = get_caltime(buff);
if(cal_time == -1){
printf("変換失敗\n");
return;
}
now_time = time(NULL);
if(cal_time <= now_time){
printf("入力した日時が不適切です.\n");
return;
}
printf("\033[2J");
tm_now = localtime(&cal_time);
printf("%4d年%2d月%2d日 %2d時%2d分%2d秒まで\n",
tm_now->tm_year+1900, tm_now->tm_mon+1, tm_now->tm_mday,
tm_now->tm_hour, tm_now->tm_min, tm_now->tm_sec);
prev = -1;
for(;;){
now_time = time(NULL);
if(prev == now_time)
continue;
prev = now_time;
inter = cal_time - now_time;
if(inter < 0)
break;
sec = inter % 60;
inter /= 60;
min = inter % 60;
inter /= 60;
hour = inter % 24;
day = inter / 24;
printf("あと%d日と%2d時間%2d分%2d秒\n", day, hour, min, sec);
}
}
time_t get_caltime(char *tstr)
{
char buf[20];
time_t cal_time;
struct tm
work_tm;
if(strlen(tstr) != 14)
return(-1);
strncpy(buf, tstr, 4);
buf[4] = '\0';
work_tm.tm_year = atoi(buf) - 1900;
strncpy(buf, tstr+4, 2);
buf[2] = '\0';
work_tm.tm_mon = atoi(buf) - 1;
strncpy(buf, tstr+4+2, 2);
work_tm.tm_mday = atoi(buf);
strncpy(buf, tstr+4+2+2, 2);
work_tm.tm_hour = atoi(buf);
strncpy(buf, tstr+4+2+2+2, 2);
work_tm.tm_min = atoi(buf);
strncpy(buf, tstr+4+2+2+2+2, 2);
work_tm.tm_sec = atoi(buf);
work_tm.tm_isdst = -1;
if((cal_time = mktime(&work_tm)) == -1){
return(-1);
}
return(cal_time);
}
|