-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCal.m
59 lines (49 loc) · 1.37 KB
/
Cal.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
//
// Cal.m
// Allowance
//
// Created by Pablo Collins on 7/24/10.
//
#import "Cal.h"
@implementation Cal
+ (NSCalendar *)sharedInstance {
static NSCalendar *me;
if (me == nil) {
me = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
}
return me;
}
+ (NSDate *)startDate {
static NSDate *start;
if (start == nil) {
NSCalendar *cal = [Cal sharedInstance];
NSDateComponents *cmp = [[NSDateComponents alloc] init];
[cmp setYear:2010];
[cmp setMonth:1];
[cmp setDay:3];
start = [cal dateFromComponents:cmp];
}
return start;
}
+ (NSUInteger)weekNumForDate:(NSDate *)d {
NSDate *startDate = [Cal startDate];
NSTimeInterval ti = [d timeIntervalSinceDate:startDate];
int out = ti / (60*60*24*7);
return out;
}
+ (NSDate *)dateForWeeknum:(NSUInteger)n {
return [self dateForWeeknum:n andWeekday:1];
}
+ (NSDate *)dateForWeeknum:(NSUInteger)n andWeekday:(NSUInteger)w {
NSTimeInterval ti = ((n * 7) + w - 1) * 60*60*24;
return [NSDate dateWithTimeInterval:ti sinceDate:[Cal startDate]];
}
+ (NSInteger)currentWeek {
return [Cal weekNumForDate:[NSDate date]];
}
+ (NSInteger)currentDay {
NSCalendar *cal = [Cal sharedInstance];
NSDateComponents *cmp = [cal components:(NSWeekdayCalendarUnit) fromDate:[NSDate date]];
return [cmp weekday];
}
@end