Cheok Yan Cheng
Cheok Yan Cheng

Reputation: 42690

How to convert an epoch milliseconds timestamp in UInt64 to DispatchTime?

We have an epoch timestamp in UInt64 as input.

May I know, how can we convert it to DispatchTime

let timestampInMilliseconds: UInt64 = ...

// How we can convert timestampInMilliseconds to DispatchTime?
let dispatchTime: DispatchTime = ...

DispatchQueue.main.asyncAfter(deadline: dispatchTime) {
}

Upvotes: 0

Views: 818

Answers (1)

Sweeper
Sweeper

Reputation: 271260

First find out how many milliseconds is that time stamp since now:

let secondsSinceNow = Date(timeIntervalSince1970: TimeInterval(timestampInMilliseconds)).timeIntervalSinceNow
let millisSinceNow = Int(secondsSinceNow * 1000)

Then you can get your dispatch time by adding to .now():

let dispatchTime: DispatchTime = .now() + .milliseconds(millisSinceNow)

Upvotes: 2

Related Questions