C++ 比较boost中不同时区的时间

C++ 比较boost中不同时区的时间,c++,boost,timezone,C++,Boost,Timezone,我有一个服务器,它从多个设备收集信息。每个设备与服务器位于不同的时区。 我希望将服务器的时间与设备发送服务器的时间进行比较。 1.设备如何获取其当前时间(包括时区)(将发送到服务器)? 2.服务器如何将其本地时间与服务器给定的时间进行比较?您可以使用完全准备好处理时区的库。您的代码可能类似于: // The device will collect the time and send it to the server // along its timezone information (e.g.,

我有一个服务器,它从多个设备收集信息。每个设备与服务器位于不同的时区。 我希望将服务器的时间与设备发送服务器的时间进行比较。 1.设备如何获取其当前时间(包括时区)(将发送到服务器)? 2.服务器如何将其本地时间与服务器给定的时间进行比较?

您可以使用完全准备好处理时区的库。您的代码可能类似于:

// 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;
//设备将收集时间并将其发送到服务器
//沿其时区信息(例如,美国东海岸)
ptime curr_time(秒时钟::本地时间());
//服务器将首先使用时区信息将该时间转换为UTC。
//或者,服务器可能只发送UTC时间。
typedef boost::日期\时间::本地\调整器美国\东部;
ptime utc时间=美国东部时间::当地时间到utc时间(当前时间);
//最后,服务器将UTC时间转换为本地时间(例如,美国亚利桑那州)。
typedef boost::日期\时间::本地\调整器us \亚利桑那州;
ptime local_time=美国亚利桑那州::utc_至_local(utc_时间);

std::为什么不让所有设备发送UTC时间?因为最后我需要将UTC时间转换为本地时间,以便在GUI中显示本地调整中的-5是什么?我如何知道机器的雾凇区?(它的linux-用户可以配置它,所以我不能硬编码)当从UTC转换回本地时间时,boost如何知道转换到哪个时区?你不能忽略夏令时。@Shay似乎没有一种可移植的方法来获取时区(见此。例如,时区可以在安装过程中进行配置。@Shay I更新了答案,以显示更好的DST处理方式。正如您所看到的,它不会是100%自动的(尤其是如果您希望支持我们之外的时区)。