Friday, May 3, 2013

NSTimeZone setDefaultTimeZone example ios


setDefaultTimeZone:

Sets the default time zone for the current application to a given time zone.
+ (void)setDefaultTimeZone:(NSTimeZone *)aTimeZone
Parameters
aTimeZone
The new default time zone for the current application.
Discussion
There can be only one default time zone, so by setting a new default time zone, you lose the previous one.
Example of [NSTimeZone setDefaultTimeZone]
// Say that defaultTimeZone is originally GMT
NSTimeZone * myDefaultTZ = [NSTimeZone defaultTimeZone];
NSTimeZone * myLocalTZ = [NSTimeZone localTimeZone];
[NSTimeZone setDefaultTimeZone:[NSTimeZone timeZoneWithName:@"Etc/GMT-4"]];
NSLog(@"%@", myDefaultTZ);    // Still gives GMT
NSLog(@"%@", [NSTimeZone defaultTimeZone]);    // GMT-4, the new value
NSLog(@"%@", myLocalTZ);    // Also the new value!

Example of [NSTimeZone setDefaultTimeZone]
[NSTimeZone setDefaultTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"CEST"]];
NSLog(@"Paris time: %@", [[NSDate date] descriptionWithLocale:nil ]);
Example of [NSTimeZone setDefaultTimeZone]
NSDate* date = [NSDate date];
NSLog(@"%@", [date description]);

[NSTimeZone setDefaultTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"CST"]];
NSDate* nDate = [NSDate date];
NSLog(@"%@", [nDate description]);