secondsFromGMT returns 设备和模拟器上的不同值
secondsFromGMT returns different values on device and simulator
我正在开发一个显示不同时区时间的时间应用程序。为此,我使用标准时间 haDate
(UTC tz)。
对于 displayDate
我使用的是系统时区 api。时区是 America/Santiago
(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
returns -10800
但在设备上它 returns -14400
,少了 1 小时。
设备和模拟器使用相同的时区Asia/Kolkata
(UTC+5:30)。我知道 America/Santiago
使用 DST 但为什么它给我不同的秒数,即使两者(模拟器和设备)都在同一时区。
出了什么问题,我该如何解决?
注意
为了解决 DST 问题,我正在使用此代码。但它总是转到两个设备上的 else 部分。 (顺便说一句,下面的代码不是必需的,因为 tz.secondsFromGMT
总是在 DST 调整后 return 秒。)
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) offset -10800
在设备上
tz America/Santiago (GMT-4) offset -14400
所以它没有使用 DST。
注 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.
我正在开发一个显示不同时区时间的时间应用程序。为此,我使用标准时间 haDate
(UTC tz)。
对于 displayDate
我使用的是系统时区 api。时区是 America/Santiago
(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
returns -10800
但在设备上它 returns -14400
,少了 1 小时。
设备和模拟器使用相同的时区Asia/Kolkata
(UTC+5:30)。我知道 America/Santiago
使用 DST 但为什么它给我不同的秒数,即使两者(模拟器和设备)都在同一时区。
出了什么问题,我该如何解决?
注意
为了解决 DST 问题,我正在使用此代码。但它总是转到两个设备上的 else 部分。 (顺便说一句,下面的代码不是必需的,因为 tz.secondsFromGMT
总是在 DST 调整后 return 秒。)
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) offset -10800
在设备上
tz America/Santiago (GMT-4) offset -14400
所以它没有使用 DST。
注 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.