forked from omz/AppSales-Mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppleFiscalCalendar.m
125 lines (112 loc) · 5.35 KB
/
AppleFiscalCalendar.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
//
// AppleFiscalCalendar.m
// AppSalesMobile
//
// Created by Tim Shadel on 4/5/11.
// Copyright 2011 Shadel Software, Inc. All rights reserved.
//
#import "AppleFiscalCalendar.h"
#import "AppSalesUtils.h"
@implementation AppleFiscalCalendar
- (id)init
{
self = [super init];
if (self) {
sortedDateStrings = [[NSArray alloc] initWithObjects:
@"20080927", // Oct 2008
@"20081101", // Nov 2008
@"20081129", // Dec 2008
@"20081227", // Jan 2009
@"20090131", // Feb 2009
@"20090228", // Mar 2009
@"20090328", // Apr 2009
@"20090502", // May 2009
@"20090530", // Jun 2009
@"20090627", // Jul 2009
@"20090801", // Aug 2009
@"20090829", // Sep 2009
@"20090926", // Oct 2009
@"20091031", // Nov 2009
@"20091128", // Dec 2009
@"20091226", // Jan 2010
@"20100130", // Feb 2010
@"20100227", // Mar 2010
@"20100327", // Apr 2010
@"20100501", // May 2010
@"20100529", // Jun 2010
@"20100626", // Jul 2010
@"20100731", // Aug 2010
@"20100828", // Sep 2010
@"20100925", // Oct 2010
@"20101030", // Nov 2010
@"20101127", // Dec 2010
@"20101225", // Jan 2011
@"20110129", // Feb 2011
@"20110226", // Mar 2011
@"20110326", // Apr 2011
@"20110430", // May 2011
@"20110528", // Jun 2011
@"20110625", // Jul 2011
@"20110730", // Aug 2011
@"20110827", // Sep 2011
nil];
const NSUInteger numSortedDates = [sortedDateStrings count];
NSMutableArray *names = [NSMutableArray arrayWithCapacity:numSortedDates];
NSMutableArray *dates = [NSMutableArray arrayWithCapacity:numSortedDates];
NSDateFormatter *dayStringParser = [NSDateFormatter new];
[dayStringParser setDateFormat:@"YYYYMMdd"];
NSDateFormatter *sectionTitleFormatter = [NSDateFormatter new];
[sectionTitleFormatter setDateFormat:@"MMMM yyyy"];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
for (NSString *dayString in sortedDateStrings) {
NSDate *date = [dayStringParser dateFromString:dayString];
[dates addObject:date];
// Name of the fiscal month can be reliably found by the calendar month of a day 2 weeks after the fiscal month begins
NSDateComponents *components = [NSDateComponents new];
[components setDay:14];
NSDate *result = [gregorian dateByAddingComponents:components toDate:date options:0];
[components release];
NSString *fiscalMonthName = [sectionTitleFormatter stringFromDate:result];
[names addObject:fiscalMonthName];
}
[gregorian release];
[dayStringParser release];
[sectionTitleFormatter release];
sortedFiscalMonthNames = [[NSArray arrayWithArray:names] retain];
sortedDates = [[NSArray arrayWithArray:dates] retain];
}
return self;
}
- (NSString *)fiscalMonthForDate:(NSDate *)requestedDate
{
NSUInteger indexOfNextMonth = [sortedDates
indexOfObject:requestedDate
inSortedRange:NSMakeRange(0, [sortedDates count])
options:NSBinarySearchingLastEqual|NSBinarySearchingInsertionIndex
usingComparator:
^(id obj1, id obj2){
// Pass the day if equals, so that we can always go back one index
return [obj1 compare:obj2] == NSOrderedAscending ? NSOrderedAscending : NSOrderedDescending;
}];
if (indexOfNextMonth > 0) {
return [sortedFiscalMonthNames objectAtIndex:indexOfNextMonth-1];
} else {
return nil;
}
}
+ (AppleFiscalCalendar *)sharedFiscalCalendar
{
ASSERT_IS_MAIN_THREAD();
static AppleFiscalCalendar *sharedFiscalCalendar = nil;
if (sharedFiscalCalendar == nil)
sharedFiscalCalendar = [AppleFiscalCalendar new];
return sharedFiscalCalendar;
}
- (void)dealloc
{
RELEASE_SAFELY(sortedDates);
RELEASE_SAFELY(sortedDateStrings);
RELEASE_SAFELY(sortedFiscalMonthNames);
[super dealloc];
}
@end