从时区获取当前时间,而不是系统/移动时间

时间:2016-07-29 13:18:57

标签: ios swift timezone nsdate

任何人都可以帮助我获取时区的当前时间吗?

例如,如果用户有移动设备的印度时区,即下午5点,但是他将移动时间静态更改为下午5点30分,这里我需要实际的时区时间,而不是用户根据自己的喜好更改的移动时间

1 个答案:

答案 0 :(得分:1)

如果您需要访问世界时钟,请联系NTP服务器。去Cocoapods并找到一个可以做到这一点的吊舱。以下示例适用于ios-ntp

class ViewController: UIViewController {
    let netAssociation = NetAssociation(serverName: "time.apple.com")

    func viewDidLoad() {
        super.viewDidLoad()

        netAssociation.delegate = self
        netAssociation.sendTimeQuery()
    }

    func reportFromDelegate() {
        let timeOffset = netAssociation.offset

        // serverTime will always be in UTC. Use an NSDateFormatter
        // to display it in your local timezone 
        let serverTime = NSDate(timeIntervalSince1970: timeOffset)
    }
}
相关问题