OGeek|极客世界-中国程序员成长平台

标题: ios - NSTimeZone secondsFromGMTForDate 中的错误? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 10:34
标题: ios - NSTimeZone secondsFromGMTForDate 中的错误?

我正在编写一个涉及人们出生时间的应用程序,并且我正在尝试使用 IOS 来获取准确的 GMT 偏移量。我得到了明显错误的答案,我想知道其他人是否遇到过它们,或者是否有任何解决方法。

这是我用来计算偏移量的代码(第一行),以及用于显示我所拥有的记录的代码。

    ret.dateTime.offset = [tz secondsFromGMTForDate:date] / 3600.0;
    double dstOffset = [tz daylightSavingTimeOffsetForDate:date] / 3600.0;
    NSLog(@"Setting offset for %@ on %@, to %3.1f", ret.location.description, ret.dateTime.briefDateDescription, ret.dateTime.offset);
    NSLog (@"  (TZ Name = %@, date = %@, dstOffset = %3.1f)", tz.name, date, dstOffset);

以下是结果 - 对于东部时间的普利茅斯和纽约来说是正确的,并且 DST 偏移对于所有这些都是正确的,但是对于明尼阿波利斯(中部时间)来说 secondsFromGMTForDate 偏移了一小时,并且偏移了亚特兰大另一个方向的一个小时(东部时间)。

Setting offset for Minneapolis, MN on 3/26/70, to -5.0
  (TZ Name = America/Menominee, date = 1970-03-26 14:29:00 +0000, dstOffset = 0.0)
Setting offset for Plymouth, MA on 1/13/80, to -5.0
  (TZ Name = America/New_York, date = 1980-01-14 02:52:00 +0000, dstOffset = 0.0)
Setting offset for Atlanta, GA on 10/10/48, to -6.0
  (TZ Name = America/Kentucky/Monticello, date = 1948-10-10 13:07:00 +0000, dstOffset = 0.0)
Setting offset for New York, NY on 6/11/80, to -4.0
  (TZ Name = America/New_York, date = 1980-06-11 12:25:00 +0000, dstOffset = 1.0)

有什么线索吗?其他人在 70 年 3 月 26 日尝试明尼阿波利斯时会得到什么? America/Menominee 绝对是中央时间,并且 DST 偏移量为 0,secondsFromGMTForDate 绝对应该是 -6 小时,而不是 -5。



Best Answer-推荐答案


这些都是正确的。与大多数操作系统一样,Apple 的时区系统基于 IANA time zone data .该数据包括历史信息和当前信息,因此即使时区规则可能发生变化,旧日期也能正确转换。

对于America/Menominee,数据包括以下注释:

# Dickinson, Gogebic, Iron, and Menominee Counties, Michigan,
# switched from EST to CST/CDT in 1973.

你的考试日期是 1970 年,当时的地区不同。

对于America/Kentucky/Monticello,评论包括:

# After prolonged debate, and despite continuing deep differences of opinion,
# Wayne County (central Kentucky) is switching from Central (-0600) to Eastern
# (-0500) time.  They won't "fall back" this year.  See Sara Shipley,
# The difference an hour makes, Nando Times (2000-08-29 15:33 -0400).

您对该区域的测试日期是 1948 年,看起来那个位置当时是中部时间。

关于ios - NSTimeZone secondsFromGMTForDate 中的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29565710/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4