我正在开发一个显示不同时区时间的时间应用程序。为此,我使用标准时间 haDate
(UTC tz)。
对于 displayDate
我正在使用系统时区 api。时区是美国/圣地亚哥
(UTC-3:00)。
NSTimeZone *tz=[NSTimeZone timeZoneWithName:_timeZone];
_displayDate=[_haDate dateByAddingTimeInterval:tz.secondsFromGMT];
“haDate”代码 -
NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
formatter.timeZone=[NSTimeZone timeZoneWithName:@"UTC"];
NSString *utcTimeCurrent=[dict objectForKey:@"utctime"];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
_haDate=[formatter dateFromString:utcTimeCurrent];
问题是当我在模拟器上运行此代码时 tz.secondsFromGMT
返回 -10800
但在设备上它返回 -14400
,这是减少 1 小时。
设备和模拟器使用相同的时区 Asia/Kolkata
(UTC+5:30)。我知道 America/Santiago
使用 DST,但为什么即使两者(模拟器和设备)都在同一时区,它也会给我不同的秒数。
出了什么问题,我该如何解决?
注意
为了解决 DST 问题,我使用了这段代码。但它总是转到两个设备上的 else 部分。 (顺便说一句,下面的代码不是必需的,因为 tz.secondsFromGMT
总是在 DST 调整后返回秒数。)
NSTimeZone *tz=[NSTimeZone timeZoneWithName:_timeZone];
if (tz.isDaylightSavingTime)
{
_displayDate=[_haDate dateByAddingTimeInterval:tz.secondsFromGMT+tz.daylightSavingTimeOffset];
}
else
{
_displayDate=[_haDate dateByAddingTimeInterval:tz.secondsFromGMT];
}
当我记录 tz 时 -
在模拟器上
tz America/Santiago (GMT-3) 偏移量 -10800
在设备上
tz America/Santiago (GMT-4) 偏移量 -14400
所以它没有使用夏令时。
注意事项 2
此问题仅出现在 iPad 2 中。其他设备正常。
注意事项 3
我的 iPad 2 使用 iOS 8.4。两个时区(智利标准时间 -America/Santiago
和复活节岛标准时间 - Pacific/Easter
)都给我错误的秒数
最佳答案
智利在 2015 年初更改了 TZ。可能您的 iPad 2 的 iOS 版本使用了过时的时区数据库。更新它。
Time in Chile is divided into two time zones. Continental Chile uses the time offset UTC−03:00. Additionally, Easter Island uses the time offset UTC−05:00.[1]
Until 2015, Continental Chile used the time offset UTC−04:00 and Easter Island used UTC−06:00 for standard time, with daylight saving time roughly between October and March every year. However, in January 2015 it was announced that all the country will keep the time offset used during daylight saving time.
关于ios - secondsFromGMT 在设备和模拟器上返回不同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31334312/