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?

Accepted Reply

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"

  • First I got an different error after the import, because of an issue with the Swift installation. After running the installer again and repairing the swift version it worked, thanks!

  • Yay!

Add a Comment

Replies

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"

  • First I got an different error after the import, because of an issue with the Swift installation. After running the installer again and repairing the swift version it worked, thanks!

  • Yay!

Add a Comment