setDefaultTimeZone equivalent for testing

With `NSTimeZone` we could change the apps time zone with `setDefaultTimeZone` and run UI tests under various time zones.

With the Swift 3's `TimeZone` struct there's no equivalent functionality that I can see.

Any ideas on how we can continue testing our Swift 3 app under various time zones?

Accepted Reply

With the Swift 3's

TimeZone
struct there's no equivalent functionality that I can see.

The following is the logical equivalent of

-setDefaultTimeZone:
and it certainly compiles in Xcode 8:
import Foundation

NSTimeZone.default = NSTimeZone(forSecondsFromGMT: 0) as TimeZone

However, I’m not sure it’ll do what you need it to do. Please try it out in your environment and let us know how you get along.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Replies

With the Swift 3's

TimeZone
struct there's no equivalent functionality that I can see.

The following is the logical equivalent of

-setDefaultTimeZone:
and it certainly compiles in Xcode 8:
import Foundation

NSTimeZone.default = NSTimeZone(forSecondsFromGMT: 0) as TimeZone

However, I’m not sure it’ll do what you need it to do. Please try it out in your environment and let us know how you get along.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thanks eskimo, it worked 🙂. At least for now under Xcode 8.

Also directly instantiating a TimeZone and setting NSTimeZone.default to it, also works as expected.


Removing the setDefaultTimeZone method makes sense (in a normal running app it's an odd thing to do), but there should be something to still allow testing (more properly in the test setup than in TimeZone).

it worked

Yay!

Removing the

setDefaultTimeZone
method makes sense (in a normal running app it's an odd thing to do)

Agreed.

but there should be something to still allow testing

Agreed.

(more properly in the test setup than in TimeZone).

Agreed. I think this would make a fine enhancement request for XCTest. If you do file a bug, please post your bug number, just for the record.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thanks again eskimo. Filed an enhancement request with bug number 28856901.