Hieu Pham Hieu Pham - 1 month ago 10
iOS Question

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!


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 ...


I hope that helps!