Swift - get current time on Windows

Hi, I'm trying to use time measurement in a test application on Windows. Therefore I use the DispatchTime.now() function like this:

let timeBefore = DispatchTime.now()

While searching online all examples used the function like this. Now I'm getting an error while compiling:

←[1msrc/main.swift:115:26: ←[0m←[0;1;31merror: ←[0m←[1mcannot find 'DispatchTime' in scope
←[0m        let timeBefore = DispatchTime.now()
←[0;1;32m                         ^~~~~~~~~~~~
←[0mNMAKE : fatal error U1077: "swiftc": Rückgabe-Code "0x1"

Is there something missing? Like an import? Or is this an issue with Windows and if that's the case, is there a workaround or other function to use?

Answered by DTS Engineer in 711531022

Like an import?

I’m hardly an Windows expert but, even on the Mac, using DispatchTime.now() requires you to import Dispatch. So, this code:

// import Dispatch

func main() {
    print(DispatchTime.now())
}

main()

won’t compile unless you uncomment the first line.

One gotcha here is that, on Apple platforms, Foundation re-exports Dispatch. So if you change the first line to this:

import Foundation

then things work. AFAIK that’s not the case on other platforms.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Accepted Answer

Like an import?

I’m hardly an Windows expert but, even on the Mac, using DispatchTime.now() requires you to import Dispatch. So, this code:

// import Dispatch

func main() {
    print(DispatchTime.now())
}

main()

won’t compile unless you uncomment the first line.

One gotcha here is that, on Apple platforms, Foundation re-exports Dispatch. So if you change the first line to this:

import Foundation

then things work. AFAIK that’s not the case on other platforms.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Swift - get current time on Windows
 
 
Q