1997-09-01 21:59:51 +00:00
|
|
|
/* Time zone management. -*- Mode: ObjC -*-
|
|
|
|
Copyright (C) 1997 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
Written by: Yoo C. Chung <wacko@laplace.snu.ac.kr>
|
|
|
|
Date: June 1997
|
|
|
|
|
|
|
|
This file is part of the GNUstep Base Library.
|
|
|
|
|
1997-10-17 13:35:52 +00:00
|
|
|
This library is free software; you can redistribute it and/or
|
1997-09-01 21:59:51 +00:00
|
|
|
modify it under the terms of the GNU Library General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2 of
|
|
|
|
the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful, but
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
1997-10-17 13:35:52 +00:00
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Library General Public License for more details.
|
1997-09-01 21:59:51 +00:00
|
|
|
|
1997-10-17 13:35:52 +00:00
|
|
|
You should have received a copy of the GNU Library General Public
|
1997-09-01 21:59:51 +00:00
|
|
|
License along with this library; if not, write to the Free Software
|
1999-08-25 16:12:36 +00:00
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
|
|
/* We use a implementation independent of the system, since POSIX
|
|
|
|
functions for time zones are woefully inadequate for implementing
|
|
|
|
NSTimeZone, and time zone names can be different from system to
|
|
|
|
system.
|
|
|
|
|
|
|
|
We do not use a dictionary for storing time zones, since such a
|
|
|
|
dictionary would be VERY large (~500K). And we would have to use a
|
|
|
|
complicated object determining whether we're using daylight savings
|
1997-11-03 14:30:13 +00:00
|
|
|
time and such for every entry in the dictionary. (Though we will
|
|
|
|
eventually have to change the implementation to prevent the year
|
|
|
|
2038 problem.)
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
|
|
The local time zone can be specified with the user defaults
|
1997-10-17 13:35:52 +00:00
|
|
|
database, the TZ environment variable, the file LOCAL_TIME_FILE, or
|
1997-10-24 16:58:20 +00:00
|
|
|
the fallback time zone (which is UTC), with the ones listed first
|
|
|
|
having precedence.
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
|
|
Any time zone must be a file name in ZONES_DIR.
|
|
|
|
|
|
|
|
FIXME?: use leap seconds? */
|
|
|
|
|
1997-11-06 00:51:23 +00:00
|
|
|
#include <config.h>
|
1998-12-20 21:27:47 +00:00
|
|
|
#include <base/preface.h>
|
1997-09-01 21:59:51 +00:00
|
|
|
#include <limits.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <Foundation/NSArray.h>
|
|
|
|
#include <Foundation/NSCoder.h>
|
1997-10-17 13:35:52 +00:00
|
|
|
#include <Foundation/NSDate.h>
|
|
|
|
#include <Foundation/NSDictionary.h>
|
1997-09-01 21:59:51 +00:00
|
|
|
#include <Foundation/NSException.h>
|
|
|
|
#include <Foundation/NSLock.h>
|
|
|
|
#include <Foundation/NSObject.h>
|
|
|
|
#include <Foundation/NSProcessInfo.h>
|
1997-10-17 13:35:52 +00:00
|
|
|
#include <Foundation/NSString.h>
|
1997-09-01 21:59:51 +00:00
|
|
|
#include <Foundation/NSUserDefaults.h>
|
|
|
|
#include <Foundation/NSUtilities.h>
|
|
|
|
#include <Foundation/NSZone.h>
|
1997-10-18 19:49:50 +00:00
|
|
|
#include <Foundation/NSBundle.h>
|
1999-08-25 16:12:36 +00:00
|
|
|
#include <Foundation/NSMapTable.h>
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
|
|
#define NOID
|
|
|
|
#include "tzfile.h"
|
|
|
|
|
|
|
|
|
|
|
|
/* Key for local time zone in user defaults. */
|
1997-11-28 20:13:58 +00:00
|
|
|
#define LOCALDBKEY @"Local Time Zone"
|
1995-08-23 16:13:42 +00:00
|
|
|
|
1997-09-01 21:59:51 +00:00
|
|
|
/* Directory that contains the time zone data. */
|
1999-04-23 02:54:45 +00:00
|
|
|
#define TIME_ZONE_DIR @"NSTimeZones"
|
1995-08-23 16:13:42 +00:00
|
|
|
|
1997-09-01 21:59:51 +00:00
|
|
|
/* Location of time zone abbreviation dictionary. It is a text file
|
|
|
|
with each line comprised of the abbreviation, a whitespace, and the
|
|
|
|
name. Neither the abbreviation nor the name can contain
|
|
|
|
whitespace, and each line must not be longer than 80 characters. */
|
1997-10-18 19:49:50 +00:00
|
|
|
#define ABBREV_DICT @"abbreviations"
|
1995-08-23 16:13:42 +00:00
|
|
|
|
1997-09-01 21:59:51 +00:00
|
|
|
/* File holding regions grouped by latitude. It is a text file with
|
|
|
|
each line comprised of the latitude region, whitespace, and the
|
1997-11-03 14:30:13 +00:00
|
|
|
name. Neither the abbreviation nor the name can contain
|
1997-09-01 21:59:51 +00:00
|
|
|
whitespace, and each line must not be longer than 80 characters. */
|
1997-10-18 19:49:50 +00:00
|
|
|
#define REGIONS_FILE @"regions"
|
1995-08-23 16:13:42 +00:00
|
|
|
|
1997-10-17 13:35:52 +00:00
|
|
|
/* Name of the file that contains the name of the local time zone. */
|
1997-10-18 19:49:50 +00:00
|
|
|
#define LOCAL_TIME_FILE @"localtime"
|
1997-10-17 13:35:52 +00:00
|
|
|
|
1997-09-01 21:59:51 +00:00
|
|
|
/* Directory that contains the actual time zones. */
|
1997-11-28 20:13:58 +00:00
|
|
|
#define ZONES_DIR @"zones/"
|
1995-08-23 16:13:42 +00:00
|
|
|
|
|
|
|
|
1997-09-01 21:59:51 +00:00
|
|
|
@class NSInternalTimeTransition;
|
|
|
|
@class NSConcreteTimeZone;
|
|
|
|
@class NSConcreteAbsoluteTimeZone;
|
|
|
|
@class NSConcreteTimeZoneDetail;
|
1995-08-23 16:13:42 +00:00
|
|
|
|
|
|
|
|
1997-09-01 21:59:51 +00:00
|
|
|
/* Temporary structure for holding time zone details. */
|
|
|
|
struct ttinfo
|
1995-08-23 16:13:42 +00:00
|
|
|
{
|
1997-09-01 21:59:51 +00:00
|
|
|
int offset; // Seconds east of UTC
|
|
|
|
BOOL isdst; // Daylight savings time?
|
|
|
|
char abbr_idx; // Index into time zone abbreviations string
|
|
|
|
};
|
1995-08-23 16:13:42 +00:00
|
|
|
|
|
|
|
|
1997-09-01 21:59:51 +00:00
|
|
|
/* The local time zone. */
|
|
|
|
static id localTimeZone;
|
1995-08-23 16:13:42 +00:00
|
|
|
|
1997-09-01 21:59:51 +00:00
|
|
|
/* Dictionary for time zones. Each time zone must have a unique
|
|
|
|
name. */
|
|
|
|
static NSMutableDictionary *zoneDictionary;
|
1995-08-23 16:13:42 +00:00
|
|
|
|
1997-09-01 21:59:51 +00:00
|
|
|
/* Fake one-to-one abbreviation to time zone name dictionary. */
|
|
|
|
static NSDictionary *fake_abbrev_dict;
|
1995-08-23 16:13:42 +00:00
|
|
|
|
1997-09-01 21:59:51 +00:00
|
|
|
/* Lock for creating time zones. */
|
1999-08-25 16:12:36 +00:00
|
|
|
static NSRecursiveLock *zone_mutex;
|
1995-08-23 16:13:42 +00:00
|
|
|
|
|
|
|
|
1997-09-01 21:59:51 +00:00
|
|
|
/* Decode the four bytes at PTR as a signed integer in network byte order.
|
|
|
|
Based on code included in the GNU C Library 2.0.3. */
|
|
|
|
static inline int
|
|
|
|
decode (const void *ptr)
|
1995-08-23 16:13:42 +00:00
|
|
|
{
|
1997-09-01 21:59:51 +00:00
|
|
|
#if defined(WORDS_BIGENDIAN) && SIZEOF_INT == 4
|
|
|
|
return *(const int *) ptr;
|
|
|
|
#else /* defined(WORDS_BIGENDIAN) && SIZEOF_INT == 4 */
|
|
|
|
const unsigned char *p = ptr;
|
|
|
|
int result = *p & (1 << (CHAR_BIT - 1)) ? ~0 : 0;
|
|
|
|
|
|
|
|
result = (result << 8) | *p++;
|
|
|
|
result = (result << 8) | *p++;
|
|
|
|
result = (result << 8) | *p++;
|
|
|
|
result = (result << 8) | *p++;
|
|
|
|
return result;
|
|
|
|
#endif /* defined(WORDS_BIGENDIAN) && SIZEOF_INT == 4 */
|
|
|
|
}
|
1995-08-23 16:13:42 +00:00
|
|
|
|
|
|
|
|
1997-11-03 14:30:13 +00:00
|
|
|
/* Object enumerator for NSInternalAbbrevDict. */
|
|
|
|
@interface NSInternalAbbrevDictObjectEnumerator : NSEnumerator
|
|
|
|
{
|
|
|
|
NSEnumerator *dict_enum;
|
|
|
|
}
|
|
|
|
|
1999-08-25 16:12:36 +00:00
|
|
|
- (id) initWithDict: (NSDictionary*)aDict;
|
1997-11-03 14:30:13 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
|
1997-09-01 21:59:51 +00:00
|
|
|
/* Front end that actually uses [NSTimeZone abbrebiationMap]. */
|
|
|
|
@interface NSInternalAbbrevDict : NSDictionary
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
@interface NSInternalTimeTransition : NSObject
|
1997-10-17 13:35:52 +00:00
|
|
|
{
|
1997-09-01 21:59:51 +00:00
|
|
|
int trans_time; // When the transition occurs
|
|
|
|
char detail_index; // Index of time zone detail
|
1997-10-17 13:35:52 +00:00
|
|
|
}
|
1997-09-01 21:59:51 +00:00
|
|
|
|
1999-08-25 16:12:36 +00:00
|
|
|
- (id) initWithTime: (int)aTime withIndex: (char)anIndex;
|
|
|
|
- (int) transTime;
|
|
|
|
- (char) detailIndex;
|
1997-09-01 21:59:51 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
@interface NSConcreteTimeZone : NSTimeZone
|
|
|
|
{
|
|
|
|
NSString *name;
|
|
|
|
NSArray *transitions; // Transition times and rules
|
|
|
|
NSArray *details; // Time zone details
|
1995-08-23 16:13:42 +00:00
|
|
|
}
|
1997-09-01 21:59:51 +00:00
|
|
|
|
1999-08-25 16:12:36 +00:00
|
|
|
- (id) initWithName: (NSString*)aName
|
|
|
|
withTransitions: (NSArray*)trans
|
|
|
|
withDetails: (NSArray*)zoneDetails;
|
1997-10-17 13:35:52 +00:00
|
|
|
@end
|
1997-09-01 21:59:51 +00:00
|
|
|
|
1995-08-23 16:13:42 +00:00
|
|
|
|
1997-09-01 21:59:51 +00:00
|
|
|
@interface NSConcreteAbsoluteTimeZone : NSTimeZone
|
1995-08-23 16:13:42 +00:00
|
|
|
{
|
1997-09-01 21:59:51 +00:00
|
|
|
NSString *name;
|
1997-10-17 13:35:52 +00:00
|
|
|
id detail;
|
1997-09-01 21:59:51 +00:00
|
|
|
int offset; // Offset from UTC in seconds.
|
1995-08-23 16:13:42 +00:00
|
|
|
}
|
|
|
|
|
1999-08-25 16:12:36 +00:00
|
|
|
- (id) initWithOffset: (int)anOffset;
|
1997-10-17 13:35:52 +00:00
|
|
|
@end
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
@interface NSConcreteTimeZoneDetail : NSTimeZoneDetail
|
1995-08-23 16:13:42 +00:00
|
|
|
{
|
1997-10-24 16:58:20 +00:00
|
|
|
NSTimeZone *timeZone; // Time zone which created this object.
|
1997-09-01 21:59:51 +00:00
|
|
|
NSString *abbrev; // Abbreviation for time zone detail.
|
|
|
|
int offset; // Offset from UTC in seconds.
|
|
|
|
BOOL is_dst; // Is it daylight savings time?
|
1995-08-23 16:13:42 +00:00
|
|
|
}
|
1997-09-01 21:59:51 +00:00
|
|
|
|
1999-08-25 16:12:36 +00:00
|
|
|
- (id) initWithTimeZone: (NSTimeZone*)aZone
|
|
|
|
withAbbrev: (NSString*)anAbbrev
|
|
|
|
withOffset: (int)anOffset
|
|
|
|
withDST: (BOOL)isDST;
|
1997-10-17 13:35:52 +00:00
|
|
|
@end
|
1997-09-01 21:59:51 +00:00
|
|
|
|
1997-11-03 14:30:13 +00:00
|
|
|
/* Private methods for obtaining resource file names. */
|
|
|
|
@interface NSTimeZone (Private)
|
1999-08-25 16:12:36 +00:00
|
|
|
+ (NSString*) getAbbreviationFile;
|
|
|
|
+ (NSString*) getRegionsFile;
|
|
|
|
+ (NSString*) getLocalTimeFile;
|
|
|
|
+ (NSString*) getTimeZoneFile: (NSString*)name;
|
1997-11-03 14:30:13 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
@implementation NSInternalAbbrevDictObjectEnumerator
|
|
|
|
|
1999-05-27 09:52:49 +00:00
|
|
|
- (void) dealloc
|
1997-11-03 14:30:13 +00:00
|
|
|
{
|
1999-05-27 09:52:49 +00:00
|
|
|
RELEASE(dict_enum);
|
1997-11-03 14:30:13 +00:00
|
|
|
}
|
|
|
|
|
1999-05-27 09:52:49 +00:00
|
|
|
- (id) initWithDict: (NSDictionary*)aDict
|
1997-11-03 14:30:13 +00:00
|
|
|
{
|
1999-05-27 09:52:49 +00:00
|
|
|
dict_enum = RETAIN([aDict objectEnumerator]);
|
1997-11-03 14:30:13 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
1999-05-27 09:52:49 +00:00
|
|
|
- (id) nextObject
|
1997-11-03 14:30:13 +00:00
|
|
|
{
|
|
|
|
id object;
|
|
|
|
|
|
|
|
object = [dict_enum nextObject];
|
|
|
|
if (object != nil)
|
|
|
|
return [object objectAtIndex: 0];
|
|
|
|
else
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
1997-09-01 21:59:51 +00:00
|
|
|
@implementation NSInternalAbbrevDict
|
1997-11-03 14:30:13 +00:00
|
|
|
|
1999-08-25 16:12:36 +00:00
|
|
|
+ (id) allocWithZone: (NSZone*)zone
|
1995-08-23 16:13:42 +00:00
|
|
|
{
|
1997-09-01 21:59:51 +00:00
|
|
|
return NSAllocateObject(self, 0, zone);
|
1997-10-17 13:35:52 +00:00
|
|
|
}
|
1997-11-03 14:30:13 +00:00
|
|
|
|
1999-05-27 09:52:49 +00:00
|
|
|
- (id) init
|
1997-10-17 13:35:52 +00:00
|
|
|
{
|
1997-09-01 21:59:51 +00:00
|
|
|
return self;
|
1997-10-17 13:35:52 +00:00
|
|
|
}
|
1997-11-03 14:30:13 +00:00
|
|
|
|
1999-08-25 16:12:36 +00:00
|
|
|
- (unsigned) count
|
1997-10-17 13:35:52 +00:00
|
|
|
{
|
1997-09-01 21:59:51 +00:00
|
|
|
return [[NSTimeZone abbreviationMap] count];
|
1997-10-17 13:35:52 +00:00
|
|
|
}
|
1997-09-01 21:59:51 +00:00
|
|
|
|
1999-05-27 09:52:49 +00:00
|
|
|
- (NSEnumerator*) keyEnumerator
|
1997-10-17 13:35:52 +00:00
|
|
|
{
|
1997-09-01 21:59:51 +00:00
|
|
|
return [[NSTimeZone abbreviationMap] keyEnumerator];
|
1997-10-17 13:35:52 +00:00
|
|
|
}
|
1997-11-03 14:30:13 +00:00
|
|
|
|
1999-05-27 09:52:49 +00:00
|
|
|
- (NSEnumerator*) objectEnumerator
|
1997-10-17 13:35:52 +00:00
|
|
|
{
|
1999-05-27 09:52:49 +00:00
|
|
|
return AUTORELEASE([[NSInternalAbbrevDictObjectEnumerator alloc]
|
|
|
|
initWithDict: [NSTimeZone abbreviationMap]]);
|
1997-10-17 13:35:52 +00:00
|
|
|
}
|
1997-09-01 21:59:51 +00:00
|
|
|
|
1999-05-27 09:52:49 +00:00
|
|
|
- (id) objectForKey: (NSString*)key
|
1997-10-17 13:35:52 +00:00
|
|
|
{
|
1997-09-01 21:59:51 +00:00
|
|
|
return [[[NSTimeZone abbreviationMap] objectForKey: key] objectAtIndex: 0];
|
1997-10-17 13:35:52 +00:00
|
|
|
}
|
1997-09-01 21:59:51 +00:00
|
|
|
|
1997-10-17 13:35:52 +00:00
|
|
|
@end
|
1997-11-03 14:30:13 +00:00
|
|
|
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
|
|
@implementation NSInternalTimeTransition
|
|
|
|
|
1999-05-27 09:52:49 +00:00
|
|
|
- (NSString*) description
|
1997-10-17 13:35:52 +00:00
|
|
|
{
|
1999-08-25 16:12:36 +00:00
|
|
|
return [NSString stringWithFormat: @"%@(%d, %d)",
|
|
|
|
[self class], trans_time, (int)detail_index];
|
1995-08-23 16:13:42 +00:00
|
|
|
}
|
|
|
|
|
1999-08-25 16:12:36 +00:00
|
|
|
- (id) initWithTime: (int)aTime withIndex: (char)anIndex
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
|
|
|
[super init];
|
|
|
|
trans_time = aTime;
|
|
|
|
detail_index = anIndex;
|
|
|
|
return self;
|
1997-10-17 13:35:52 +00:00
|
|
|
}
|
1997-11-03 14:30:13 +00:00
|
|
|
|
1999-08-25 16:12:36 +00:00
|
|
|
- (int) transTime
|
1997-10-17 13:35:52 +00:00
|
|
|
{
|
1997-09-01 21:59:51 +00:00
|
|
|
return trans_time;
|
1997-10-17 13:35:52 +00:00
|
|
|
}
|
1997-11-03 14:30:13 +00:00
|
|
|
|
1999-08-25 16:12:36 +00:00
|
|
|
- (char) detailIndex
|
1997-10-17 13:35:52 +00:00
|
|
|
{
|
1997-09-01 21:59:51 +00:00
|
|
|
return detail_index;
|
1997-10-17 13:35:52 +00:00
|
|
|
}
|
1997-09-01 21:59:51 +00:00
|
|
|
|
1997-10-17 13:35:52 +00:00
|
|
|
@end
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
@implementation NSConcreteTimeZone
|
|
|
|
|
1999-05-27 09:52:49 +00:00
|
|
|
- (id) initWithName: (NSString*)aName
|
|
|
|
withTransitions: (NSArray*)trans
|
|
|
|
withDetails: (NSArray*)zoneDetails
|
1997-10-17 13:35:52 +00:00
|
|
|
{
|
1999-08-25 16:12:36 +00:00
|
|
|
NSZone *z;
|
|
|
|
|
1997-09-01 21:59:51 +00:00
|
|
|
[super init];
|
1999-08-25 16:12:36 +00:00
|
|
|
z = [self zone];
|
|
|
|
name = [aName copyWithZone: z];
|
1999-05-27 09:52:49 +00:00
|
|
|
transitions = RETAIN(trans);
|
|
|
|
details = RETAIN(zoneDetails);
|
1997-09-01 21:59:51 +00:00
|
|
|
return self;
|
1997-10-17 13:35:52 +00:00
|
|
|
}
|
1997-11-03 14:30:13 +00:00
|
|
|
|
1999-08-25 16:12:36 +00:00
|
|
|
- (void) dealloc
|
1997-10-17 13:35:52 +00:00
|
|
|
{
|
1999-05-27 09:52:49 +00:00
|
|
|
RELEASE(name);
|
|
|
|
RELEASE(transitions);
|
|
|
|
RELEASE(details);
|
1997-09-01 21:59:51 +00:00
|
|
|
[super dealloc];
|
1997-10-17 13:35:52 +00:00
|
|
|
}
|
1997-11-03 14:30:13 +00:00
|
|
|
|
1999-05-27 09:52:49 +00:00
|
|
|
- (void) encodeWithCoder: (NSCoder*)aCoder
|
1995-08-23 16:13:42 +00:00
|
|
|
{
|
1997-09-01 21:59:51 +00:00
|
|
|
[super encodeWithCoder: aCoder];
|
1998-03-12 14:21:20 +00:00
|
|
|
if (self == localTimeZone)
|
|
|
|
[aCoder encodeObject: @"NSLocalTimeZone"];
|
|
|
|
else
|
|
|
|
[aCoder encodeObject: name];
|
|
|
|
}
|
|
|
|
|
1999-05-27 09:52:49 +00:00
|
|
|
- (id) awakeAfterUsingCoder: (NSCoder*)aCoder
|
1998-03-12 14:21:20 +00:00
|
|
|
{
|
1999-05-27 09:52:49 +00:00
|
|
|
if ([name isEqual: @"NSLocalTimeZone"])
|
|
|
|
{
|
|
|
|
return localTimeZone;
|
|
|
|
}
|
1998-03-12 14:21:20 +00:00
|
|
|
return [NSTimeZone timeZoneWithName: name];
|
1995-08-23 16:13:42 +00:00
|
|
|
}
|
|
|
|
|
1999-05-27 09:52:49 +00:00
|
|
|
- (id) initWithDecoder: (NSCoder*)aDecoder
|
1997-10-17 13:35:52 +00:00
|
|
|
{
|
1997-09-01 21:59:51 +00:00
|
|
|
self = [super initWithCoder: aDecoder];
|
1998-09-02 12:34:38 +00:00
|
|
|
[aDecoder decodeValueOfObjCType: @encode(id) at: &name];
|
1998-03-12 14:21:20 +00:00
|
|
|
return self;
|
1997-10-17 13:35:52 +00:00
|
|
|
}
|
1997-11-03 14:30:13 +00:00
|
|
|
|
1999-05-27 09:52:49 +00:00
|
|
|
- (NSTimeZoneDetail*) timeZoneDetailForDate: (NSDate*)date
|
1995-08-23 16:13:42 +00:00
|
|
|
{
|
1997-09-01 21:59:51 +00:00
|
|
|
unsigned index;
|
|
|
|
int the_time;
|
|
|
|
unsigned count;
|
|
|
|
|
|
|
|
the_time = (int)[date timeIntervalSince1970];
|
|
|
|
count = [transitions count];
|
|
|
|
if (count == 0
|
|
|
|
|| the_time < [[transitions objectAtIndex: 0] transTime])
|
|
|
|
/* Either DATE is before any transitions or there is no transition.
|
|
|
|
Return the first non-DST type, or the first one if they are all DST. */
|
|
|
|
{
|
|
|
|
unsigned detail_count;
|
|
|
|
|
|
|
|
detail_count = [details count];
|
|
|
|
index = 0;
|
|
|
|
while (index < detail_count
|
1999-05-27 09:52:49 +00:00
|
|
|
&& [[details objectAtIndex: index] isDaylightSavingTimeZone])
|
1997-09-01 21:59:51 +00:00
|
|
|
index++;
|
|
|
|
if (index == detail_count)
|
|
|
|
index = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
/* Find the first transition after DATE, and then pick the type of
|
|
|
|
the transition before it. */
|
|
|
|
{
|
|
|
|
for (index = 1; index < count; index++)
|
|
|
|
if (the_time < [[transitions objectAtIndex: index] transTime])
|
|
|
|
break;
|
|
|
|
index = [[transitions objectAtIndex: index-1] detailIndex];
|
|
|
|
}
|
|
|
|
return [details objectAtIndex: index];
|
1995-08-23 16:13:42 +00:00
|
|
|
}
|
1997-09-01 21:59:51 +00:00
|
|
|
|
1999-05-27 09:52:49 +00:00
|
|
|
- (NSArray*) timeZoneDetailArray
|
1995-08-23 16:13:42 +00:00
|
|
|
{
|
1997-09-01 21:59:51 +00:00
|
|
|
return details;
|
1997-10-17 13:35:52 +00:00
|
|
|
}
|
1997-09-01 21:59:51 +00:00
|
|
|
|
1999-08-25 16:12:36 +00:00
|
|
|
- (NSString*) timeZoneName
|
1997-10-17 13:35:52 +00:00
|
|
|
{
|
1997-09-01 21:59:51 +00:00
|
|
|
return name;
|
1997-10-17 13:35:52 +00:00
|
|
|
}
|
1997-09-01 21:59:51 +00:00
|
|
|
|
1997-10-17 13:35:52 +00:00
|
|
|
@end
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
@implementation NSConcreteAbsoluteTimeZone
|
1997-11-03 14:30:13 +00:00
|
|
|
|
1999-08-25 16:12:36 +00:00
|
|
|
static NSMapTable *absolutes = 0;
|
|
|
|
|
|
|
|
+ (void) initialize
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
1999-08-25 16:12:36 +00:00
|
|
|
if (self == [NSConcreteAbsoluteTimeZone class])
|
|
|
|
{
|
|
|
|
absolutes = NSCreateMapTable(NSIntMapKeyCallBacks,
|
|
|
|
NSNonOwnedPointerMapValueCallBacks, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initWithOffset: (int)anOffset
|
|
|
|
{
|
|
|
|
NSConcreteAbsoluteTimeZone *z;
|
|
|
|
|
|
|
|
[zone_mutex lock];
|
|
|
|
z = (NSConcreteAbsoluteTimeZone*)NSMapGet(absolutes, (void*)(gsaddr)anOffset);
|
|
|
|
if (z)
|
|
|
|
{
|
|
|
|
RETAIN(z);
|
|
|
|
RELEASE(self);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[super init];
|
|
|
|
name = [[NSString alloc] initWithFormat: @"%d", anOffset];
|
|
|
|
detail = [[NSConcreteTimeZoneDetail alloc]
|
|
|
|
initWithTimeZone: self withAbbrev: name
|
|
|
|
withOffset: anOffset withDST: NO];
|
|
|
|
offset = anOffset;
|
|
|
|
z = self;
|
|
|
|
NSMapInsert(absolutes, (void*)(gsaddr)anOffset, (void*)z);
|
|
|
|
}
|
|
|
|
[zone_mutex unlock];
|
|
|
|
return z;
|
1997-10-17 13:35:52 +00:00
|
|
|
}
|
1997-11-03 14:30:13 +00:00
|
|
|
|
1999-08-25 16:12:36 +00:00
|
|
|
- (void) dealloc
|
1997-10-17 13:35:52 +00:00
|
|
|
{
|
1999-08-25 16:12:36 +00:00
|
|
|
[zone_mutex lock];
|
|
|
|
NSMapRemove(absolutes, (void*)(gsaddr)offset);
|
|
|
|
[zone_mutex unlock];
|
1999-05-27 09:52:49 +00:00
|
|
|
RELEASE(name);
|
|
|
|
RELEASE(detail);
|
1997-09-01 21:59:51 +00:00
|
|
|
[super dealloc];
|
1997-10-17 13:35:52 +00:00
|
|
|
}
|
1997-11-03 14:30:13 +00:00
|
|
|
|
1999-08-25 16:12:36 +00:00
|
|
|
- (void) encodeWithCoder: aCoder
|
1995-08-23 16:13:42 +00:00
|
|
|
{
|
1997-09-01 21:59:51 +00:00
|
|
|
[super encodeWithCoder: aCoder];
|
|
|
|
[aCoder encodeObject: name];
|
1995-08-23 16:13:42 +00:00
|
|
|
}
|
1997-11-03 14:30:13 +00:00
|
|
|
|
1999-08-25 16:12:36 +00:00
|
|
|
- (id) initWithCoder: aDecoder
|
1997-10-17 13:35:52 +00:00
|
|
|
{
|
1997-09-01 21:59:51 +00:00
|
|
|
self = [super initWithCoder: aDecoder];
|
1998-09-02 12:34:38 +00:00
|
|
|
[aDecoder decodeValueOfObjCType: @encode(id) at: &name];
|
1997-09-01 21:59:51 +00:00
|
|
|
offset = [name intValue];
|
1999-08-25 16:12:36 +00:00
|
|
|
detail = [[NSConcreteTimeZoneDetail alloc]
|
|
|
|
initWithTimeZone: self withAbbrev: name
|
|
|
|
withOffset: offset withDST: NO];
|
1997-09-01 21:59:51 +00:00
|
|
|
return self;
|
|
|
|
}
|
1997-11-03 14:30:13 +00:00
|
|
|
|
1999-08-25 16:12:36 +00:00
|
|
|
- (NSTimeZoneDetail*) timeZoneDetailForDate: (NSDate*)date
|
1997-10-17 13:35:52 +00:00
|
|
|
{
|
|
|
|
return detail;
|
1995-08-23 16:13:42 +00:00
|
|
|
}
|
1997-09-01 21:59:51 +00:00
|
|
|
|
1999-08-25 16:12:36 +00:00
|
|
|
- (NSString*) timeZoneName
|
1995-08-23 16:13:42 +00:00
|
|
|
{
|
1997-09-01 21:59:51 +00:00
|
|
|
return name;
|
1997-10-17 13:35:52 +00:00
|
|
|
}
|
1997-09-01 21:59:51 +00:00
|
|
|
|
1999-08-25 16:12:36 +00:00
|
|
|
- (NSArray*) timeZoneDetailArray
|
1997-10-17 13:35:52 +00:00
|
|
|
{
|
|
|
|
return [NSArray arrayWithObject: detail];
|
1995-08-23 16:13:42 +00:00
|
|
|
}
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
@implementation NSConcreteTimeZoneDetail
|
|
|
|
|
1999-08-25 16:12:36 +00:00
|
|
|
- (id) initWithTimeZone: (NSTimeZone*)aZone
|
|
|
|
withAbbrev: (NSString*)anAbbrev
|
|
|
|
withOffset: (int)anOffset
|
|
|
|
withDST: (BOOL)isDST
|
1997-10-17 13:35:52 +00:00
|
|
|
{
|
1997-09-01 21:59:51 +00:00
|
|
|
[super init];
|
1999-05-27 09:52:49 +00:00
|
|
|
timeZone = RETAIN(aZone);
|
|
|
|
abbrev = RETAIN(anAbbrev);
|
1997-09-01 21:59:51 +00:00
|
|
|
offset = anOffset;
|
|
|
|
is_dst = isDST;
|
|
|
|
return self;
|
1997-10-17 13:35:52 +00:00
|
|
|
}
|
1997-09-01 21:59:51 +00:00
|
|
|
|
1999-08-25 16:12:36 +00:00
|
|
|
- (void) dealloc
|
1997-10-17 13:35:52 +00:00
|
|
|
{
|
1999-05-27 09:52:49 +00:00
|
|
|
RELEASE(timeZone);
|
|
|
|
RELEASE(abbrev);
|
1997-09-01 21:59:51 +00:00
|
|
|
[super dealloc];
|
|
|
|
}
|
1997-11-03 14:30:13 +00:00
|
|
|
|
1999-08-25 16:12:36 +00:00
|
|
|
- (void) encodeWithCoder: aCoder
|
1995-08-23 16:13:42 +00:00
|
|
|
{
|
1997-09-01 21:59:51 +00:00
|
|
|
[super encodeWithCoder: aCoder];
|
|
|
|
[aCoder encodeObject: abbrev];
|
|
|
|
[aCoder encodeValueOfObjCType: @encode(int) at: &offset];
|
|
|
|
[aCoder encodeValueOfObjCType: @encode(BOOL) at: &is_dst];
|
1995-08-23 16:13:42 +00:00
|
|
|
}
|
1997-11-03 14:30:13 +00:00
|
|
|
|
1997-09-01 21:59:51 +00:00
|
|
|
- initWithCoder: aDecoder
|
1995-08-23 16:13:42 +00:00
|
|
|
{
|
1997-09-01 21:59:51 +00:00
|
|
|
self = [super initWithCoder: aDecoder];
|
1998-09-02 12:34:38 +00:00
|
|
|
[aDecoder decodeValueOfObjCType: @encode(id) at: &abbrev];
|
1997-09-01 21:59:51 +00:00
|
|
|
[aDecoder decodeValueOfObjCType: @encode(int) at: &offset];
|
|
|
|
[aDecoder decodeValueOfObjCType: @encode(BOOL) at: &is_dst];
|
|
|
|
return self;
|
1997-10-17 13:35:52 +00:00
|
|
|
}
|
1997-09-01 21:59:51 +00:00
|
|
|
|
1999-08-25 16:12:36 +00:00
|
|
|
- (NSTimeZoneDetail*) timeZoneDetailForDate: (NSDate*)date
|
1997-10-24 16:58:20 +00:00
|
|
|
{
|
|
|
|
return [timeZone timeZoneDetailForDate: date];
|
|
|
|
}
|
1997-11-03 14:30:13 +00:00
|
|
|
|
1999-08-25 16:12:36 +00:00
|
|
|
- (NSString*) timeZoneName
|
1997-10-24 16:58:20 +00:00
|
|
|
{
|
|
|
|
return [timeZone timeZoneName];
|
|
|
|
}
|
|
|
|
|
1999-08-25 16:12:36 +00:00
|
|
|
- (NSArray*) timeZoneDetailArray
|
1997-10-24 16:58:20 +00:00
|
|
|
{
|
|
|
|
return [timeZone timeZoneDetailArray];
|
|
|
|
}
|
|
|
|
|
1999-08-25 16:12:36 +00:00
|
|
|
- (BOOL) isDaylightSavingTimeZone
|
1997-10-17 13:35:52 +00:00
|
|
|
{
|
1997-09-01 21:59:51 +00:00
|
|
|
return is_dst;
|
1995-08-23 16:13:42 +00:00
|
|
|
}
|
1997-09-01 21:59:51 +00:00
|
|
|
|
1999-08-25 16:12:36 +00:00
|
|
|
- (NSString*) timeZoneAbbreviation
|
1995-08-23 16:13:42 +00:00
|
|
|
{
|
1997-09-01 21:59:51 +00:00
|
|
|
return abbrev;
|
1995-08-23 16:13:42 +00:00
|
|
|
}
|
1997-09-01 21:59:51 +00:00
|
|
|
|
1999-08-25 16:12:36 +00:00
|
|
|
- (int) timeZoneSecondsFromGMT
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
|
|
|
return offset;
|
1997-10-17 13:35:52 +00:00
|
|
|
}
|
1997-09-01 21:59:51 +00:00
|
|
|
|
1995-08-23 16:13:42 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
|
1997-09-01 21:59:51 +00:00
|
|
|
@implementation NSTimeZone
|
1995-08-23 16:13:42 +00:00
|
|
|
|
1999-08-25 16:12:36 +00:00
|
|
|
+ (void) initialize
|
1997-10-17 13:35:52 +00:00
|
|
|
{
|
1997-09-01 21:59:51 +00:00
|
|
|
if (self == [NSTimeZone class])
|
|
|
|
{
|
|
|
|
id localZoneString = nil;
|
1995-08-23 16:13:42 +00:00
|
|
|
|
1999-08-25 16:12:36 +00:00
|
|
|
zone_mutex = [NSRecursiveLock new];
|
1997-11-03 14:30:13 +00:00
|
|
|
zoneDictionary = [[NSMutableDictionary alloc] init];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
|
|
localZoneString = [[NSUserDefaults standardUserDefaults]
|
1997-11-28 20:13:58 +00:00
|
|
|
stringForKey: LOCALDBKEY];
|
1997-09-01 21:59:51 +00:00
|
|
|
if (localZoneString == nil)
|
1997-10-17 13:35:52 +00:00
|
|
|
/* Try to get timezone from environment. */
|
1997-09-01 21:59:51 +00:00
|
|
|
localZoneString = [[[NSProcessInfo processInfo]
|
|
|
|
environment] objectForKey: @"TZ"];
|
1997-10-17 13:35:52 +00:00
|
|
|
if (localZoneString == nil)
|
|
|
|
/* Try to get timezone from LOCAL_TIME_FILE. */
|
|
|
|
{
|
1997-10-18 19:49:50 +00:00
|
|
|
NSString *f = [NSTimeZone getLocalTimeFile];
|
1997-10-17 13:35:52 +00:00
|
|
|
char zone_name[80];
|
|
|
|
FILE *fp;
|
|
|
|
|
1997-10-18 19:49:50 +00:00
|
|
|
if (f)
|
|
|
|
{
|
1999-05-11 09:21:38 +00:00
|
|
|
#if defined(__WIN32__)
|
|
|
|
fp = fopen([f fileSystemRepresentation], "rb");
|
|
|
|
#else
|
|
|
|
fp = fopen([f fileSystemRepresentation], "r");
|
|
|
|
#endif
|
1997-10-18 19:49:50 +00:00
|
|
|
if (fp != NULL)
|
|
|
|
{
|
1997-10-24 16:58:20 +00:00
|
|
|
if (fscanf(fp, "%79s", zone_name) == 1)
|
|
|
|
localZoneString = [NSString stringWithCString: zone_name];
|
1997-10-18 19:49:50 +00:00
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
}
|
1997-10-17 13:35:52 +00:00
|
|
|
}
|
1997-09-01 21:59:51 +00:00
|
|
|
if (localZoneString != nil)
|
|
|
|
localTimeZone = [NSTimeZone timeZoneWithName: localZoneString];
|
|
|
|
else
|
1997-10-24 16:58:20 +00:00
|
|
|
NSLog(@"No local time zone specified.");
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
|
|
/* If local time zone fails to allocate, then allocate something
|
|
|
|
that is sure to succeed (unless we run out of memory, of
|
|
|
|
course). */
|
|
|
|
if (localTimeZone == nil)
|
1997-10-24 16:58:20 +00:00
|
|
|
{
|
1997-11-03 14:30:13 +00:00
|
|
|
NSLog(@"Using time zone with absolute offset 0.");
|
1997-10-24 16:58:20 +00:00
|
|
|
localTimeZone = [NSTimeZone timeZoneForSecondsFromGMT: 0];
|
|
|
|
}
|
1999-08-25 16:12:36 +00:00
|
|
|
RETAIN(localTimeZone);
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
|
|
fake_abbrev_dict = [[NSInternalAbbrevDict alloc] init];
|
|
|
|
}
|
1995-08-23 16:13:42 +00:00
|
|
|
}
|
|
|
|
|
1999-08-25 16:12:36 +00:00
|
|
|
- (NSString*) description
|
1997-11-03 14:30:13 +00:00
|
|
|
{
|
|
|
|
return [self timeZoneName];
|
|
|
|
}
|
|
|
|
|
1999-08-25 16:12:36 +00:00
|
|
|
+ (NSTimeZoneDetail*) defaultTimeZone
|
1995-08-23 16:13:42 +00:00
|
|
|
{
|
1997-09-01 21:59:51 +00:00
|
|
|
return [localTimeZone timeZoneDetailForDate: [NSDate date]];
|
1995-08-23 16:13:42 +00:00
|
|
|
}
|
|
|
|
|
1999-08-25 16:12:36 +00:00
|
|
|
+ (NSTimeZone*) localTimeZone
|
1995-08-23 16:13:42 +00:00
|
|
|
{
|
1997-09-01 21:59:51 +00:00
|
|
|
return localTimeZone;
|
1995-08-23 16:13:42 +00:00
|
|
|
}
|
|
|
|
|
1999-08-25 16:12:36 +00:00
|
|
|
+ (NSTimeZone*) timeZoneForSecondsFromGMT: (int)seconds
|
1995-08-23 16:13:42 +00:00
|
|
|
{
|
1997-09-01 21:59:51 +00:00
|
|
|
/* We simply return the following because an existing time zone with
|
|
|
|
the given offset might not always have the same offset (daylight
|
|
|
|
savings time, change in standard time, etc.). */
|
1999-08-25 16:12:36 +00:00
|
|
|
return AUTORELEASE([[NSConcreteAbsoluteTimeZone alloc] initWithOffset: seconds]);
|
1995-08-23 16:13:42 +00:00
|
|
|
}
|
|
|
|
|
1999-08-25 16:12:36 +00:00
|
|
|
+ (NSTimeZoneDetail*) timeZoneWithAbbreviation: (NSString*)abbreviation
|
1995-08-23 16:13:42 +00:00
|
|
|
{
|
1997-09-01 21:59:51 +00:00
|
|
|
/* We obtain a time zone from the abbreviation dictionary, get the
|
|
|
|
time zone detail array, and then obtain the time zone detail we
|
|
|
|
want from that array. This convulated and twisted method is used
|
|
|
|
because there is no way to directly obtain the time zone
|
|
|
|
detail. */
|
|
|
|
id zone, detailArray, e, object;
|
|
|
|
|
|
|
|
zone = [self timeZoneWithName: [[self abbreviationDictionary]
|
|
|
|
objectForKey: abbreviation]];
|
|
|
|
if (zone == nil)
|
|
|
|
return nil;
|
|
|
|
detailArray = [zone timeZoneDetailArray];
|
|
|
|
e = [detailArray objectEnumerator];
|
|
|
|
while ((object = [e nextObject]) != nil)
|
|
|
|
if ([[object timeZoneAbbreviation] isEqualToString: abbreviation])
|
|
|
|
return object;
|
|
|
|
|
|
|
|
/* If we reach here, we've got an inconsistency in our time zone
|
|
|
|
database. */
|
|
|
|
[NSException
|
|
|
|
raise: NSInternalInconsistencyException
|
1997-11-03 14:30:13 +00:00
|
|
|
format: @"Time zone abbreviation `%@' inconsistent.", abbreviation];
|
1997-09-01 21:59:51 +00:00
|
|
|
return nil;
|
1995-08-23 16:13:42 +00:00
|
|
|
}
|
|
|
|
|
1999-08-25 16:12:36 +00:00
|
|
|
+ (NSTimeZone*) timeZoneWithName: (NSString*)aTimeZoneName
|
1995-08-23 16:13:42 +00:00
|
|
|
{
|
1997-09-01 21:59:51 +00:00
|
|
|
static NSString *fileException = @"fileException";
|
1997-11-03 14:30:13 +00:00
|
|
|
static NSString *errMess = @"File read error in NSTimeZone.";
|
1997-10-18 19:49:50 +00:00
|
|
|
id zone, transArray, detailsArray;
|
1997-09-01 21:59:51 +00:00
|
|
|
int i, n_trans, n_types, names_size;
|
|
|
|
id *abbrevsArray;
|
|
|
|
char *trans, *type_idxs, *zone_abbrevs;
|
|
|
|
struct tzhead header;
|
|
|
|
struct ttinfo *types; // Temporary array for details
|
1997-11-03 14:30:13 +00:00
|
|
|
FILE *file = NULL;
|
1997-10-18 19:49:50 +00:00
|
|
|
NSString *fileName;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
|
|
[zone_mutex lock];
|
|
|
|
zone = [zoneDictionary objectForKey: aTimeZoneName];
|
|
|
|
if (zone != nil)
|
|
|
|
{
|
|
|
|
[zone_mutex unlock];
|
|
|
|
return zone;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure that only time zone files are accessed.
|
|
|
|
FIXME: Make this more robust. */
|
|
|
|
if ([aTimeZoneName length] == 0
|
1998-09-30 07:42:38 +00:00
|
|
|
|| ([aTimeZoneName cString])[0] == '/'
|
|
|
|
|| strchr([aTimeZoneName cString], '.') != NULL)
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
1997-11-03 14:30:13 +00:00
|
|
|
NSLog(@"Disallowed time zone name `%@'.", aTimeZoneName);
|
1997-09-01 21:59:51 +00:00
|
|
|
[zone_mutex unlock];
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_DURING
|
1999-08-25 16:12:36 +00:00
|
|
|
{
|
|
|
|
zone = [NSConcreteTimeZone alloc];
|
|
|
|
|
|
|
|
/* Open file. */
|
|
|
|
fileName = [NSTimeZone getTimeZoneFile: aTimeZoneName];
|
|
|
|
#if defined(__WIN32__)
|
|
|
|
file = fopen([fileName fileSystemRepresentation], "rb");
|
|
|
|
#else
|
|
|
|
file = fopen([fileName fileSystemRepresentation], "r");
|
|
|
|
#endif
|
|
|
|
if (file == NULL)
|
|
|
|
[NSException raise: fileException format: errMess];
|
|
|
|
|
|
|
|
/* Read header. */
|
|
|
|
if (fread(&header, sizeof(struct tzhead), 1, file) != 1)
|
|
|
|
[NSException raise: fileException format: errMess];
|
|
|
|
|
|
|
|
n_trans = decode(header.tzh_timecnt);
|
|
|
|
n_types = decode(header.tzh_typecnt);
|
|
|
|
names_size = decode(header.tzh_charcnt);
|
|
|
|
|
|
|
|
/* Read in transitions. */
|
|
|
|
trans = NSZoneMalloc(NSDefaultMallocZone(), 4*n_trans);
|
|
|
|
type_idxs = NSZoneMalloc(NSDefaultMallocZone(), n_trans);
|
|
|
|
if (fread(trans, 4, n_trans, file) != n_trans
|
|
|
|
|| fread(type_idxs, 1, n_trans, file) != n_trans)
|
|
|
|
[NSException raise: fileException format: errMess];
|
|
|
|
transArray = [NSMutableArray arrayWithCapacity: n_trans];
|
|
|
|
for (i = 0; i < n_trans; i++)
|
|
|
|
[transArray
|
|
|
|
addObject: [[NSInternalTimeTransition alloc]
|
|
|
|
initWithTime: decode(trans+(i*4))
|
|
|
|
withIndex: type_idxs[i]]];
|
|
|
|
NSZoneFree(NSDefaultMallocZone(), trans);
|
|
|
|
NSZoneFree(NSDefaultMallocZone(), type_idxs);
|
|
|
|
|
|
|
|
/* Read in time zone details. */
|
|
|
|
types =
|
|
|
|
NSZoneMalloc(NSDefaultMallocZone(), sizeof(struct ttinfo)*n_types);
|
|
|
|
for (i = 0; i < n_types; i++)
|
|
|
|
{
|
|
|
|
unsigned char x[4];
|
1997-10-24 16:58:20 +00:00
|
|
|
|
1999-08-25 16:12:36 +00:00
|
|
|
if (fread(x, 1, 4, file) != 4
|
|
|
|
|| fread(&types[i].isdst, 1, 1, file) != 1
|
|
|
|
|| fread(&types[i].abbr_idx, 1, 1, file) != 1)
|
|
|
|
[NSException raise: fileException format: errMess];
|
|
|
|
types[i].offset = decode(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read in time zone abbreviation strings. */
|
|
|
|
zone_abbrevs = NSZoneMalloc(NSDefaultMallocZone(), names_size);
|
|
|
|
if (fread(zone_abbrevs, 1, names_size, file) != names_size)
|
|
|
|
[NSException raise: fileException format: errMess];
|
|
|
|
abbrevsArray = NSZoneMalloc(NSDefaultMallocZone(), sizeof(id)*names_size);
|
|
|
|
i = 0;
|
|
|
|
while (i < names_size)
|
|
|
|
{
|
|
|
|
abbrevsArray[i] = [NSString stringWithCString: zone_abbrevs+i];
|
|
|
|
i = (strchr(zone_abbrevs+i, '\0')-zone_abbrevs)+1;
|
|
|
|
}
|
|
|
|
NSZoneFree(NSDefaultMallocZone(), zone_abbrevs);
|
|
|
|
|
|
|
|
/* Create time zone details. */
|
|
|
|
detailsArray = [NSMutableArray arrayWithCapacity: n_types];
|
|
|
|
for (i = 0; i < n_types; i++)
|
|
|
|
{
|
|
|
|
NSConcreteTimeZoneDetail *detail;
|
|
|
|
|
|
|
|
detail = [[NSConcreteTimeZoneDetail alloc]
|
|
|
|
initWithTimeZone: zone
|
|
|
|
withAbbrev: abbrevsArray[(int)types[i].abbr_idx]
|
|
|
|
withOffset: types[i].offset
|
|
|
|
withDST: (types[i].isdst > 0)];
|
|
|
|
[detailsArray addObject: detail];
|
|
|
|
RELEASE(detail);
|
|
|
|
}
|
|
|
|
NSZoneFree(NSDefaultMallocZone(), abbrevsArray);
|
|
|
|
NSZoneFree(NSDefaultMallocZone(), types);
|
|
|
|
|
|
|
|
[zone initWithName: aTimeZoneName
|
|
|
|
withTransitions: transArray
|
|
|
|
withDetails: detailsArray];
|
|
|
|
[zoneDictionary setObject: zone forKey: [zone timeZoneName]];
|
|
|
|
}
|
1997-09-01 21:59:51 +00:00
|
|
|
NS_HANDLER
|
1999-08-25 16:12:36 +00:00
|
|
|
{
|
|
|
|
if (zone != nil)
|
|
|
|
RELEASE(zone);
|
|
|
|
zone = nil;
|
|
|
|
if ([localException name] != fileException)
|
|
|
|
[localException raise];
|
|
|
|
NSLog(@"Unable to obtain time zone `%@'.", aTimeZoneName);
|
|
|
|
}
|
1997-09-01 21:59:51 +00:00
|
|
|
NS_ENDHANDLER
|
|
|
|
|
1999-08-25 16:12:36 +00:00
|
|
|
RELEASE(zone); /* retained in zoneDictionary. */
|
|
|
|
|
1997-11-03 14:30:13 +00:00
|
|
|
if (file != NULL)
|
|
|
|
fclose(file);
|
1997-09-01 21:59:51 +00:00
|
|
|
[zone_mutex unlock];
|
|
|
|
return zone;
|
1995-08-23 16:13:42 +00:00
|
|
|
}
|
|
|
|
|
1999-08-25 16:12:36 +00:00
|
|
|
- (NSTimeZoneDetail*) timeZoneDetailForDate: (NSDate*)date
|
1995-08-23 16:13:42 +00:00
|
|
|
{
|
1997-09-01 21:59:51 +00:00
|
|
|
return [self subclassResponsibility: _cmd];
|
1995-08-23 16:13:42 +00:00
|
|
|
}
|
|
|
|
|
1999-08-25 16:12:36 +00:00
|
|
|
+ (void) setDefaultTimeZone: (NSTimeZone*)aTimeZone
|
1995-08-23 16:13:42 +00:00
|
|
|
{
|
1997-09-01 21:59:51 +00:00
|
|
|
if (aTimeZone == nil)
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
1997-11-03 14:30:13 +00:00
|
|
|
format: @"Nil time zone specified."];
|
1999-08-25 16:12:36 +00:00
|
|
|
ASSIGN(localTimeZone, aTimeZone);
|
1995-08-23 16:13:42 +00:00
|
|
|
}
|
|
|
|
|
1999-08-25 16:12:36 +00:00
|
|
|
+ (NSDictionary*) abbreviationDictionary
|
1995-08-23 16:13:42 +00:00
|
|
|
{
|
1997-09-01 21:59:51 +00:00
|
|
|
return fake_abbrev_dict;
|
1995-08-23 16:13:42 +00:00
|
|
|
}
|
|
|
|
|
1999-08-25 16:12:36 +00:00
|
|
|
+ (NSDictionary*) abbreviationMap
|
1995-08-23 16:13:42 +00:00
|
|
|
{
|
1997-09-01 21:59:51 +00:00
|
|
|
/* Instead of creating the abbreviation dictionary when the class is
|
|
|
|
initialized, we create it when we first need it, since the
|
|
|
|
dictionary can be potentially very large, considering that it's
|
|
|
|
almost never used. */
|
|
|
|
|
|
|
|
static NSMutableDictionary *abbreviationDictionary = nil;
|
|
|
|
FILE *file; // For the file containing the abbreviation dictionary
|
|
|
|
char abbrev[80], name[80];
|
1997-10-18 19:49:50 +00:00
|
|
|
NSString *fileName;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
|
|
if (abbreviationDictionary != nil)
|
|
|
|
return abbreviationDictionary;
|
|
|
|
|
|
|
|
/* Read dictionary from file. */
|
1997-10-17 13:35:52 +00:00
|
|
|
abbreviationDictionary = [[NSMutableDictionary alloc] init];
|
1997-10-18 19:49:50 +00:00
|
|
|
fileName = [NSTimeZone getAbbreviationFile];
|
1999-05-11 09:21:38 +00:00
|
|
|
#if defined(__WIN32__)
|
|
|
|
file = fopen([fileName fileSystemRepresentation], "rb");
|
|
|
|
#else
|
|
|
|
file = fopen([fileName fileSystemRepresentation], "r");
|
|
|
|
#endif
|
1997-09-01 21:59:51 +00:00
|
|
|
if (file == NULL)
|
1997-11-03 14:30:13 +00:00
|
|
|
[NSException
|
|
|
|
raise: NSInternalInconsistencyException
|
|
|
|
format: @"Failed to open time zone abbreviation dictionary."];
|
1997-10-17 13:35:52 +00:00
|
|
|
while (fscanf(file, "%79s %79s", abbrev, name) == 2)
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
|
|
|
id a, the_name, the_abbrev;
|
|
|
|
|
|
|
|
the_name = [NSString stringWithCString: name];
|
|
|
|
the_abbrev = [NSString stringWithCString: abbrev];
|
|
|
|
a = [abbreviationDictionary objectForKey: the_abbrev];
|
|
|
|
if (a == nil)
|
|
|
|
{
|
1997-10-17 13:35:52 +00:00
|
|
|
a = [[NSMutableArray alloc] init];
|
1997-09-01 21:59:51 +00:00
|
|
|
[abbreviationDictionary setObject: a forKey: the_abbrev];
|
|
|
|
}
|
|
|
|
[a addObject: the_name];
|
|
|
|
}
|
|
|
|
fclose(file);
|
1995-08-23 16:13:42 +00:00
|
|
|
|
1997-09-01 21:59:51 +00:00
|
|
|
return abbreviationDictionary;
|
1997-10-17 13:35:52 +00:00
|
|
|
}
|
1997-11-03 14:30:13 +00:00
|
|
|
|
1999-08-25 16:12:36 +00:00
|
|
|
- (NSString*) timeZoneName
|
1997-10-17 13:35:52 +00:00
|
|
|
{
|
1997-09-01 21:59:51 +00:00
|
|
|
return [self subclassResponsibility: _cmd];
|
1997-10-17 13:35:52 +00:00
|
|
|
}
|
1997-11-03 14:30:13 +00:00
|
|
|
|
1999-08-25 16:12:36 +00:00
|
|
|
+ (NSArray*) timeZoneArray
|
1997-10-17 13:35:52 +00:00
|
|
|
{
|
1997-09-01 21:59:51 +00:00
|
|
|
/* We create the array only when we need it to reduce overhead. */
|
|
|
|
|
|
|
|
static NSArray *regionsArray = nil;
|
|
|
|
int index, i;
|
|
|
|
char name[80];
|
|
|
|
FILE *file;
|
|
|
|
id temp_array[24];
|
1997-10-18 19:49:50 +00:00
|
|
|
NSString *fileName;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
|
|
if (regionsArray != nil)
|
|
|
|
return regionsArray;
|
|
|
|
|
|
|
|
for (i = 0; i < 24; i++)
|
1999-08-25 16:12:36 +00:00
|
|
|
temp_array[i] = [NSMutableArray array];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
1997-10-18 19:49:50 +00:00
|
|
|
fileName = [NSTimeZone getRegionsFile];
|
1999-05-11 09:21:38 +00:00
|
|
|
#if defined(__WIN32__)
|
|
|
|
file = fopen([fileName fileSystemRepresentation], "rb");
|
|
|
|
#else
|
|
|
|
file = fopen([fileName fileSystemRepresentation], "r");
|
|
|
|
#endif
|
1997-09-01 21:59:51 +00:00
|
|
|
if (file == NULL)
|
1997-11-03 14:30:13 +00:00
|
|
|
[NSException
|
|
|
|
raise: NSInternalInconsistencyException
|
|
|
|
format: @"Failed to open time zone regions array file."];
|
1997-09-01 21:59:51 +00:00
|
|
|
while (fscanf(file, "%d %s", &index, name) == 2)
|
1999-08-25 16:12:36 +00:00
|
|
|
[temp_array[index] addObject: [NSString stringWithCString: name]];
|
1997-09-01 21:59:51 +00:00
|
|
|
fclose(file);
|
|
|
|
regionsArray = [[NSArray alloc] initWithObjects: temp_array count: 24];
|
|
|
|
return regionsArray;
|
1997-10-17 13:35:52 +00:00
|
|
|
}
|
1997-11-03 14:30:13 +00:00
|
|
|
|
1999-08-25 16:12:36 +00:00
|
|
|
- (NSArray*) timeZoneDetailArray
|
1997-10-17 13:35:52 +00:00
|
|
|
{
|
1997-09-01 21:59:51 +00:00
|
|
|
return [self subclassResponsibility: _cmd];
|
1997-10-17 13:35:52 +00:00
|
|
|
}
|
1997-11-03 14:30:13 +00:00
|
|
|
|
1997-10-17 13:35:52 +00:00
|
|
|
@end
|
1997-09-01 21:59:51 +00:00
|
|
|
|
1995-08-23 16:13:42 +00:00
|
|
|
|
1997-09-01 21:59:51 +00:00
|
|
|
@implementation NSTimeZoneDetail
|
1995-08-23 16:13:42 +00:00
|
|
|
|
1999-08-25 16:12:36 +00:00
|
|
|
- (NSString*) description
|
1997-11-03 14:30:13 +00:00
|
|
|
{
|
|
|
|
return [NSString
|
|
|
|
stringWithFormat: @"%@(%@, %s%d)",
|
|
|
|
[self timeZoneName],
|
|
|
|
[self timeZoneAbbreviation],
|
|
|
|
([self isDaylightSavingTimeZone]? "IS_DST, ": ""),
|
|
|
|
[self timeZoneSecondsFromGMT]];
|
|
|
|
}
|
|
|
|
|
1999-08-25 16:12:36 +00:00
|
|
|
- (BOOL) isDaylightSavingTimeZone
|
1997-10-17 13:35:52 +00:00
|
|
|
{
|
1997-09-01 21:59:51 +00:00
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
return NO;
|
1997-10-17 13:35:52 +00:00
|
|
|
}
|
1997-09-01 21:59:51 +00:00
|
|
|
|
1999-08-25 16:12:36 +00:00
|
|
|
- (NSString*) timeZoneAbbreviation
|
1997-10-17 13:35:52 +00:00
|
|
|
{
|
1997-09-01 21:59:51 +00:00
|
|
|
return [self subclassResponsibility: _cmd];
|
1997-10-17 13:35:52 +00:00
|
|
|
}
|
1997-09-01 21:59:51 +00:00
|
|
|
|
1999-08-25 16:12:36 +00:00
|
|
|
- (int) timeZoneSecondsFromGMT
|
1997-10-17 13:35:52 +00:00
|
|
|
{
|
1997-09-01 21:59:51 +00:00
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
return 0;
|
1997-10-17 13:35:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
1997-11-03 14:30:13 +00:00
|
|
|
|
1997-10-18 19:49:50 +00:00
|
|
|
@implementation NSTimeZone (Private)
|
1997-10-17 13:35:52 +00:00
|
|
|
|
1999-08-25 16:12:36 +00:00
|
|
|
+ (NSString*) getAbbreviationFile
|
1997-10-18 19:49:50 +00:00
|
|
|
{
|
|
|
|
return [NSBundle pathForGNUstepResource: ABBREV_DICT
|
|
|
|
ofType: @""
|
|
|
|
inDirectory: TIME_ZONE_DIR];
|
|
|
|
}
|
1997-10-17 13:35:52 +00:00
|
|
|
|
1999-08-25 16:12:36 +00:00
|
|
|
+ (NSString*) getRegionsFile
|
1997-10-18 19:49:50 +00:00
|
|
|
{
|
|
|
|
return [NSBundle pathForGNUstepResource: REGIONS_FILE
|
|
|
|
ofType: @""
|
|
|
|
inDirectory: TIME_ZONE_DIR];
|
|
|
|
}
|
1997-10-17 13:35:52 +00:00
|
|
|
|
1999-08-25 16:12:36 +00:00
|
|
|
+ (NSString*) getLocalTimeFile
|
1997-10-18 19:49:50 +00:00
|
|
|
{
|
|
|
|
return [NSBundle pathForGNUstepResource: LOCAL_TIME_FILE
|
|
|
|
ofType: @""
|
|
|
|
inDirectory: TIME_ZONE_DIR];
|
|
|
|
}
|
1997-10-17 13:35:52 +00:00
|
|
|
|
1999-08-25 16:12:36 +00:00
|
|
|
+ (NSString*) getTimeZoneFile: (NSString *)name
|
1997-10-18 19:49:50 +00:00
|
|
|
{
|
1997-11-28 20:13:58 +00:00
|
|
|
NSString *fileName = [NSString stringWithFormat: @"%@%@",
|
1997-10-18 19:49:50 +00:00
|
|
|
ZONES_DIR, name];
|
1997-11-03 14:30:13 +00:00
|
|
|
|
1997-10-18 19:49:50 +00:00
|
|
|
return [NSBundle pathForGNUstepResource: fileName
|
|
|
|
ofType: @""
|
|
|
|
inDirectory: TIME_ZONE_DIR];
|
|
|
|
}
|
1997-10-17 13:35:52 +00:00
|
|
|
|
1997-10-18 19:49:50 +00:00
|
|
|
@end
|