1年の期間(in)が必要なプログラムを作成していますtime_t
。
他の方法でtime_t
は、DD / MM /YYYY+期間= time_t
DD/ MM / YYYY+1の
したがって、必ずしも365日とは限りません(2012年2月29日は2013年2月28日となります)
これが私が持ってきたアルゴリズムです:
if YEAR is leap than
if we are before the 29th feb' than return 365+1 days
else if we are the 29th feb' than return 365-1 days
else return 365 days
else if YEAR+1 is leap than
if we are before or the 28th feb' than return 365 days
else return 365+1 days
else return 365 days
ここでは、1日は60 * 60*24秒です
このアルゴリズムは機能しているようです。しかし、これらすべての条件と2つの可能な戻り値だけを使用せずにこれを行う別の方法があるのか、それとも物事を最適化するための「トリック」があるのか疑問に思いました。
tm_year
私はこのようなものからインクリメントしようとしましたstruct tm
:
// t is the input time_t
struct tm Tm (*localtime(&t));
if (Tm.tm_mon == 2 && Tm.tm_mday == 29) --Tm.tm_mday;
++Tm.tm_year;
return mktime(&Tm) - t;
しかし、結果は私が望むものではありません、私は-1時間、または-25を取得しました...
1年が365*24 * 60*60ではないからだと思います。