複数のデバイスから情報を収集するサーバーがあります。各デバイスは、サーバーとは異なるタイム ゾーンにあります。サーバーの時刻と、デバイスがサーバーを送信する時刻を比較したいと考えています。1. デバイスはどのようにしてタイムゾーンを含む現在の時刻を取得できますか (これはサーバーに送信されます)? 2. サーバーはどのようにしてローカル時刻をサーバーから提供された時刻と比較できますか?
1091 次
1 に答える
0
タイムゾーンを処理するために完全に準備されたBoost date_timeライブラリを使用できます。コードは次のようになります。
// The device will collect the time and send it to the server
// along its timezone information (e.g., US East Coast)
ptime curr_time(second_clock::local_time());
// The server will first convert that time to UTC using the timezone information.
// Alternatively, the server may just send UTC time.
typedef boost::date_time::local_adjustor<ptime, -5, us_dst> us_eastern;
ptime utc_time = us_eastern::local_to_utc(curr_time);
// Finally the server will convert UTC time to local time (e.g., US Arizona).
typedef boost::date_time::local_adjustor<ptime, -7, no_dst> us_arizona;
ptime local_time = us_arizona::utc_to_local(utc_time);
std::cout << to_simple_string(local_time) << std::endl;
DSTを処理するには、ローカル アジャスターの定義中 (us_eastern
およびus_arizona
サンプル コード内)に手動で指定する必要があります。米国では DST のサポートが含まれていますが、DST ユーティリティを使用して他の国の DST を処理することもできます (ただし、国ごとに DST ルールを定義する必要があります)。
于 2012-07-22T17:57:35.687 に答える