如何在C ++中获取Linux上的当前时区 [英] How to get current timezone on linux in C++

查看:1456
本文介绍了如何在C ++中获取Linux上的当前时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





目前我正在使用Strftime来获取我的机器在Windows中的时区。但是在Linux中,它只提供像(IST)这样的短时区字符串,但我需要(印度标准时间)。在windows中,相同的api给出了长时区字符串。



下面是我用于linux和windows的代码。

strftime( buff,sizeof buff,%Z,&tnow);



提前致谢。

Hi,

Currently i am using Strftime to get the timezone of my machine in windows. But in linux it gives only short timezone string like (IST) but i need (Indian standard time). In windows the same api give the long timezone string.

Below is the code i am using for both linux and windows.
strftime( buff, sizeof buff, "%Z" , &tnow );

Thanks in advance.

推荐答案

您需要使用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;


这篇关于如何在C ++中获取Linux上的当前时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆