ios - secondsFromGMT 在设备和模拟器上返回不同的值

标签 ios objective-c cocoa-touch timezone nsdate

我正在开发一个显示不同时区时间的时间应用程序。为此,我使用标准时间 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.

https://en.wikipedia.org/wiki/Time_in_Chile

关于ios - secondsFromGMT 在设备和模拟器上返回不同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31334312/

相关文章:

ios - 使用第三方库对 Swift 进行单元测试

ios - 隐藏标签栏容器 View

ios - 向 UITableViewController 内的表格 View 添加左右边距

ios - SKLabelNode 触摸更新

iphone - 在运行同一应用程序的两个 iOS 设备之间传输对象的最佳方式是什么?

ios - UISlider - 画线/图像到轨道(条)

objective-c - 在Obj-C中创建Properties,默认的Getter怎么写?

iphone - 使用委托(delegate)切换多个 View

objective-c - 使用 Core Data 在多个 View Controller 之间共享数据

ios - 接近套件 :Error Domain=kCLErrorDomain Code=17