/
NSConcreteTimeZoneDetail.m
197 lines (170 loc) · 5.16 KB
/
NSConcreteTimeZoneDetail.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
NSConcreteTimeZoneDetail.m
Copyright (C) 1995, 1996 Ovidiu Predescu and Mircea Oancea.
All rights reserved.
Author: Mircea Oancea <mircea@jupiter.elcom.pub.ro>
Ovidiu Predescu <ovidiu@bx.logicnet.ro>
This file is part of libFoundation.
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted, provided
that the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.
We disclaim all warranties with regard to this software, including all
implied warranties of merchantability and fitness, in no event shall
we be liable for any special, indirect or consequential damages or any
damages whatsoever resulting from loss of use, data or profits, whether in
an action of contract, negligence or other tortious action, arising out of
or in connection with the use or performance of this software.
*/
#include <Foundation/common.h>
#include <Foundation/NSString.h>
#include <Foundation/NSArray.h>
#include <Foundation/NSDictionary.h>
#include <Foundation/NSString.h>
#include <Foundation/NSScanner.h>
#include <Foundation/NSCoder.h>
#include "NSConcreteTimeZoneDetail.h"
@implementation NSConcreteTimeZoneDetail
+ (id)detailFromPropertyList:(id)propList name:(NSString*)_name
parentZone:(NSTimeZone *)_zone
{
id abbrev;
int offset;
BOOL _isDST;
abbrev = [propList objectForKey:@"abbrev"];
offset = [self _offsetFromString:[propList objectForKey:@"offset"]];
_isDST = [[propList objectForKey:@"isDST"] isEqual:@"1"];
#if 0
printf("%s is DST %s -> %s\n",
[abbrev cString],
[[propList objectForKey:@"isDST"] cString],
_isDST ? "yes" : "no");
#endif
return AUTORELEASE([[self alloc]
initWithAbbreviation:abbrev
secondsFromGMT:offset
isDaylightSaving:_isDST
name:_name
parentZone:_zone]);
}
- (id)initWithAbbreviation:(NSString *)anAbbreviation
secondsFromGMT:(int)aDifference
isDaylightSaving:(BOOL)aDst
name:(NSString *)_name
parentZone:(NSTimeZone *)_zone
{
[super init];
self->abbreviation = [anAbbreviation copyWithZone:[self zone]];
self->name = [_name copyWithZone:[self zone]];
self->offsetFromGMT = aDifference;
self->isDST = aDst;
self->parentZone = _zone;
return self;
}
- (void)dealloc
{
RELEASE(self->abbreviation);
RELEASE(self->name);
[super dealloc];
}
- (id)copyWithZone:(NSZone*)zone
{
if ([self zone] == zone)
return RETAIN(self);
return [[[self class] alloc]
initWithAbbreviation:self->abbreviation
secondsFromGMT:self->offsetFromGMT
isDaylightSaving:self->isDST
name:self->name
parentZone:self->parentZone];
}
- (BOOL)isDaylightSavingTimeZone
{
return self->isDST;
}
- (NSString *)timeZoneAbbreviation
{
return self->abbreviation;
}
- (NSString *)name
{
return self->name;
}
- (int)timeZoneSecondsFromGMT
{
return self->offsetFromGMT;
}
- (NSString *)timeZoneName
{
return self->name;
}
- (NSArray *)timeZoneDetailArray
{
return [self->parentZone timeZoneDetailArray];
}
- (NSTimeZone *)timeZoneForDate:(NSDate *)date
{
/* new in MacOSXS */
#if 0
NSLog(@"detail for %@, tz %@, self %@",
date, self->parentZone, self);
#endif
return [self->parentZone timeZoneForDate:date];
}
- (NSString *)description
{
int offset = abs(offsetFromGMT);
int hours = offset / 3600;
int minutes = (offset - 3600 * hours) / 60;
int seconds = (offset - 3600 * hours - 60 * minutes);
return [NSString stringWithFormat:@"%@ %c%02d:%02d:%02d",
abbreviation,
offsetFromGMT < 0 ? '-' : '+',
hours,
minutes,
seconds];
}
+ (int)_offsetFromString:(NSString*)string
{
NSScanner *scanner;
int hours, minutes, seconds, offset;
BOOL isNegative, errors;
scanner = [NSScanner scannerWithString:string];
errors = NO;
if ([scanner scanInt:&hours]
&& [scanner scanString:@":" intoString:NULL]
&& [scanner scanInt:&minutes]
&& [scanner scanString:@":" intoString:NULL]
&& [scanner scanInt:&seconds]) {
isNegative = (hours < 0);
hours = abs(hours);
if (hours < 0 || hours > 23) {
NSLog (@"Hours should be between 0 and 23 in '%@'", string);
errors = YES;
}
if (minutes < 0 || minutes > 59) {
NSLog (@"Minutes should be between 0 and 59 in '%@'", string);
errors = YES;
}
if (seconds < 0 || seconds > 59) {
NSLog (@"Seconds should be between 0 and 59 in '%@'", string);
errors = YES;
}
if (errors)
return 0;
offset = 3600 * hours + 60 * minutes + seconds;
return isNegative ? -offset : offset;
}
else {
NSLog (@"Cannot parse offset definition '%@'", string);
return 0;
}
}
@end /* NSConcreteTimeZoneDetail */
/*
Local Variables:
c-basic-offset: 4
tab-width: 8
End:
*/