指定された時間より 24 時間少ないコードを作成しようとすると、mktime()
一貫性のない出力が表示されます。これを次のように計算しcurrent_time(GMT) - 86400
ます。これは正しい値を返すはずです。入力時間に基づいて計算するだけです。以前mktime()
は時刻を変更して GMT 時刻を取得し、通常の計算を行っていました。以下にコードを含めました。
#include <stdio.h>
#include <time.h>
int main()
{
time_t currentTime, tempTime;
struct tm *localTime;
time(¤tTime);
//localTime = localtime(¤tTime);
localTime = gmtime(¤tTime); //get the time in GMT as we are in PDT
printf("Time %2d:%02d\n", (localTime->tm_hour)%24, localTime->tm_min);
localTime->tm_hour = 19; // Set the time to 19:00 GMT
localTime->tm_min = 0;
localTime->tm_sec = 0;
tempTime = mktime(localTime);
//tempTime = mktime(localTime) - timezone;
printf("Current time is %ld and day before time is %ld\n", currentTime, (currentTime - 86400));
printf("Current timezone is %ld \n", timezone);
printf("New time is %ld and day before time is %ld\n",tempTime, (tempTime - 86400));
}
しかし、出力を確認すると、 call の呼び出し後に間違った結果が返されていますmktime()
。以下は、上記のプログラムの出力です。
$ ./a.out
Time 11:51
Current time is 1341229916 and day before time is 1341143516
New time is 1341284400 and day before time is 1341198000
$ ./print_gmt 1341229916
Mon Jul 2 11:51:56 2012
$ ./print_gmt 1341143516
Sun Jul 1 11:51:56 2012
$ ./print_gmt 1341284400
Tue Jul 3 03:00:00 2012
$ ./print_gmt 1341198000
Mon Jul 2 03:00:00 2012
$ date
Mon Jul 2 04:52:46 PDT 2012
ここで、タイムゾーンを減算する行 (time.h に存在) のコメントを外すと、出力は期待どおりになります。以下は、上記のプログラムのタイムゾーンの値です
$ ./a.out
. . .
Current timezone is 28800
. . .
mktime()
では、マニュアルページではタイムゾーンの調整について言及されていないのに、なぜこのような一貫性のない動作があるのでしょうか。そのような変換を行う際に何か欠けているものはありますか?
前もって感謝します。