【问题标题】:Getting current time from timezone, not the system/mobile time从时区获取当前时间,而不是系统/移动时间
【发布时间】:2016-12-04 05:43:24
【问题描述】:

谁能帮我获取时区的当前时间?

例如,如果用户有一个印度时区,即 5.00 PM,但他将他的移动时间静态更改为 5.30 PM,这里我需要实际时区时间,而不是用户根据他的喜好更改的移动时间.

【问题讨论】:

标签: ios swift timezone nsdate


【解决方案1】:

如果您需要访问世界时钟,请联系 NTP 服务器。去 Cocoapods 并找到一个 pod 来做到这一点。以下示例适用于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)
    }
}

【讨论】:

    猜你喜欢
    • 2013-03-19
    • 2011-07-02
    • 2013-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多