我正在编写一个涉及人们出生时间的应用程序,我正在尝试使用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)有什么线索吗?当其他人在3/26/70尝试明尼阿波利斯的时候,他们得到了什么?美国/梅诺米尼绝对是中央时间,如果DST偏移为0,secondsFromGMTForDate肯定应该是-6小时,而不是-5小时。
发布于 2015-04-10 16:38:56
这些是正确的。和大多数操作系统一样,苹果的时区系统是基于IANA时区数据的。该数据包括历史信息和当前信息,因此即使时区规则可能发生更改,旧日期也将正确转换。
对于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年,看起来那个地点是在当时的中央时间。
https://stackoverflow.com/questions/29565710
复制相似问题