提问人: 提问时间:12/11/2008 最后编辑:TheNeil 更新时间:9/3/2019 访问量:277329
iPhone:如何获取当前毫秒数?
iPhone: How to get current milliseconds?
答:
[[NSDate date] timeIntervalSince1970];
它以双精度形式返回自 epoch 以来的秒数。我几乎可以肯定您可以从小数部分访问毫秒。
评论
[NSDate timeIntervalSinceReferenceDate]
更便宜。(尽管它引用了不同的“纪元”——如果你想要 1970 年,请添加常量。NSTimeIntervalSince1970
timeIntervalSince1970
如果您正在考虑将其用于相对计时(例如游戏或动画),我宁愿使用 CACurrentMediaTime()
double CurrentTime = CACurrentMediaTime();
这是推荐的方式; 从联网的同步时钟中汲取,在与网络重新同步时偶尔会打嗝。NSDate
它返回当前绝对时间(以秒为单位)。
如果只需要小数部分(通常在同步动画时使用),
let ct = CACurrentMediaTime().truncatingRemainder(dividingBy: 1)
评论
[NSDate timeIntervalSinceReferenceDate]
是另一种选择,如果您不想包含 Quartz 框架。它返回一个双精度值,表示秒数。
了解 CodeTimestamp 可能很有用,它提供了基于 mach 的计时函数的包装器。这为您提供了纳秒级分辨率的计时数据 - 比毫秒级精确 1000000 倍。是的,精确度要精确一百万倍。(前缀是毫、微、纳米,每个前缀都比上一个精确 1000 倍。即使你不需要 CodeTimestamps,也可以查看代码(它是开源的),看看他们如何使用 mach 来获取计时数据。当您需要更高的精度并希望比 NSDate 方法更快的方法调用时,这将非常有用。
http://eng.pulse.me/line-by-line-speed-analysis-for-ios-apps/
评论
-fno-objc-arc
到目前为止,我在 iOS (iPad) 上找到了一个很好的解决方案,当您想执行一些间隔评估(例如,帧速率、渲染帧的时序......gettimeofday
#include <sys/time.h>
struct timeval time;
gettimeofday(&time, NULL);
long millis = (time.tv_sec * 1000) + (time.tv_usec / 1000);
评论
我在 iPhone 4S 和 iPad 3(发布版本)上对所有其他答案进行了基准测试。 开销最小,幅度很小。 比其他的慢得多,可能是由于实例化开销,尽管对于许多用例来说可能无关紧要。CACurrentMediaTime
timeIntervalSince1970
NSDate
如果您想要最小的开销并且不介意添加 Quartz Framework 依赖项,我建议您这样做。或者,如果便携性是您的首要任务。CACurrentMediaTime
gettimeofday
iPhone 4S的
CACurrentMediaTime: 1.33 µs/call
gettimeofday: 1.38 µs/call
[NSDate timeIntervalSinceReferenceDate]: 1.45 µs/call
CFAbsoluteTimeGetCurrent: 1.48 µs/call
[[NSDate date] timeIntervalSince1970]: 4.93 µs/call
iPad的 3
CACurrentMediaTime: 1.25 µs/call
gettimeofday: 1.33 µs/call
CFAbsoluteTimeGetCurrent: 1.34 µs/call
[NSDate timeIntervalSinceReferenceDate]: 1.37 µs/call
[[NSDate date] timeIntervalSince1970]: 3.47 µs/call
评论
NSTimeIntervalSince1970
NSTimeIntervalSince1970
我需要一个包含 的确切结果的对象。由于这个函数被调用了很多次,而且我真的不需要创建一个对象,所以性能不是很好。NSNumber
[[NSDate date] timeIntervalSince1970]
NSDate
因此,要获取原始函数给我的格式,请尝试以下操作:
#include <sys/time.h>
struct timeval tv;
gettimeofday(&tv,NULL);
double perciseTimeStamp = tv.tv_sec + tv.tv_usec * 0.000001;
这应该给你完全相同的结果[[NSDate date] timeIntervalSince1970]
// Timestamp after converting to milliseconds.
NSString * timeInMS = [NSString stringWithFormat:@"%lld", [@(floor([date timeIntervalSince1970] * 1000)) longLongValue]];
在 Swift 中,我们可以创建一个函数并执行以下操作
func getCurrentMillis()->Int64{
return Int64(NSDate().timeIntervalSince1970 * 1000)
}
var currentTime = getCurrentMillis()
虽然它在 Swift 3.0 中工作正常,但我们可以修改和使用该类而不是在 3.0 中Date
NSDate
斯威夫特 3.0
func getCurrentMillis()->Int64 {
return Int64(Date().timeIntervalSince1970 * 1000)
}
var currentTime = getCurrentMillis()
试试这个:
NSDate * timestamp = [NSDate dateWithTimeIntervalSince1970:[[NSDate date] timeIntervalSince1970]];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss.SSS"];
NSString *newDateString = [dateFormatter stringFromDate:timestamp];
timestamp = (NSDate*)newDateString;
在此示例中,dateWithTimeIntervalSince1970 与格式化程序 @“YYYY-MM-dd HH:mm:ss 结合使用。SSS“,它将返回带有年、月、日的日期和以小时、分钟、秒和毫秒为单位的时间。请参阅示例:“2015-12-02 04:43:15.008”。我使用 NSString 来确保格式之前已经写过。
CFAbsoluteTimeGetCurrent()
相对于 2001 年 1 月 1 日 00:00:00 GMT 的绝对参考日期,绝对时间以秒为单位。正值表示参考日期之后的日期,负值表示参考日期之前的日期。例如,绝对时间 -32940326 相当于 1999 年 12 月 16 日 17:54:34。对此函数的重复调用并不能保证单调递增的结果。由于与外部时间引用同步或由于用户对时钟的显式更改,系统时间可能会减少。
这就是我用于 Swift 的方法
var date = NSDate()
let currentTime = Int64(date.timeIntervalSince1970 * 1000)
print("Time in milliseconds is \(currentTime)")
使用此站点来验证准确性 http://currentmillis.com/
斯威夫特 2
let seconds = NSDate().timeIntervalSince1970
let milliseconds = seconds * 1000.0
斯威夫特 3
let currentTimeInMiliseconds = Date().timeIntervalSince1970.milliseconds
评论
TimeInterval
aka 没有毫秒属性。Double
Date().timeIntervalSince1970 * 1000
这与 @TristanLorach 发布的答案基本相同,只是针对 Swift 3 进行了重新编码:
/// Method to get Unix-style time (Java variant), i.e., time since 1970 in milliseconds. This
/// copied from here: http://stackoverflow.com/a/24655601/253938 and here:
/// http://stackoverflow.com/a/7885923/253938
/// (This should give good performance according to this:
/// http://stackoverflow.com/a/12020300/253938 )
///
/// Note that it is possible that multiple calls to this method and computing the difference may
/// occasionally give problematic results, like an apparently negative interval or a major jump
/// forward in time. This is because system time occasionally gets updated due to synchronization
/// with a time source on the network (maybe "leap second"), or user setting the clock.
public static func currentTimeMillis() -> Int64 {
var darwinTime : timeval = timeval(tv_sec: 0, tv_usec: 0)
gettimeofday(&darwinTime, nil)
return (Int64(darwinTime.tv_sec) * 1000) + Int64(darwinTime.tv_usec / 1000)
}
NSTimeInterval time = ([[NSDate date] timeIntervalSince1970]); //double
long digits = (long)time; //first 10 digits
int decimalDigits = (int)(fmod(time, 1) * 1000); //3 missing digits
/*** long ***/
long timestamp = (digits * 1000) + decimalDigits;
/*** string ***/
NSString *timestampString = [NSString stringWithFormat:@"%ld%03d",digits ,decimalDigits];
func currentmicrotimeTimeMillis() -> Int64{
let nowDoublevaluseis = NSDate().timeIntervalSince1970
return Int64(nowDoublevaluseis*1000)
}
获取当前日期的毫秒数。
斯威夫特 4+:
func currentTimeInMilliSeconds()-> Int
{
let currentDate = Date()
let since1970 = currentDate.timeIntervalSince1970
return Int(since1970 * 1000)
}
let timeInMiliSecDate = Date()
let timeInMiliSec = Int (timeInMiliSecDate.timeIntervalSince1970 * 1000)
print(timeInMiliSec)
评论