简体   繁体   English

timeIntervalSince1970以毫秒为单位使用什么类型?

[英]What type to use for timeIntervalSince1970 in ms?

I need time since Epoch in ms for an API request, so I'm looking to have a function that converts my myUIDatePicker.date.timeIntervalSince1970 into milliseconds by multiplying by 1000. My question is what should the return value be? 我需要从Epoch到毫秒之间的时间来请求API,所以我希望有一个函数可以将我的myUIDatePicker.date.timeIntervalSince1970乘以1000转换为毫秒。我的问题是返回值应该是多少?

Right now I have 现在我有

func setTimeSinceEpoch(datePicker: UIDatePicker) -> Int {
    return Int(datePicker.date.timeIntervalSince1970 * 1000)
}

Will this cause any issues? 这会引起任何问题吗? I need an integer, not a floating point, but will I have issues with overflow? 我需要一个整数,而不是浮点数,但是我会遇到溢出问题吗? I tested it out with print statements and it seems to work but I want to find the best way of doing this. 我用打印语句对其进行了测试,它似乎可以工作,但是我想找到最佳的方法。

Looking at Apple Docs : 苹果文档

var NSTimeIntervalSince1970: Double { get }

There is a nice function called distantFuture. 有一个很好的函数,名为distantFuture。 Even if you use this date in you func the result will be smaller then the max Int. 即使在函数中使用此日期,结果也会小于最大Int。

let future = NSDate.distantFuture() // "Jan 1, 4001, 12:00 AM"
print((Int(future.timeIntervalSince1970) * 1000) < Int.max) // true

So, until 4001 you're good to go. 因此,直到4001年,您都可以使用。 It will work perfectly on 64-bits systems. 它将在64位系统上完美运行。

Note: If your system supports iPhone 5 (32-bits) it's going to get an error on pretty much any date you use. 注意:如果您的系统支持iPhone 5(32位),则在您使用的几乎任何日期都会出现错误。 Int in Iphone 5 corresponds to Int32. Iphone 5中的Int对应于Int32。 Returning an Int64 is a better approach. 返回Int64是更好的方法。 See this . 看到这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM