NSTimeZone is an abstract class that defines the behavior of time zone objects. Time zone objects represent geopolitical regions. Consequently, these objects have region names. Time zone objects also represent a temporal offset, either plus or minus, from Greenwich Mean Time (GMT) and an abbreviation (such as PST).
Time zones affect the values of date components that are calculated by calendar objects for a given NSDate object. You can create an NSTimeZone object and use it to set the time zone of an NSCalendar object. By default, NSCalendar uses the application's (or process's) default time zone when the calendar object is created, which itself defaults to the time zone set in System Preferences.
NSTimeZone provides several class methods to make time zone objects: timeZoneWithName:, timeZoneWithAbbreviation:, and timeZoneForSecondsFromGMT:. The most flexible method is timeZoneWithName:. The name passed to this method may be in any of the formats understood by the system, for example EST, Etc/GMT-2, America/Argentina/Buenos_Aires, Europe/Monaco, or US/Pacific, as shown in the following code fragment:
NSTimeZone *timeZoneEST = [NSTimeZone timeZoneWithName:@"EST"]; |
NSTimeZone *timeZoneBuenos_Aires = |
[NSTimeZone timeZoneWithName:@"America/Argentina/Buenos_Aires"]; |
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 use the knownTimeZoneNames class method:
NSArray *timeZoneNames = [NSTimeZone knownTimeZoneNames]; |
You can set the default time zone within your application using 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 on any computer on which it finds itself.
Last updated: 2009-07-21