【发布时间】:2016-12-04 05:43:24
【问题描述】:
谁能帮我获取时区的当前时间?
例如,如果用户有一个印度时区,即 5.00 PM,但他将他的移动时间静态更改为 5.30 PM,这里我需要实际时区时间,而不是用户根据他的喜好更改的移动时间.
【问题讨论】:
-
这个gist 可能就是你要找的。span>
谁能帮我获取时区的当前时间?
例如,如果用户有一个印度时区,即 5.00 PM,但他将他的移动时间静态更改为 5.30 PM,这里我需要实际时区时间,而不是用户根据他的喜好更改的移动时间.
【问题讨论】:
如果您需要访问世界时钟,请联系 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)
}
}
【讨论】: