Skip to content

Commit

Permalink
Applies fixes for ARC from patch manually—could use some testing
Browse files Browse the repository at this point in the history
  • Loading branch information
billymeltdown committed Mar 26, 2014
1 parent 7fe666f commit fbf1252
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions NSDate+Helper.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,17 @@ @implementation NSDate (Helper)
+ (void)initializeStatics {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if (_calendar == nil) {
_calendar = [[NSCalendar currentCalendar] retain];
}
if (_displayFormatter == nil) {
_displayFormatter = [[NSDateFormatter alloc] init];
@autoreleasepool {
if (_calendar == nil) {
#if __has_feature(objc_arc)
_calendar = [NSCalendar currentCalendar];
#else
_calendar = [[NSCalendar currentCalendar] retain];
#endif
}
if (_displayFormatter == nil) {
_displayFormatter = [[NSDateFormatter alloc] init];
}
}
});
}
Expand Down Expand Up @@ -141,10 +147,8 @@ + (NSString *)stringForDisplayFromDate:(NSDate *)date prefixed:(BOOL)prefixed al
NSDate *today = [NSDate date];
NSDateComponents *offsetComponents = [_calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)
fromDate:today];

NSDate *midnight = [_calendar dateFromComponents:offsetComponents];
NSString *displayString = nil;

// comparing against midnight
NSComparisonResult midnight_result = [date compare:midnight];
if (midnight_result == NSOrderedDescending) {
Expand All @@ -158,7 +162,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
NSComparisonResult lastweek_result = [date compare:lastweek];
if (lastweek_result == NSOrderedDescending) {
if (displayTime) {
Expand All @@ -169,7 +175,6 @@ + (NSString *)stringForDisplayFromDate:(NSDate *)date prefixed:(BOOL)prefixed al
} else {
// check if same calendar year
NSInteger thisYear = [offsetComponents year];

NSDateComponents *dateComponents = [_calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)
fromDate:date];
NSInteger thatYear = [dateComponents year];
Expand All @@ -195,7 +200,6 @@ + (NSString *)stringForDisplayFromDate:(NSDate *)date prefixed:(BOOL)prefixed al
[_displayFormatter setDateFormat:[prefix stringByAppendingString:dateFormat]];
}
}

// use display formatter to return formatted date string
displayString = [_displayFormatter stringFromDate:date];
return displayString;
Expand All @@ -210,6 +214,7 @@ + (NSString *)stringForDisplayFromDate:(NSDate *)date {
}

- (NSString *)stringWithFormat:(NSString *)format {
[[self class] initializeStatics];
[_displayFormatter setDateFormat:format];
NSString *timestamp_str = [_displayFormatter stringFromDate:self];
return timestamp_str;
Expand All @@ -220,27 +225,27 @@ - (NSString *)string {
}

- (NSString *)stringWithDateStyle:(NSDateFormatterStyle)dateStyle timeStyle:(NSDateFormatterStyle)timeStyle {
[[self class] initializeStatics];
[_displayFormatter setDateStyle:dateStyle];
[_displayFormatter setTimeStyle:timeStyle];
NSString *outputString = [_displayFormatter stringFromDate:self];
return outputString;
}

- (NSDate *)beginningOfWeek {
[[self class] initializeStatics];
// largely borrowed from "Date and Time Programming Guide for Cocoa"
// we'll use the default calendar and hope for the best
NSCalendar *calendar = [NSCalendar currentCalendar];
NSCalendar *calendar = _calendar;
NSDate *beginningOfWeek = nil;
BOOL ok = [calendar rangeOfUnit:NSWeekCalendarUnit startDate:&beginningOfWeek
interval:NULL forDate:self];
if (ok) {
return beginningOfWeek;
}

// couldn't calc via range, so try to grab Sunday, assuming gregorian style
// Get the weekday component of the current date
NSDateComponents *weekdayComponents = [calendar components:NSWeekdayCalendarUnit fromDate:self];

/*
Create a date components to represent the number of days to subtract from the current date.
The weekday value for Sunday in the Gregorian calendar is 1, so subtract 1 from the number of days to subtract from the date in question. (If today's Sunday, subtract 0 days.)
Expand All @@ -249,32 +254,36 @@ - (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];
return [calendar dateFromComponents:components];
}

- (NSDate *)beginningOfDay {
NSCalendar *calendar = [NSCalendar currentCalendar];
[[self class] initializeStatics];
NSCalendar *calendar = _calendar;
// Get the weekday component of the current date
NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)
fromDate:self];
return [calendar dateFromComponents:components];
}

- (NSDate *)endOfWeek {
NSCalendar *calendar = [NSCalendar currentCalendar];
[[self class] initializeStatics];
NSCalendar *calendar = _calendar;
// Get the weekday component of the current date
NSDateComponents *weekdayComponents = [calendar components:NSWeekdayCalendarUnit fromDate:self];
NSDateComponents *componentsToAdd = [[NSDateComponents alloc] init];
// 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

0 comments on commit fbf1252

Please sign in to comment.