How to get the timezone of a location in iOS app? [duplicate]

喜欢而已 提交于 2019-12-11 07:03:10

问题


I am new to iOS development. I am trying to build an app which can primarily do two things:

a. get user's system time (say, his phone is in London, so his time)

b. get the time in a given location (say, San Francisco)

and then, I want to calculate the difference in time between the location (e.g. London, UK is 8 hours ahead of San Francisco, CA)

Can someone please help me with suggestions for how can I get (a) and (b)?


回答1:


For both (a) and (b):

      let offset = TimeZone.current.secondsFromGMT();

        print(offset)//Your current timezone offset in seconds

        let loc = CLLocation.init(latitude: 48.8566, longitude: 2.3522);//Paris's lon/lat


        let coder = CLGeocoder();
        coder.reverseGeocodeLocation(loc) { (placemarks, error) in
            let place = placemarks?.last;

            let newOffset = place?.timeZone?.secondsFromGMT();

            print(newOffset);//Paris's timezone offset in seconds
        }



回答2:


If you're looking for local time you can use:

NSTimeZone *timeZoneLocal = [NSTimeZone localTimeZone];  
NSString *strTimeZoneName = [timeZoneLocal name];

  or

NSTimeZone *timeZoneLocal = [NSTimeZone defaultTimeZone];
NSString *strTimeZoneName = [timeZoneLocal name];

You would probably just need two text fields and have the user compare the time difference.



来源:https://stackoverflow.com/questions/45372506/how-to-get-the-timezone-of-a-location-in-ios-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!