Reputation: 6692
I wanna set the system timezone, date time in iOS by code. Any ideas or private apis help me? Example: Set time zone to GMT+8, and date time to Aug 10, 2013 8:30 pm. How to do it? Thanks!
Upvotes: 9
Views: 15051
Reputation: 4760
Still working on Xcode 11. The swift 5 implementation would look like:
override func setUpWithError() throws {
// Put setup code here. This method is called before the invocation of each test method in the class.
//All test by default will use GMT time zone. If different, change it within the func
TimeZone.ReferenceType.default = gmtTimeZone
}
override func tearDownWithError() throws {
// Put teardown code here. This method is called after the invocation of each test method in the class.
//Rest to current
TimeZone.ReferenceType.default = TimeZone.current
}
In my XCTestCase class I have my time zones are declared like:
private let gmtTimeZone = TimeZone(abbreviation: "GMT")!
private let gmtPlus1TimeZone = TimeZone(abbreviation: "GMT+1")!
private let gmtMinus1TimeZone = TimeZone(abbreviation: "GMT-1")!
I am defaulting all my test to GMT but I for a specific test I want to change it, then:
func test_Given_NotGMTTimeZone_ThenAssertToGMT() {
TimeZone.ReferenceType.default = gmtPlus1TimeZone
...
}
Added You can also work with time zone names like "British Summer Time"
private let britishSummerTimeZone = TimeZone(abbreviation: "BST")!
Added
This did not work for me due to time zone caching. I needed to reset the cache after each time zone change to stop the tests from interfering with each other. E.g.
TimeZone.ReferenceType.default = gmtTimeZone
TimeZone.ReferenceType.resetSystemTimeZone ()
Upvotes: 3
Reputation: 226
As the others have said you can't edit the system timezone from within an app, however you can set a default timezone for your entire application using +setDefaultTimeZone:
on NSTimeZone
.
You mentioned this was for testing, so I'm going to assume this is for unit testing some custom date formatters and such, in which case you could do something like this in your unit test:
static NSTimeZone *cachedTimeZone;
@implementation DateUtilTests
+ (void)setUp
{
[super setUp];
cachedTimeZone = [NSTimeZone defaultTimeZone];
// Set to whatever timezone you want your tests to run in
[NSTimeZone setDefaultTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
}
+ (void)tearDown
{
[NSTimeZone setDefaultTimeZone:cachedTimeZone];
[super tearDown];
}
// ... do some tests ...
@end
I hope that helps!
Upvotes: 21
Reputation: 11675
How @Filip said in comments, that's not possible to edit system preferences inside a app. What you might do (don't know if it will be useful for you) is set the timezone of the NSDates that you are working with, inside your APP.
That's an example of how to do that:
NSString *dateString = @"2013-08-07 17:49:54";
NSDateFormatter *dateFormatter = [NSDateFormatter new];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Europe/London"]; //set here the timezone you want
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
[dateFormatter setTimeZone:timeZone];
NSDate *date = [dateFormatter dateFromString:dateString];
Here's a list of possible timezones to be used with timeZoneWithName:` : http://pastebin.com/ibNU2RcG
Upvotes: 2