forked from robbiehanson/AlarmClock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRHDateToStringTransformer.m
82 lines (63 loc) · 2.59 KB
/
RHDateToStringTransformer.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
#import "RHDateToStringTransformer.h"
@implementation RHDateToStringTransformer
// CLASS METHODS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+ (Class)transformedValueClass;
{
return [NSString class];
}
+ (BOOL)allowsReverseTransformation;
{
return NO;
}
// TRANSFORMING WORK
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (id)transformedValue:(id)value;
{
if(![value isKindOfClass:[NSDate class]])
return nil;
NSCalendarDate *date;
if([value isKindOfClass:[NSCalendarDate class]])
date = value;
else
date = [value dateWithCalendarFormat:nil timeZone:nil];
int today = [[NSCalendarDate calendarDate] dayOfCommonEra];
int dateDay = [date dayOfCommonEra];
if(dateDay == today)
{
// NSUserDefaults Constant: NSThisDayDesignations:
// Key for an array of strings that specify what this day is called.
// The default is an array containing two strings, "today" and "now".
NSArray *todayDesignations;
todayDesignations = [[NSUserDefaults standardUserDefaults] stringArrayForKey:NSThisDayDesignations];
NSString *todayStr = [[todayDesignations objectAtIndex:0] capitalizedString];
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setFormatterBehavior:NSDateFormatterBehavior10_4];
[df setDateStyle:NSDateFormatterNoStyle];
[df setTimeStyle:NSDateFormatterShortStyle];
return [NSString stringWithFormat:@"%@ %@", todayStr, [df stringFromDate:date]];
}
else if(dateDay == (today-1))
{
// NSUserDefaults Constant: NSPriorDayDesignations:
// Key for an array of strings that denote the day before today.
// The default is an array that contains a single string, "yesterday".
NSArray *yesterdayDesignations;
yesterdayDesignations = [[NSUserDefaults standardUserDefaults] stringArrayForKey:NSPriorDayDesignations];
NSString *yesterdayStr = [[yesterdayDesignations objectAtIndex:0] capitalizedString];
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setFormatterBehavior:NSDateFormatterBehavior10_4];
[df setDateStyle:NSDateFormatterNoStyle];
[df setTimeStyle:NSDateFormatterShortStyle];
return [NSString stringWithFormat:@"%@ %@", yesterdayStr, [df stringFromDate:date]];
}
else
{
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setFormatterBehavior:NSDateFormatterBehavior10_4];
[df setDateStyle:NSDateFormatterMediumStyle];
[df setTimeStyle:NSDateFormatterShortStyle];
return [df stringFromDate:date];
}
}
@end