2

GMT 時刻と現在時刻の時差を計算したかったのです。このために、mktime を使用して tm 時間 (GMT) を time_t 形式に変換しています。time() APIを使用した現在の時刻。

struct tm = x;  time_t t1, t2; 
time(&t1);
/* here x  will get in GMT format */
t2 = mktime(&x);
sec = difftime(t2 , t1);

これで同じタイムゾーンを作成するために、mktime() は現地時間への変換を処理しますか? sec = difftime(t2 , gmtime(&t1); または、明示的に追加する必要がありますか

4

1 に答える 1

1

はいmktime、現地時間に変換します。男性を読んでください:

http://www.mkssoftware.com/docs/man3/mktime.3.asp

mktime() : convert local time to seconds since the Epoch

EDIT:2つの日付間の時差を計算するには、これを使用できます

    time_t t1, t2;
    struct tm my_target_date;

    /* Construct your date */
    my_target_date.tm_sec = 0;
    my_target_date.tm_min = 0;
    my_target_date.tm_hour = 0;
    my_target_date.tm_mday = 20;
    my_target_date.tm_mon = 7;
    my_target_date.tm_year = 112; /* Date today */
    t1 = mktime (&my_target_date);
    t2 = time (NULL);
    printf ("Number of days since target date : %ld\n", (t2 - t1) / 86400); /* 1 day = 86400 sec, use 3600 if you want hours */
于 2012-08-21T08:32:47.103 に答える