문제

여기에 스 니펫이 있습니다. http://ideone.com/fywv3j

여기에도 게시 :

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

int gtime_to = 20131231;

int main() {
  time_t current_time, licence_expiry_time;
  struct tm licence_expiry_time_struct;
  memset(&licence_expiry_time_struct, 0, sizeof(struct tm));
  licence_expiry_time_struct.tm_mday = gtime_to % 100;                  // get the day
  licence_expiry_time_struct.tm_mon = ((gtime_to % 10000) / 100) - 1;   // get the month
  licence_expiry_time_struct.tm_year = gtime_to / 10000;                // get the year
  licence_expiry_time_struct.tm_hour = 0;
  licence_expiry_time_struct.tm_min = 0;
  licence_expiry_time_struct.tm_sec = 0;
  licence_expiry_time_struct.tm_wday = 0;
  licence_expiry_time_struct.tm_yday = 0;
  licence_expiry_time_struct.tm_isdst = -1;
  licence_expiry_time = mktime(&licence_expiry_time_struct);
  time(&current_time); 
  printf("date = %d, month = %d, year = %d\n", licence_expiry_time_struct.tm_mday, licence_expiry_time_struct.tm_mon, licence_expiry_time_struct.tm_year);
  printf("cur = %d, expiry = %d, diff = %d", current_time, licence_expiry_time, difftime(licence_expiry_time, current_time));
  return 0;
}

출력 mktime () ~이다 -1 내가 시도한 모든 가능한 가치에 대해.

이것은 라인입니다cur = 1386084345, expiry = -1, diff = -25165824값의 값 만료 그렇지 않아야합니다 -1. 누구든지 내가 어디에서 잘못하고 있는지 말해 줄 수 있습니까?

도움이 되었습니까?

해결책

당신의 해는 1900 년 이래로 상쇄 될 것입니다. 그것은 아마도 아마도 범인 일 것입니다.

노력하다:

licence_expiry_time_struct.tm_year = gtime_to / 10000 - 1900; 

일반적으로 사용할 때 mktime() 유효한 것으로 시작하는 것이 좋습니다 tm 처음에 구조 (즉, 전화로 localtime()) 그런 다음 필드를 변경합니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top