Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the system timezone for iOS by code?

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!

like image 549
Hieu Pham Avatar asked Aug 08 '13 01:08

Hieu Pham


People also ask

What is default timezone?

US' Default Time Zone Unfortunately, there is no default time, so you have to change your time depending on the time zone used in the place.

What is timezone Swift?

2022 Time Zones - SwiftPST. UTC-8h.


1 Answers

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!

like image 84
justinm Avatar answered Sep 21 '22 15:09

justinm