使用C 在特定时区获取当前时间
get current time in specific timezone using c++
我的代码应在linux&还有风力。我想在Yyyy-MM-DD HH24:MI:SS中获得当前时间。默认时区为UTC 08,其中我的系统可以位于任何时区。
如果您可以帮助我使用C 代码(我没有C 11,14编译器(
我看到了一种解决方案 - 利用时间在UTC中获得当前时间,然后操纵目标时区的TZ环境变量。然后使用localtime_r转换为时区的本地时间。
,但不确定如何使用适用于Windows和Linux的C 实现此目标。
我建议您查看boost库boost/date_time/posix_time/posix_time.hpp
。
从那里您可以简单地获取当前时间,例如:
boost::posix_time::ptime curr_time = boost::posix_time::microsec_clock::local_time();
它具有根据需要将其转换为字符串的方法:
std::string curr_time_str = to_simple_string(curr_time);
然后返回到Ptime对象:
curr_time = boost::posix_time::time_from_string(curr_time_str);
等。
http://www.boost.org/doc/libs/1_61_0/doc/html/date_time/posix_time/posix_time.html
应该在大多数平台上工作:
int main(int argc, const char * argv[])
{
time_t ts = 0;
struct tm t;
char buf[16];
::localtime_r(&ts, &t);
::strftime(buf, sizeof(buf), "%z", &t);
std::cout << "Current timezone: " << buf << std::endl;
::strftime(buf, sizeof(buf), "%Z", &t);
std::cout << "Current timezone: " << buf << std::end;
...
}
相关文章:
- C++为构建时间获取QDateTime的可靠方法
- c++ 11 如何获取系统时间?
- 获取 QInputEvent 在 Qt 4.8 中被放入 QEventLoop 队列时的时间戳
- 如何以毫秒为单位获取开始时间和 now() 之间的毫秒差异(以 C++为单位?
- 如何从远程 SFTP 服务器获取 HH-MM-SS 时间戳格式的文件列表
- 如何在 c++ stl 中获取列表中被推回的元素的地址,在常量时间内?
- 如何在 for 循环中的特定时间内获取要打印的额外值
- 如何以" Safe "的方式获取 C++ 中的当前时间
- C++程序以获取当前时间
- 为什么在C++中获取日期和/或时间如此复杂?
- 在 c++ 中获取 X 毫秒前的时间戳
- 如何获取两个时间点之间的时间
- 使用C++中的put_time以毫秒精度获取当前时间
- 获取合成时间C++
- 注册表-获取值(而不是键)上次更改的时间和键创建日期
- 如何在给定时钟偏移的情况下正确获取时间
- 从具有不同曝光时间的图像中获取HDR图像
- 如何在Mac OSX中获取真实的日历微秒时间(自1970年以来的纪元)?
- 通过修改时间获取目录中的文件
- 有没有办法通过程序在特定时间获取文件部分加载内容的大小C++