defaultTimeZone
Returns the default time zone for the current application.
+ (NSTimeZone *)defaultTimeZone
Return Value
The default time zone for the current application. If no default time zone has been set, this method invokes
systemTimeZone
and returns the system time zone.Discussion of [NSTimeZone defaultTimeZone]
The default time zone is the one that the application is running with, which you can change (so you can make the application run as if it were in a different time zone).
If you get the default time zone and hold onto the returned object, it does not change if a subsequent invocation of
setDefaultTimeZone:
changes the default time zone—you still have the specific time zone you originally got. Contrast this behavior with the object returned by localTimeZone
.Example of [NSTimeZone defaultTimeZone]
-(NSDate *) dateToLocalTime
{
NSTimeZone *tz = [NSTimeZone defaultTimeZone];
NSInteger seconds = [tz secondsFromGMTForDate: self];
return [NSDate dateWithTimeInterval: seconds sinceDate: self];
}
-(NSDate *) dateToGlobalTime
{
NSTimeZone *tz = [NSTimeZone defaultTimeZone];
NSInteger seconds = -[tz secondsFromGMTForDate: self];
return [NSDate dateWithTimeInterval: seconds sinceDate: self];
}
Example of [NSTimeZone defaultTimeZone]
// 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!