-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKalLogic.m
executable file
·144 lines (114 loc) · 4.56 KB
/
KalLogic.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
/*
* Copyright (c) 2009 Keith Lazuka
* License: http://www.opensource.org/licenses/mit-license.html
*/
#import "KalLogic.h"
#import "KalPrivate.h"
@interface KalLogic ()
- (void)moveToMonthForDate:(NSDate *)date;
- (void)recalculateVisibleDays;
- (NSUInteger)numberOfDaysInPreviousPartialWeek;
- (NSUInteger)numberOfDaysInFollowingPartialWeek;
@property (nonatomic, strong) NSDate *fromDate;
@property (nonatomic, strong) NSDate *toDate;
@property (nonatomic, strong) NSArray *daysInSelectedMonth;
@property (nonatomic, strong) NSArray *daysInFinalWeekOfPreviousMonth;
@property (nonatomic, strong) NSArray *daysInFirstWeekOfFollowingMonth;
@end
@implementation KalLogic
@synthesize baseDate, fromDate, toDate, daysInSelectedMonth, daysInFinalWeekOfPreviousMonth, daysInFirstWeekOfFollowingMonth;
+ (NSSet *)keyPathsForValuesAffectingSelectedMonthNameAndYear
{
return [NSSet setWithObjects:@"baseDate", nil];
}
- (id)initForDate:(NSDate *)date
{
if ((self = [super init])) {
monthAndYearFormatter = [[NSDateFormatter alloc] init];
// [monthAndYearFormatter setDateFormat:NSLocalizedString(@"CalendarTitle", @"")];
monthAndYearFormatter.dateFormat = @"YYYY年MMMM";
[monthAndYearFormatter setMonthSymbols:[NSArray arrayWithObjects:@"1月",@"2月",@"3月",@"4月",@"5月",@"6月",@"7月",@"8月",@"9月",@"10月",@"11月",@"12月", nil]];
[self moveToMonthForDate:date];
}
return self;
}
- (id)init
{
return [self initForDate:[NSDate date]];
}
- (void)moveToMonthForDate:(NSDate *)date
{
self.baseDate = [date cc_dateByMovingToFirstDayOfTheMonth];
[self recalculateVisibleDays];
}
- (void)retreatToPreviousMonth
{
[self moveToMonthForDate:[self.baseDate cc_dateByMovingToFirstDayOfThePreviousMonth]];
}
- (void)advanceToFollowingMonth
{
[self moveToMonthForDate:[self.baseDate cc_dateByMovingToFirstDayOfTheFollowingMonth]];
}
- (NSString *)selectedMonthNameAndYear;
{
return [monthAndYearFormatter stringFromDate:self.baseDate];
}
#pragma mark Low-level implementation details
- (NSUInteger)numberOfDaysInPreviousPartialWeek
{
int num = (int)[self.baseDate cc_weekday] - 1;
if (num == 0)
num = 7;
return num;
}
- (NSUInteger)numberOfDaysInFollowingPartialWeek
{
NSDateComponents *c = [self.baseDate cc_componentsForMonthDayAndYear];
c.day = [self.baseDate cc_numberOfDaysInMonth];
NSDate *lastDayOfTheMonth = [[NSCalendar currentCalendar] dateFromComponents:c];
int num =(int) (7 - [lastDayOfTheMonth cc_weekday]);
if (num == 0)
num = 7;
return num;
}
- (NSArray *)calculateDaysInFinalWeekOfPreviousMonth
{
NSMutableArray *days = [NSMutableArray array];
NSDate *beginningOfPreviousMonth = [self.baseDate cc_dateByMovingToFirstDayOfThePreviousMonth];
int n = (int)[beginningOfPreviousMonth cc_numberOfDaysInMonth];
int numPartialDays = (int)[self numberOfDaysInPreviousPartialWeek];
NSDateComponents *c = [beginningOfPreviousMonth cc_componentsForMonthDayAndYear];
for (int i = n - (numPartialDays - 1); i < n + 1; i++)
[days addObject:[NSDate dateForDay:i month:c.month year:c.year]];
return days;
}
- (NSArray *)calculateDaysInSelectedMonth
{
NSMutableArray *days = [NSMutableArray array];
NSUInteger numDays = [self.baseDate cc_numberOfDaysInMonth];
NSDateComponents *c = [self.baseDate cc_componentsForMonthDayAndYear];
for (int i = 1; i < numDays + 1; i++)
[days addObject:[NSDate dateForDay:i month:c.month year:c.year]];
return days;
}
- (NSArray *)calculateDaysInFirstWeekOfFollowingMonth
{
NSMutableArray *days = [NSMutableArray array];
NSDateComponents *c = [[self.baseDate cc_dateByMovingToFirstDayOfTheFollowingMonth] cc_componentsForMonthDayAndYear];
NSUInteger numPartialDays = [self numberOfDaysInFollowingPartialWeek];
for (int i = 1; i < numPartialDays + 1; i++)
[days addObject:[NSDate dateForDay:i month:c.month year:c.year]];
return days;
}
- (void)recalculateVisibleDays
{
self.daysInSelectedMonth = [self calculateDaysInSelectedMonth];
self.daysInFinalWeekOfPreviousMonth = [self calculateDaysInFinalWeekOfPreviousMonth];
self.daysInFirstWeekOfFollowingMonth = [self calculateDaysInFirstWeekOfFollowingMonth];
NSDate *from = [self.daysInFinalWeekOfPreviousMonth count] > 0 ? [self.daysInFinalWeekOfPreviousMonth objectAtIndex:0] : [self.daysInSelectedMonth objectAtIndex:0];
NSDate *to = [self.daysInFirstWeekOfFollowingMonth count] > 0 ? [self.daysInFirstWeekOfFollowingMonth lastObject] : [self.daysInSelectedMonth lastObject];
self.fromDate = [from cc_dateByMovingToBeginningOfDay];
self.toDate = [to cc_dateByMovingToEndOfDay];
}
#pragma mark -
@end