从时区获取当前时间,而不是系统/移动时间 [英] Getting current time from timezone, not the system/mobile time

查看:149
本文介绍了从时区获取当前时间,而不是系统/移动时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Can any one help me to get the current time of the time zone?

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

Say for example if the user has an Indian timezone with mobile i.e. 5.00 PM, but he statically change his mobile time to 5.30 PM, here I need the actual timezone time, not the mobile time which the user changed as per his preference.

推荐答案

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

If you need access to the world's clock, contact an NTP server. Go to Cocoapods and find a pod to do that. The example below is for 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)
    }
}

这篇关于从时区获取当前时间,而不是系统/移动时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆