iPhone:Find the current timezone offset in HOURS

后端 未结 8 697
萌比男神i
萌比男神i 2021-02-05 03:11

How to find out the current timezone offset on the device in HOURS ?

8条回答
  •  星月不相逢
    2021-02-05 03:46

    -(void)getDeviceUTCOffset{
    
        NSDate *date = [NSDate date];
        NSTimeZone* deviceTimeZone_ = [NSTimeZone systemTimeZone];
        float offset = [deviceTimeZone_ secondsFromGMTForDate:date] / 3600.0;   
    }
    

    To test code - test on a device

    change device Timezone in

    Settings > General > Date Time
    Turn off SET AUTOMATICALLY
    Then tap TimeZone and choose major cities east/west of you
    Run code and check float offset
    

    Compare value to cities on this site

    e.g. heres Helsinki

    Standard time zone: UTC/GMT +2 hours - as of Nov

    http://www.timeanddate.com/worldclock/city.html?n=101

    Check against countries with British Summer Time or their equivalent.

提交回复
热议问题