2012-07-22 31 views
0

我有一臺服務器從幾個設備收集信息。每臺設備與服務器位於不同的時區。 我想比較服務器的時間和設備發送服務器的時間。 1.設備如何獲取當前時間(包括時區)(這將發送到服務器)? 2.服務器如何比較當地時間與服務器給出的時間?比較來自不同時區的時間提升

+1

爲什麼不讓所有設備發送UTC時間? – 2012-07-22 16:06:58

+0

http://www.boost.org/doc/libs/1_50_0/doc/html/date_time/local_time.html#date_time.local_time.local_date_time – timrau 2012-07-22 16:10:26

+0

因爲最後我需要將utc時間轉換爲本地時間以便呈現在GUI – Shay 2012-07-22 17:40:04

回答

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_easternus_arizona)期間手動指定它。 DST支持包含在美國境內,但您可以使用DST utilities來處理其他國家的DST(不過您需要根據每個國家/地區定義DST規則)。

+0

什麼是local_adjust中的-5? 我如何知道機器的霧區? (它的Linux - 用戶可以配置它,所以我不能這樣做硬編碼) 當從UTC轉換回當地時間,如何提升知道什麼時區轉換? – Shay 2012-07-22 18:01:25

+0

這是時區信息(與UTC的小時差)。 – betabandido 2012-07-22 18:02:11

+0

您不能忽略夏令時。 – 2012-07-22 18:03:40