且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在C ++中获取Linux上的当前时区

更新时间:2023-01-27 13:30:03

您需要使用tzfile和 tzset [ ^ ]查找名称的函数。
You need to use the tzfile and tzset[^] functions to find the name.


我认为没有办法在Linux系统库中得到它,因为底层库只使用缩写形式,而长形式可能无法在任何地方使用。



但是,ICU库确实有必要的方法等等。它默认安装在大多数现代Linux发行版上(您需要libicu-dev或等效编译): http:// userguide .icu-project.org / datetime / timezone [ ^ ]



示例代码:



I don't think there is a way in the linux system libraries to get that since the underlying libraries use the abbreviated forms only and the long forms are likely not available anywhere.

However, the ICU library does have the requisite methods and many more. It is installed by default on most modern linux distros (you will need libicu-dev or equivalent to compile): http://userguide.icu-project.org/datetime/timezone[^]

Sample code:

#include 
#include <iostream>

using namespace U_ICU_NAMESPACE;

  TimeZone* tz = TimeZone::createDefault();
  UnicodeString us;
  std::string s;
  tz->getDisplayName(us);
  us.toUTF8String(s);
  std::cout << "Current timezone: " << s << '\n';
  delete tz;