18

I'm not iOS developer but started learning Swift.

I try to convert some logic from Android project to iOS.

I have the following method:

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){
   // ....
  }
}

From the method above you can see that sample.getStartDate() returns type UInt64.

I thought it's like long in Java: System.currentTimeMillis()

But current time in milliseconds defined as Double.

Is it a proper way to mix Double and UInt64 or do I need to represent all milliseconds as Double only?

Thanks,

snaggs
  • 5,543
  • 17
  • 64
  • 127

5 Answers5

30

in iOS it is better to use double, but if you want to easy port your code and keep it consistent you can try this:

func currentTimeMillis() -> Int64{
    let nowDouble = NSDate().timeIntervalSince1970
    return Int64(nowDouble*1000)
}
Ben
  • 3,832
  • 1
  • 29
  • 30
7

Swift does not allow comparing different types.

seconds is a Double floating point value in seconds with sub-second accuracy.
sampleDate is a UInt64 but the units are not given. sampleDate needs be converted to a Double floating point value with units of seconds.

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

Then they can be compared:

if(sampleDate > cuttDate){}
zaph
  • 111,848
  • 21
  • 189
  • 228
7

Here's an alternative version, for 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)
   }
RenniePet
  • 11,420
  • 7
  • 80
  • 106
  • To whoever gave me an up-vote on this today, please note that I've now edited the code to improve it in two ways - see the edit comments. – RenniePet Apr 01 '17 at 14:38
2
func get_microtime() -> Int64{
    let nowDouble = NSDate().timeIntervalSince1970
    return Int64(nowDouble*1000)
}

Working fine

Softlabsindia
  • 791
  • 6
  • 11
1

Simple one-line code to get time token in UInt64

let time = UInt64(Date().timeIntervalSince1970 * 1000)

print(time) <----- prints time in UInt64

Additional tip:

Looking for timestamp with 10 Digit milliseconds since 1970 for API call then

let timeStamp = Date().timeIntervalSince1970

print(timeStamp) <-- prints current time stamp

vilas deshmukh
  • 389
  • 2
  • 5