【问题标题】:Swift ios date as milliseconds Double or UInt64?Swift ios 日期为毫秒 Double 或 UInt64?
【发布时间】:2014-10-05 10:45:58
【问题描述】:

我不是 iOS 开发人员,但开始学习 Swift。

我尝试将一些逻辑从 Android 项目转换为 iOS。

我有以下方法:

func addGroupItemSample(sample : WmGroupItemSample){ // some custom class

    var seconds: NSTimeInterval = NSDate().timeIntervalSince1970
    var  cuttDate:Double =  seconds*1000;

    var sampleDate: UInt64 = sample.getStartDate(); // <-- problematic place

if(sampleDate > cuttDate){
   // ....
  }
}

从上面的方法可以看出sample.getStartDate()返回类型UInt64

我认为这就像 Java 中的 longSystem.currentTimeMillis()

但当前时间以毫秒为单位定义为Double

混合DoubleUInt64 是一种正确的方法,还是我只需要将所有毫秒表示为Double

谢谢,

【问题讨论】:

    标签: swift nsdate nstimeinterval


    【解决方案1】:

    在 iOS 中最好使用 double,但如果您想轻松移植代码并保持一致,您可以试试这个:

    func currentTimeMillis() -> Int64{
        let nowDouble = NSDate().timeIntervalSince1970
        return Int64(nowDouble*1000)
    }
    

    【讨论】:

    • 你为什么要做 Int64(..) + Int64(...) ?
    • 变量 'nowDouble' 从未发生变异;考虑更改为“让”常量
    【解决方案2】:

    Swift 不允许比较不同的类型。

    secondsDouble 浮点值,以秒为单位,精度为亚秒级。
    sampleDate 是 UInt64,但未给出单位。 sampleDate 需要转换为以秒为单位的 Double 浮点值。

    var sampleDate: Double = Double(sample.getStartDate())
    

    然后可以比较:

    if(sampleDate > cuttDate){}
    

    【讨论】:

      【解决方案3】:

      这是 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)
         }
      

      【讨论】:

      • 对于今天给我投票的人,请注意我现在已经编辑了代码以通过两种方式改进它 - 请参阅编辑 cmets。
      【解决方案4】:
      func get_microtime() -> Int64{
          let nowDouble = NSDate().timeIntervalSince1970
          return Int64(nowDouble*1000)
      }
      

      工作正常

      【讨论】:

        【解决方案5】:

        在 UInt64 中获取时间令牌的简单一行代码

        让时间 = UInt64(Date().timeIntervalSince1970 * 1000)

        print(time)

        补充提示:

        然后查找自 1970 年以来 10 位毫秒的时间戳用于 API 调用

        让 timeStamp = Date().timeIntervalSince1970

        print(timeStamp)

        【讨论】:

          猜你喜欢
          • 2014-10-12
          • 1970-01-01
          • 1970-01-01
          • 2017-03-01
          • 2021-06-30
          • 2019-12-24
          • 2015-12-17
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多