如何在Windows下获取时区缩写 [英] How to get timezone abbreviation under Windows
问题描述
我们正在尝试通过 strftime
:
We are trying to get the timezone from std::tm
with strftime
:
char timezone[50];
strftime(timezone, sizeof(timezone), "%Z", &timeCreated);
在iOS上,我们会得到所需的 EST。但是在Windows上,我们会得到东部夏令时。有人知道如何以缩写形式一致地获取C ++中的当前时区吗?
On iOS, we get "EST" which is what we want. But on Windows, we get "Eastern Summer Time". Anybody know how to consistently get the current timezone in C++ in abbreviation form?
我考虑通过简单地选择每个单词的第一个字符来从时区的全名中进行缩写。但是,我检查了缩写的列表,并注意到我们可能会有这样的时区 ,缩写为 CHUT。这使得无法进行手动调整。
I consider making the abbreviation from the full name of the timezone by simply picking out the first character in each word. But I check the list of abbreviations and notice that we could have timezone like this one "Chuuk Time" and abbreviated as "CHUT". Which makes manually adjusting not possible.
与问题不同: Windows时区及其缩写?我不需要所有时区和缩写的完整列表。但是,相反,我需要使用例如 strftime
的当前时区的系统方法。我希望他们使用系统的当前时区和当前本地时间。
Not the same as Question: Windows Timezone and their abbreviations? I don't need a full list of all timezones and abbreviations. But instead, I need a systematic way to the current timezone using for example strftime
. I want them to use the system's current timezone and the the current local.
推荐答案
使用此免费的开放源代码库,已移植到VS-2013及更高版本:
Using this free, open-source library that has been ported to VS-2013 and later:
#include "tz.h"
#include <iostream>
int
main()
{
using namespace std::chrono;
using namespace date;
std::cout << format("%Z\n", make_zoned(current_zone(), system_clock::now()));
}
这对我来说只是输出:
EDT
该库使用 IANA时区数据库,并在调用 current_zone()
时,将当前Windows时区转换为适当的 IANA时区。
This library uses the IANA timezone database, and when current_zone()
is called, translates the current Windows timezone into the appropriate IANA timezone.
这篇关于如何在Windows下获取时区缩写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!