2

これが私の現在の状況です:

  • 2 つの tm 構造体があり、どちらも現在の時刻に設定されています
  • 構造体の 1 つで時間を変更します。
  • 変更は魔法のように他の構造体で発生しています....
  • これが発生しないようにするにはどうすればよいですか? 2 つの異なる時間 (現在の時間と未来の時間) の秒数を比較して知る必要があります。これを判断するために difftime と mktime を使用しています。技術的には 2 つの tm 構造体が必要ないことは認識していますが (もう 1 つの構造体は、生の時間が読み込まれた time_t である可能性があります)、なぜこれが発生するのかを理解したいと思っています。

void Tracker::monitor(char* バッファ){

// time handling
time_t systemtime, scheduletime, currenttime;
struct tm * dispatchtime;
struct tm * uiuctime;
double remainingtime;


// let's get two structs operating with current time
dispatchtime = dispatchtime_tm();
uiuctime = uiuctime_tm();

// set the scheduled parameters
dispatchtime->tm_hour = 5;
dispatchtime->tm_min = 05;
dispatchtime->tm_sec = 14;

uiuctime->tm_hour = 0;

    // both of these will now print the same time! (0:05:14)
    // what's linking them??

// print the scheduled time
printf ("Current Time :  %2d:%02d:%02d\n", uiuctime->tm_hour, uiuctime->tm_min, uiuctime->tm_sec);
printf ("Scheduled Time :  %2d:%02d:%02d\n", dispatchtime->tm_hour, dispatchtime->tm_min, dispatchtime->tm_sec);

}

struct tm* Tracker::uiuctime_tm(){
    time_t uiucTime;
    struct tm *ts_uiuc;

    // give currentTime the current time
    time(&uiucTime);

    // change the time zone to UIUC
    putenv("TZ=CST6CDT");
    tzset();

    // get the localtime for the tz selected
    ts_uiuc = localtime(&uiucTime);

    // set back the current timezone
    unsetenv("TZ");
    tzset();

    // set back our results
    return ts_uiuc;
}

struct tm* Tracker::dispatchtime_tm(){
    time_t currentTime;
    struct tm *ts_dispatch;

    // give currentTime the current time
    time(&currentTime);

    // get the localtime for the tz selected
    ts_dispatch = localtime(&currentTime);

    // set back our results
    return ts_dispatch;
}
4

3 に答える 3

3

http://www.cplusplus.com/reference/clibrary/ctime/localtime/

この構造は静的に割り当てられ、関数gmtimeおよびlocaltimeによって共有されます。これらの関数のいずれかが呼び出されるたびに、この構造の内容が上書きされます。

構造体から値をコピーする必要があります。関数は値によってtm構造体を返すことができ、メインプログラムの関数を逆参照することができます。

于 2010-06-15T16:26:48.363 に答える
3

あなたはこれをしなければなりません:

struct tm* temp_tm;
struct tm dispatchtime; // No longer a pointer
struct tm uiuctime;     // No longer a pointer

temp_tm = dispatchtime_tm();
dispatchtime = *temp_tm; // Member to member copy

temp_tm = uiuctime_tm();
uiuctime = *temp_tm; // Member to member copy

このようにして、tm構造体のローカルコピーを保持します。この構造体は標準ライブラリの内部で割り当てられ、への各呼び出しlocaltimeは同じメモリアドレスを指します!

于 2010-06-15T16:31:16.667 に答える
1

実際には、2 つの異なる tm 構造体はまったくありません。2 つの tm 構造体ポインターがあり、両方とも localtime によって返される同じ静的構造体を指しています。したがって、一方を変更すると他方が影響を受けるように見えますが、実際には、1 つの構造体が 2 つの異なるポインターを持っているだけです。

localtimeこれを解決する最も安全な方法は、 の静的構造に依存するのではなくlocaltime_r、独自の tm 構造ポインタを渡す必要があるものを使用することです。例えば:

void Tracker::uiuctime_tm(struct tm* out){
    time_t uiucTime;

    // give currentTime the current time
    time(&uiucTime);

    // change the time zone to UIUC
    putenv("TZ=CST6CDT");
    tzset();

    // get the localtime for the tz selected, and set back the result into the output parameter.
    localtime_r(&uiucTime, out);

    // set back the current timezone
    unsetenv("TZ");
    tzset();
}

struct tm uiuctime;
uiuctime_tm(&uiuctime);
于 2010-06-15T16:51:21.127 に答える