gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:30:38 25 4
gpt4 key购买 nike

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

例如,如果用户使用移动设备的印度时区,即下午 5.00,但他将移动时间静态更改为下午 5.30,这里我需要实际的时区时间,而不是用户根据自己的偏好更改的移动时间.

最佳答案

如果您需要访问世界时钟,请联系 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)
}
}

关于ios - 从时区获取当前时间,而不是系统/移动时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43600840/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com