NSTimeZone
is an abstract class that defines the behavior of time zone objects. Time zone objects represent geopolitical regions. Consequently, these objects have names for these regions. Time zone objects also represent a temporal offset, either plus or minus, from Greenwich Mean Time (GMT) and an abbreviation (such as “PST”).
NSTimeZone
provides several methods to make time zone objects. In Java, you use the constructors. In Objective-C, you use the class methods timeZoneWithName:
, timeZoneWithAbbreviation:
, timeZoneForSecondsFromGMT:
. The most flexible method is timeZoneWithName:
. The name may be in any of the formats understood by the system, for example "EST", "Etc/GMT-2", "America/Argentina/Buenos_Aires", "Europe/Monaco", "US/Pacific", or "posixrules", as shown in the following code fragment:
NSTimeZone *timeZoneEST = [NSTimeZone timeZoneWithName:@"EST"]; |
NSTimeZone *timeZoneBuenos_Aires = |
[NSTimeZone timeZoneWithName:@"America/Argentina/Buenos_Aires"]; |
NSTimeZone *timeZonePosix = |
[NSTimeZone timeZoneWithName:@"posixrules"]; |
If you use timeZoneWithAbbreviation:
, you can use only abbreviations such as "EST". In the following code fragment, timeZoneEST
will be initialized correctly, whereas timeZoneUSPacific
will be nil
.
NSTimeZone *timeZoneEST = [NSTimeZone timeZoneWithAbbreviation:@"EST"]; |
NSTimeZone *timeZoneUSPacific = |
[NSTimeZone timeZoneWithAbbreviation:@"US/Pacific"]; |
// timeZoneUSPacific = nil |
For a complete list of time zone names and abbreviations known to the system, you can see the output of CFTimeZoneCopyKnownNames
, as shown in the following code fragment:
#import <CoreFoundation/CoreFoundation.h> |
NSString *timeZoneInformation = (NSString *)CFTimeZoneCopyKnownNames(); |
NSLog(@"timeZoneInformation: %@", timeZoneInformation); |
The class also permits you to set the default time zone within your application (setDefaultTimeZone:
). You can access this default time zone at any time with the defaultTimeZone
class method, and with the localTimeZone
class method, you can get a relative time zone object that decodes itself to become the default time zone for any locale in which it finds itself.
Some Gregorian date methods return date objects that are automatically bound to time zone objects. These date objects use the functionality of NSTimeZone
to adjust dates for the proper locale. Unless you specify otherwise, objects returned from Gregorian date are bound to the default time zone for the current locale.
© 2002, 2007 Apple Inc. All Rights Reserved. (Last updated: 2007-09-04)