如何使用C++和Boost查找纽约当地时间
How to find local time in New York, using C++ and Boost?
我想知道,即使我在位于不同国家/地区的服务器上运行代码,如何使用Boost和C++准确计算出纽约的当地时间?
我尝试过的
我试过看Boost C++的例子,但似乎什么都找不到。
对于OP,以及Matt Johnson,他希望查看正在使用的tz数据库。
#include "boost/date_time/posix_time/posix_time.hpp"
using namespace boost::posix_time;
using namespace boost::gregorian;
#include "boost/date_time/local_time/local_time.hpp"
// form an empty database
boost::local_time::tz_database tz_database;
// load the time zone database which comes with boost
tz_database.load_from_file( "../../boost/libs/date_time/data/date_time_zonespec.csv" );
// obtain a specific time zone from the database
boost::local_time::time_zone_ptr tzNewYork;
tzNewYork = tz_database.time_zone_from_region("America/New_York");
// example universal/local conversion (from boost example code)
// ptime now = boost::posix_time::microsec_clock::universal_time();
ptime now(date(2004,Nov,5), hours(10));
boost::local_time::local_date_time ny(now, tzNewYork );
ny.utc_time(); // 10am 2004-Nov-5
ny.local_time(); // 5am 2004-Nov-5
首先,您需要获得UTC时间:您可以使用boost::posix_time::second_clock::universal_time()
。
从你刚才给出的链接:
//eastern timezone is utc-5
typedef boost::date_time::local_adjustor<ptime, -5, us_dst> us_eastern;
// ...
ptime t3 = us_eastern::utc_to_local(t2);//back should be the same
std::cout << to_simple_string(t2) << " UTC is "
<< to_simple_string(t3) << " New York time "
<< "nn";
要将其转换为纽约时间,只需为您的时区定义一个local_ajustor,并从中调用utc_to_local
。
相关文章:
- 正在查找文档以获得PS4平台的C++中的设备信息
- 在C++中查找文件
- 模板元程序查找相似的连续类型名称
- 在UNIX系统中使用DIR查找文件的字节大小
- 查找最接近的大于当前数字的数字的索引
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 查找后更改类变量
- 使用正则表达式regex_search在字符串中查找字符串
- 使用gcc从静态链接的文件中查找可选符号
- 在C++中查找范围的长度
- 算法问题:查找从堆栈中弹出的所有序列
- 在Windows中查找扬声器输出的当前音量级别
- 如何在C++中使用X509证书模在令牌中查找私钥
- 使用.find函数在c++中查找字符和另一个字符之间的大小
- 在 Windows 上,是否可以让 dll 在不使用 PATH 环境变量的情况下在另一个文件夹中查找依赖项?
- 在 for 循环中查找问题时遇到困难
- 如何在文件中查找字节序列
- 试图创建一个多线程程序来查找0-100000000之间的总素数
- 使用堆查找第K个最大元素的时间复杂性
- 如何使用C++和Boost查找纽约当地时间