21

我正在尝试查找时区标识符的最大长度。这是用作时区名称的字符串(例如“America/New_York”)。tz 数据库没有帮助;我找不到实施细节。

Microsoft(.NET Framework 4.5)建议最大长度为 32,但这似乎是他们注册表的限制。

libc 指向一个名为“_POSIX_TZNAME_MAX”的限制,长度为 3 个字符,但这是 POSIX 合规性的绝对最低要求。通常,我猜一个实现会使用更多。

所以真正的问题是:安全存储时区“tzname”/标识符名称的可接受字符串长度是多少?

4

1 回答 1

29

为什么不使用不关心长度的容器——例如std::string

现在,碰巧我最近正在使用以通用 csv 格式提供的 TZ db(例如,在 Boost 的文件中),但在 Boost 源中也使用了相同的格式。

有了这些数据,我看到的最大长度为 28:

R> library(RcppBDT)                      # R package interfacing Boost Date_Time
Loading required package: Rcpp
R> tz <- new(bdtTz, "America/Chicago")   # init. an object, using my default TZ
R> tznames <- tz$getAllRegions()         # retrieve list of all TZ names
R>
R> length(tznames)                       # total number of TZ identifiers
[1] 381
R>
R> head(tznames)                         # look at first six
[1] "Africa/Abidjan"     "Africa/Accra"       "Africa/Addis_Ababa" 
[4] "Africa/Algiers"     "Africa/Asmera"      "Africa/Bamako"     
R>
R> summary(sapply(tznames, nchar))       # numerical summary of length of each
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
      9      13      15      15      17      28 
R>
R> tznames[ nchar(tznames) >= 26 ]       # looking at length 26 and above
[1] "America/Indiana/Indianapolis" "America/Kentucky/Louisville"  
[3] "America/Kentucky/Monticello"  "America/North_Dakota/Center" 
R> 

我们还可以查看直方图:

R> library(MASS)
R> truehist(sapply(tznames, nchar), 
+           main="Distribution of TZ identifier length", col="darkgrey")
R>

在此处输入图像描述

这使用了我在 R-Forge 上的 RcppBDT 包的 SVN存储库中的代码,但还没有在包的CRAN 版本中。

于 2012-09-22T18:01:29.557 回答