请务必检查我们的对应版本:[TrueTime](https://github.com/instacart/truetime-android),一个适用于 Android 的 NTP 库。
Swift 的 NTP 客户端。计算不受手动更改设备时钟时间影响的“现在”时间。
在某些应用程序中,获取真实或“正确”的日期和时间变得非常重要。在大多数设备上,如果时钟被手动更改,那么一个 NSDate()
的实例会给出受本地设置影响的时间。
用户可能出于各种原因这么做,比如处于不同的时区、试图准时并将时钟设置为早 5-10 分钟等。您的应用程序或服务可能需要一个不受这些更改影响且可作为真实时间来源的日期。TrueTime 就可以提供这个功能。
TrueTime 是如何计算的?
实际上非常简单。我们向一个NTP服务器发送请求,获取实际时间。然后,我们计算设备运行时间和网络响应时间之间的差异。每次后续请求now()
时,我们会考虑这个偏移量,并返回一个修正后的NSDate
值。
用法
Swift
import TrueTime
// At an opportune time (e.g. app start):
let client = TrueTimeClient.sharedInstance
client.start()
// You can now use this instead of NSDate():
let now = client.referenceTime?.now()
// To block waiting for fetch, use the following:
client.fetchIfNeeded { result in
switch result {
case let .success(referenceTime):
let now = referenceTime.now()
case let .failure(error):
print("Error! \(error)")
}
}
Objective-C
@import TrueTime;
// At an opportune time (e.g. app start):
TrueTimeClient *client = [TrueTimeClient sharedInstance];
[client startWithPool:@[@"time.apple.com"] port:123];
// You can now use this instead of [NSDate date]:
NSDate *now = [[client referenceTime] now];
// To block waiting for fetch, use the following:
[client fetchIfNeededWithSuccess:^(NTPReferenceTime *referenceTime) {
NSLog(@"True time: %@", [referenceTime now]);
} failure:^(NSError *error) {
NSLog(@"Error! %@", error);
}];
通知
您还可以监听TrueTimeUpdated
通知,检测参考时间何时被获取
let client = TrueTimeClient.sharedInstance
let _ = NSNotificationCenter.default.addObserver(forName: .TrueTimeUpdated, object: client) { _ in
// Now guaranteed to be non-nil.
print("Got time: \(client.referenceTime?.now()")
}
安装选项
TrueTime目前兼容iOS 8以上版本,macOS 10.10和tvOS 9。
Carthage (推荐)
将其添加到您的Cartfile
中
github "instacart/TrueTime.swift"
然后运行
$ carthage update
CocoaPods
将此添加到您的 Podfile
pod 'TrueTime'
然后运行
$ pod install
手动方式
- 运行
git submodule update --init
. - 运行
carthage bootstrap
. - 如果尚未安装,请运行
brew install swiftlint
。 - 打开
TrueTime.xcodeproj
,选择TrueTimeExample
并运行。这将构建一切并运行示例应用。
使用git子模块手动方式
- 添加TrueTime为子模块
$ git submodule add https://github.com/instacart/TrueTime.swift.git
- 按照以上手动引导进行引导。
- 将
TrueTime.xcodeproj
拖到项目导航器中。 - 转到
项目 > 目标 > 构建阶段 > 链接二进制与库
,点击+
并选择TrueTime
目标。
说明/技巧
- 由于
NSDates
只是 Unix 时间戳,所以可以安全地保存由ReferenceTime.now()
返回的值或持久化到磁盘,而无需稍后进行调整。 - 自动处理可达性事件来暂停/开始请求。
- UDP 请求并行执行,默认限制为 5 个并行调用。如果其中一个失败,我们默认将重试多达 3 次。
- TrueTime 同样 适用于 Android。
贡献
该项目遵循贡献者守则 行为准则。通过参与(包括但不限于;报告问题、评论问题以及贡献代码)你应遵守此准则。请将不可接受的行为报告给 [email protected]。
设置
开发依赖于一些 Carthage 依赖和 xcconfig git 子模块。
使用以下命令克隆仓库并设置依赖项:
git submodule update --init --recursive
carthage bootstrap
许可
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://apache.ac.cn/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.