Repository navigation
Expand file tree
/
Copy pathCPTCalendarFormatter.m
More file actions
264 lines (215 loc) · 8.92 KB
/
Copy pathCPTCalendarFormatter.m
File metadata and controls
264 lines (215 loc) · 8.92 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#import "CPTCalendarFormatter.h"
#import "CPTExceptions.h"
/** @brief A number formatter that converts calendar intervals to dates.
*
* Useful for formatting labels on an axis. The numerical
* scale of the plot space will be used to increment the specified calendar unit.
* For example, with a @link CPTAxis::majorIntervalLength majorIntervalLength @endlink of one (@num{1}) and a
* @ref referenceCalendarUnit of
* @ref NSMonthCalendarUnit, successive months will be displayed on the axis.
* Axis labels can be directly generated by setting a CPTCalendarFormatter as the @link CPTAxis::labelFormatter labelFormatter @endlink
* and/or @link CPTAxis::minorTickLabelFormatter minorTickLabelFormatter @endlink.
**/
@implementation CPTCalendarFormatter
/** @property nullable NSDateFormatter *dateFormatter
* @brief The date formatter used to generate strings from date calculations.
**/
@synthesize dateFormatter;
/** @property nullable NSDate *referenceDate
* @brief Date from which time intervals are computed.
* If @nil, the standard reference date (1 January 2001, GMT) is used.
**/
@synthesize referenceDate;
/** @property nullable NSCalendar *referenceCalendar
* @brief Calendar which is used for date calculations.
* If @nil, the current calendar is used.
**/
@synthesize referenceCalendar;
/** @property NSCalendarUnit referenceCalendarUnit
* @brief Calendar unit which is incremented in the date calculation.
* If zero (@num{0}), the date is incremented.
**/
@synthesize referenceCalendarUnit;
#pragma mark -
#pragma mark Init/Dealloc
/// @name Initialization
/// @{
/** @brief Initializes a newly allocated CPTCalendarFormatter object with a default date formatter.
* The default formatter uses @ref NSDateFormatterMediumStyle for dates and times.
* @return The initialized object.
**/
-(nonnull instancetype)init
{
NSDateFormatter *newDateFormatter = [[NSDateFormatter alloc] init];
newDateFormatter.dateStyle = NSDateFormatterMediumStyle;
newDateFormatter.timeStyle = NSDateFormatterMediumStyle;
self = [self initWithDateFormatter:newDateFormatter];
return self;
}
/// @}
/** @brief Initializes new instance with the date formatter passed.
* @param aDateFormatter The date formatter.
* @return The new instance.
**/
-(nonnull instancetype)initWithDateFormatter:(nullable NSDateFormatter *)aDateFormatter
{
if ( (self = [super init]) ) {
dateFormatter = aDateFormatter;
referenceDate = nil;
referenceCalendar = nil;
referenceCalendarUnit = NSCalendarUnitEra;
}
return self;
}
#pragma mark -
#pragma mark NSCoding Methods
/// @cond
-(void)encodeWithCoder:(nonnull NSCoder *)coder
{
[super encodeWithCoder:coder];
[coder encodeObject:self.dateFormatter forKey:@"CPTCalendarFormatter.dateFormatter"];
[coder encodeObject:self.referenceDate forKey:@"CPTCalendarFormatter.referenceDate"];
[coder encodeObject:self.referenceCalendar forKey:@"CPTCalendarFormatter.referenceCalendar"];
[coder encodeInteger:(NSInteger)self.referenceCalendarUnit forKey:@"CPTCalendarFormatter.referenceCalendarUnit"];
}
/// @endcond
/** @brief Returns an object initialized from data in a given unarchiver.
* @param coder An unarchiver object.
* @return An object initialized from data in a given unarchiver.
*/
-(nullable instancetype)initWithCoder:(nonnull NSCoder *)coder
{
if ( (self = [super init]) ) {
dateFormatter = [coder decodeObjectForKey:@"CPTCalendarFormatter.dateFormatter"];
referenceDate = [[coder decodeObjectForKey:@"CPTCalendarFormatter.referenceDate"] copy];
referenceCalendar = [[coder decodeObjectForKey:@"CPTCalendarFormatter.referenceCalendar"] copy];
referenceCalendarUnit = (NSCalendarUnit)[coder decodeIntegerForKey:@"CPTCalendarFormatter.referenceCalendarUnit"];
}
return self;
}
#pragma mark -
#pragma mark NSCopying Methods
/// @cond
-(nonnull id)copyWithZone:(nullable NSZone *)zone
{
CPTCalendarFormatter *newFormatter = [[CPTCalendarFormatter allocWithZone:zone] init];
if ( newFormatter ) {
newFormatter.dateFormatter = self.dateFormatter;
newFormatter.referenceDate = self.referenceDate;
newFormatter.referenceCalendar = self.referenceCalendar;
newFormatter.referenceCalendarUnit = self.referenceCalendarUnit;
}
return newFormatter;
}
/// @endcond
#pragma mark -
#pragma mark Formatting
/// @name Formatting
/// @{
/**
* @brief Converts decimal number for the time into a date string.
* Uses the date formatter to do the conversion. Conversions are relative to the
* reference date, unless it is @nil, in which case the standard reference date
* of 1 January 2001, GMT is used.
* @param coordinateValue The time value.
* @return The date string.
**/
-(nullable NSString *)stringForObjectValue:(nonnull id)coordinateValue
{
NSInteger componentIncrement = 0;
if ( [coordinateValue respondsToSelector:@selector(integerValue)] ) {
componentIncrement = [coordinateValue integerValue];
}
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
switch ( self.referenceCalendarUnit ) {
case NSCalendarUnitEra:
dateComponents.era = componentIncrement;
break;
case NSCalendarUnitYear:
dateComponents.year = componentIncrement;
break;
case NSCalendarUnitMonth:
dateComponents.month = componentIncrement;
break;
case NSCalendarUnitDay:
dateComponents.day = componentIncrement;
break;
case NSCalendarUnitHour:
dateComponents.hour = componentIncrement;
break;
case NSCalendarUnitMinute:
dateComponents.minute = componentIncrement;
break;
case NSCalendarUnitSecond:
dateComponents.second = componentIncrement;
break;
case NSCalendarUnitWeekday:
dateComponents.weekday = componentIncrement;
break;
case NSCalendarUnitWeekdayOrdinal:
dateComponents.weekdayOrdinal = componentIncrement;
break;
#if MAC_OS_X_VERSION_10_5<MAC_OS_X_VERSION_MAX_ALLOWED || __IPHONE_3_0<__IPHONE_OS_VERSION_MAX_ALLOWED
case NSCalendarUnitQuarter:
if ( [dateComponents respondsToSelector:@selector(setQuarter:)] ) {
dateComponents.quarter = componentIncrement;
}
else {
[NSException raise:CPTException format:@"Unsupported calendar unit: NSQuarterCalendarUnit"];
}
break;
#endif
#if MAC_OS_X_VERSION_10_6<MAC_OS_X_VERSION_MAX_ALLOWED || __IPHONE_4_0<__IPHONE_OS_VERSION_MAX_ALLOWED
case NSCalendarUnitWeekOfMonth:
if ( [dateComponents respondsToSelector:@selector(setWeekOfMonth:)] ) {
dateComponents.weekOfMonth = componentIncrement;
}
else {
[NSException raise:CPTException format:@"Unsupported calendar unit: NSWeekOfMonthCalendarUnit"];
}
break;
case NSCalendarUnitWeekOfYear:
if ( [dateComponents respondsToSelector:@selector(setWeekOfYear:)] ) {
dateComponents.weekOfYear = componentIncrement;
}
else {
[NSException raise:CPTException format:@"Unsupported calendar unit: NSWeekOfYearCalendarUnit"];
}
break;
case NSCalendarUnitYearForWeekOfYear:
if ( [dateComponents respondsToSelector:@selector(setYearForWeekOfYear:)] ) {
dateComponents.yearForWeekOfYear = componentIncrement;
}
else {
[NSException raise:CPTException format:@"Unsupported calendar unit: NSYearForWeekOfYearCalendarUnit"];
}
break;
#endif
#if MAC_OS_X_VERSION_10_7<MAC_OS_X_VERSION_MAX_ALLOWED || __IPHONE_4_0<__IPHONE_OS_VERSION_MAX_ALLOWED
case NSCalendarUnitCalendar:
[NSException raise:CPTException format:@"Unsupported calendar unit: NSCalendarCalendarUnit"];
break;
case NSCalendarUnitTimeZone:
[NSException raise:CPTException format:@"Unsupported calendar unit: NSTimeZoneCalendarUnit"];
break;
#endif
#if MAC_OS_X_VERSION_10_8<MAC_OS_X_VERSION_MAX_ALLOWED || __IPHONE_4_0<__IPHONE_OS_VERSION_MAX_ALLOWED
default:
[NSException raise:CPTException format:@"Unsupported calendar unit"];
break;
#endif
}
NSDate *startDate = [NSDate dateWithTimeIntervalSinceReferenceDate:0];
if ( self.referenceDate ) {
startDate = self.referenceDate;
}
NSCalendar *calendar = [NSCalendar currentCalendar];
if ( self.referenceCalendar ) {
calendar = self.referenceCalendar;
}
NSDate *computedDate = [calendar dateByAddingComponents:dateComponents toDate:startDate options:0];
NSString *string = [self.dateFormatter stringFromDate:computedDate];
return string;
}
/// @}
@end