質問

私はここでスニペットを持っています: 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