-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMultiresDateFormatter.m
More file actions
76 lines (61 loc) · 2.32 KB
/
Copy pathMultiresDateFormatter.m
File metadata and controls
76 lines (61 loc) · 2.32 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
//
// MultiresDateFormatter.m
// AAPLot
//
// Created by admin on 3/23/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "MultiresDateFormatter.h"
@implementation MultiresDateFormatter
@synthesize indexConverter;
- (id)initWithStartDate:(NSDate*)aStartDate endDate:(NSDate*)aEndDate// numIndices:(NSUInteger)aNumIndices
{
if ( (self = [super init]) ) {
startDate = [aStartDate retain];
endDate = [aEndDate retain];
//numIndices = aNumIndices;
indexConverter = nil;
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSTimeInterval ti = [endDate timeIntervalSinceReferenceDate] - [startDate timeIntervalSinceReferenceDate];
if (ti <= 24 * 60 * 60 * 3) { // 3 days
resolution = MultiresDateResolutionHours;
[dateFormatter setDateFormat:@"H"];
} else if (ti <= 24 * 60 * 60 * 31 * 2) { // 2 months
resolution = MultiresDateResolutionDays;
[dateFormatter setDateFormat:@"dd"];
} else if (ti <= 24 * 60 * 60 * 366 * 2) { // 2 years
resolution = MultiresDateResolutionMonths;
[dateFormatter setDateFormat:@"MMM"];
}
}
return self;
}
-(void)dealloc
{
[startDate release];
[endDate release];
[dateFormatter release];
[super dealloc];
}
#pragma mark -
#pragma mark 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.
**/
-(NSString *)stringForObjectValue:(NSDecimalNumber *)coordinateValue
{
NSString *string = @"";
if (indexConverter != nil && [(NSObject*)indexConverter respondsToSelector:@selector(dateFromIndex:)]) {
NSDate *date = [indexConverter dateFromIndex:coordinateValue];
NSTimeInterval delta = [endDate timeIntervalSinceDate:startDate];
if ([date timeIntervalSinceDate:startDate] > delta / 12 && [endDate timeIntervalSinceDate:date] > delta / 12)
string = [dateFormatter stringFromDate:date];
}
return string;
}
@end