Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ARC support added #6

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions NSDate+Helper.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,18 @@
@implementation NSDate (Helper)

+ (void)load {
#if __has_feature(objc_arc)
@autoreleasepool {
displayFormatter = [[NSDateFormatter alloc] init];
calendar = [NSCalendar currentCalendar];
}
#else
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

calendar = [[NSCalendar currentCalendar] retain];
displayFormatter = [[NSDateFormatter alloc] init];

calendar = [NSCalendar currentCalendar];
[calendar retain];
[pool drain];
#endif
}

/*
Expand All @@ -61,7 +67,9 @@ - (NSUInteger)daysAgoAgainstMidnight {
NSDateFormatter *mdf = [[NSDateFormatter alloc] init];
[mdf setDateFormat:@"yyyy-MM-dd"];
NSDate *midnight = [mdf dateFromString:[mdf stringFromDate:self]];
#if !__has_feature(objc_arc)
[mdf release];
#endif

return (int)[midnight timeIntervalSinceNow] / (60*60*24) *-1;
}
Expand Down Expand Up @@ -99,7 +107,9 @@ + (NSDate *)dateFromString:(NSString *)string withFormat:(NSString *)format {
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateFormat:format];
NSDate *date = [inputFormatter dateFromString:string];
#if !__has_feature(objc_arc)
[inputFormatter release];
#endif
return date;
}

Expand Down Expand Up @@ -140,7 +150,9 @@ + (NSString *)stringForDisplayFromDate:(NSDate *)date prefixed:(BOOL)prefixed al
NSDateComponents *componentsToSubtract = [[NSDateComponents alloc] init];
[componentsToSubtract setDay:-7];
NSDate *lastweek = [calendar dateByAddingComponents:componentsToSubtract toDate:today options:0];
#if !__has_feature(objc_arc)
[componentsToSubtract release];
#endif
if ([date compare:lastweek] == NSOrderedDescending) {
if (displayTime)
[displayFormatter setDateFormat:@"EEEE h:mm a"]; // Tuesday
Expand Down Expand Up @@ -190,7 +202,9 @@ - (NSString *)stringWithFormat:(NSString *)format {
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:format];
NSString *timestamp_str = [outputFormatter stringFromDate:self];
#if !__has_feature(objc_arc)
[outputFormatter release];
#endif
return timestamp_str;
}

Expand All @@ -203,7 +217,9 @@ - (NSString *)stringWithDateStyle:(NSDateFormatterStyle)dateStyle timeStyle:(NSD
[outputFormatter setDateStyle:dateStyle];
[outputFormatter setTimeStyle:timeStyle];
NSString *outputString = [outputFormatter stringFromDate:self];
#if !__has_feature(objc_arc)
[outputFormatter release];
#endif
return outputString;
}

Expand All @@ -230,8 +246,9 @@ - (NSDate *)beginningOfWeek {
[componentsToSubtract setDay: 0 - ([weekdayComponents weekday] - 1)];
beginningOfWeek = nil;
beginningOfWeek = [calendar dateByAddingComponents:componentsToSubtract toDate:self options:0];
#if !__has_feature(objc_arc)
[componentsToSubtract release];
#endif
//normalize to midnight, extract the year, month, and day components and create a new date from those components.
NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)
fromDate:beginningOfWeek];
Expand All @@ -252,7 +269,9 @@ - (NSDate *)endOfWeek {
// to get the end of week for a particular date, add (7 - weekday) days
[componentsToAdd setDay:(7 - [weekdayComponents weekday])];
NSDate *endOfWeek = [calendar dateByAddingComponents:componentsToAdd toDate:self options:0];
#if !__has_feature(objc_arc)
[componentsToAdd release];
#endif

return endOfWeek;
}
Expand Down