在boost中比较不同时区的时间
comparing times from different timezone in boost
我有一个服务器,它从几个设备收集信息。每个设备与服务器位于不同的时区。我希望将服务器的时间与设备发送服务器的时间进行比较。1.设备如何获取包括时区在内的当前时间(这将发送到服务器)?2.服务器如何将其本地时间与服务器提供的时间进行比较?
您可以使用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
)。美国包括夏令时支持,但您可以使用夏令时实用程序处理其他国家/地区的夏令时(不过,您需要根据每个国家/地区定义夏令时规则)。
相关文章:
- 如何在Arduino上将时间设置为中部时区?
- C++:将Unix时间转换为非本地时区
- 获取指定时区的日期和时间
- 如何以正确的时区从XTS索引中获取数字时间?
- 将带有时区的纪元时间转换为Excel格式的时间?
- 快速将带有时区的日期时间字符串转换为 UNIX 时间戳(C++)
- 使用C 在特定时区获取当前时间
- 生成包含分数和时区的完整 iso 日期时间表示字符串
- C :如何使用ICU通过给定的时区偏移来获得当前时间
- 使用Boost或STD在特定时区中获取当前的本地时间
- Poco 日期时间格式化程序 - 使用当前时区打印时间戳
- 无UTC时区中的正确时间总和
- C/C 获得时区偏移信息的时间戳
- Qt ISODate格式化的日期/时间,包括时区
- 将NTP时间设置为特定的时区
- Qt将时间和日期字符串转换为带时区的QDateTime
- 在boost中比较不同时区的时间
- 将日期和时间数字转换为time_t并指定时区
- 使用dst和boost将当前服务器时间转换为时区
- 为什么_localtime32和_gmtime32为非 GMT 时区返回相同的时间值