如何获得ICU中的所有时区名称

how to get all timezone names in ICU

本文关键字:时区 何获得 ICU      更新时间:2023-10-16

我使用boost::locale与ICU后端进行不同时区之间的时间转换。在创建boost::local::Calendar时,我可以传入一个像"America/New_York"这样的字符串来指定时区信息。

但是我如何获得所有有效时区名称的列表?

来自ICU文档,它提到用户可以使用timezone . getavailableids()方法遍历所有时区名称。但我甚至找不到一个方法叫getavailableid在timezone。h

您可以使用TimeZone.createEnumeration()获取所有时区名称的列表。文档中确实说使用getAvailabeIDs,但这种方法似乎已经不存在了。

我设法实现这样,使用ICU 4.4.2:

#include <iostream>
#include <unicode/timezone.h>
#include <unicode/unistr.h>
using namespace icu;
int main()
{
   StringEnumeration *timeZoneIds = TimeZone::createEnumeration();
   UErrorCode status = U_ZERO_ERROR;
   const UnicodeString *zoneId = timeZoneIds->snext(status);
   while (zoneId != NULL && status == U_ZERO_ERROR)
   {
      std::string zoneIdString;
      zoneId->toUTF8String(zoneIdString);
      std::cout << zoneIdString << std::endl;
      zoneId = timeZoneIds->snext(status);
   }
   delete timeZoneIds;
   return 0;
}