This repository has been archived by the owner on Feb 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
NSCalendarDate+RelativeDateDescription.m
71 lines (64 loc) · 2.77 KB
/
NSCalendarDate+RelativeDateDescription.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
//
// NSCalendarDate+RelativeDateDescription.m
// ScrobblePod
//
// Created by Ben Gummer on 20/04/07.
// Copyright 2007 Ben Gummer. All rights reserved.
//
//
#import "NSCalendarDate+RelativeDateDescription.h"
@implementation NSCalendarDate (RelativeDateDescription)
-(NSString *)relativeDateDescription {
NSCalendarDate *now = [NSCalendarDate calendarDate];
NSMutableString *relativeDateDescription = [NSMutableString new];
[relativeDateDescription appendString:[self descriptionWithCalendarFormat:@"%I:%M"]];
if ([self dateIsInToday]) {
if ([self hourOfDay]<12) {
[relativeDateDescription appendString:@" this morning"];
} else if ([self hourOfDay]>17) {
[relativeDateDescription appendString:@" this evening"];
} else {
[relativeDateDescription appendString:@" this afternoon"];
}
} else if ([self dateIsInLastTwoWeeks]) {
int weekMultiplier = 1;
int weekAddition = 0;
if ([self dateIsInCurrentWeek]) {
weekMultiplier = -1;
weekAddition = -1;
}
if (((([self dayOfWeek]-[now dayOfWeek])*weekMultiplier)+weekAddition) >= 0) {
[relativeDateDescription appendString:[self descriptionWithCalendarFormat:@" %p"]];
[relativeDateDescription appendString:@" last"];
[relativeDateDescription appendString:[self descriptionWithCalendarFormat:@" %A"]];
} else {
[relativeDateDescription appendString:[self descriptionWithCalendarFormat:@" %p"]];
[relativeDateDescription appendString:@" on "];
[relativeDateDescription appendString:[self descriptionWithCalendarFormat:@"%A %B %d"]];
}
} else {
[relativeDateDescription appendString:[self descriptionWithCalendarFormat:@" %p"]];
[relativeDateDescription appendString:@" on "];
[relativeDateDescription appendString:[self descriptionWithCalendarFormat:@"%A %B %d"]];
}
return relativeDateDescription;
}
-(BOOL)dateIsInToday {
return ([[NSCalendarDate calendarDate] dayOfCommonEra]==[self dayOfCommonEra]);
}
-(BOOL)dateIsInCurrentWeek {
NSCalendarDate *mondayOfThisWeek = [NSCalendarDate calendarDate];
while ([mondayOfThisWeek dayOfWeek]!=1) {
mondayOfThisWeek = [mondayOfThisWeek dateByAddingYears:0 months:0 days:-1 hours:0 minutes:0 seconds:0];
}
return ([self dayOfCommonEra]>=[mondayOfThisWeek dayOfCommonEra] && [self dayOfCommonEra]<=[[NSCalendarDate calendarDate] dayOfCommonEra]);
}
-(BOOL)dateIsInLastTwoWeeks {
NSCalendarDate *mondayOfThisWeek = [NSCalendarDate calendarDate];
while ([mondayOfThisWeek dayOfWeek]!=1) {
mondayOfThisWeek = [mondayOfThisWeek dateByAddingYears:0 months:0 days:-1 hours:0 minutes:0 seconds:0];
}
NSCalendarDate *mondayOfLastWeek = [mondayOfThisWeek dateByAddingYears:0 months:0 days:-7 hours:0 minutes:0 seconds:0];
return ([self dayOfCommonEra]>=[mondayOfLastWeek dayOfCommonEra] && [self dayOfCommonEra]<=[[NSCalendarDate calendarDate] dayOfCommonEra]);
}
@end