【问题标题】:Wrong systemTimeZone错误的系统时区
【发布时间】:2014-04-26 09:24:55
【问题描述】:

我正在尝试获取 systemTimeZone,但它给了我错误的数据:

NSTimeZone * currentDateTimeZone = [NSTimeZone systemTimeZone];
NSString* name = [currentDateTimeZone name];
int myGMT = (int)roundf([currentDateTimeZone secondsFromGMT]/60.f/60.f);

我住在匈牙利的布达佩斯。现在是 GMT+1,但我收到了 myGMT = 2. 但名字没问题:name = Europe/Budapest 为什么?

【问题讨论】:

    标签: ios timezone nsdate nstimezone


    【解决方案1】:

    欧洲/布达佩斯时区的当前 GMT 偏移量是 GMT+2,因为 Daylight Saving Time 于 2014 年 3 月 30 日星期日开始,时钟是 提前一小时(见http://www.timeanddate.com/worldclock/city.html?n=50)。

    你可以用

    来验证
    BOOL isDst = [currentDateTimeZone isDaylightSavingTime];
    // --> YES
    NSTimeInterval dstOffset = [currentDateTimeZone daylightSavingTimeOffset];
    // --> 3600
    

    如果需要,可以计算

    [currentDateTimeZone secondsFromGMT] - [currentDateTimeZone daylightSavingTimeOffset]
    

    获取您所在时区的“默认”GMT 偏移量。

    【讨论】:

    • 真的很好!谢谢你,你拯救了我的一天:)
    • 所以如果我这样使用它: int myGMT = [currentDateTimeZone secondsFromGMT] - [currentDateTimeZone dailySavingTimeOffset]; myGMT = (int)roundf(myGMT/60.f/60.f);它会给我正确的格林威治标准时间,所有空间?
    • @incmiko:这取决于您所说的“正确的 GMT”是什么意思。
    • 例如,我想一直在布达佩斯 GMT+1
    • @incmiko:是的,这将给出与当前日期无关的相同结果,例如布达佩斯 GMT+1。
    猜你喜欢
    • 2011-05-30
    • 1970-01-01
    • 2011-12-14
    • 2017-11-15
    • 2014-11-29
    • 1970-01-01
    • 1970-01-01
    • 2020-06-05
    • 1970-01-01
    相关资源
    最近更新 更多