Skip to content

Commit c7fa9ec

Browse files
committed
Extract date formatter creation
1 parent a5e00f8 commit c7fa9ec

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

Classes/GitXRelativeDateFormatter.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,5 @@
99
#import <Cocoa/Cocoa.h>
1010

1111

12-
@interface GitXRelativeDateFormatter : NSFormatter {
13-
14-
}
15-
12+
@interface GitXRelativeDateFormatter : NSFormatter
1613
@end

Classes/GitXRelativeDateFormatter.m

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
#define WEEK 7
1616

17-
1817
@implementation GitXRelativeDateFormatter
1918

2019
- (NSString *)stringForObjectValue:(id)date
@@ -41,11 +40,16 @@ - (NSString *)stringForObjectValue:(id)date
4140
if (secondsAgo < (2 * HOUR))
4241
return @"1 hour ago";
4342

43+
static NSDateFormatter *midnightFormatter = nil;
44+
static dispatch_once_t onceToken;
45+
dispatch_once(&onceToken, ^{
46+
midnightFormatter = [[NSDateFormatter alloc] init];
47+
midnightFormatter.dateFormat = @"yyyy-MM-dd";
48+
});
49+
4450
// figure out # of days ago based on calender days (so yesterday is the day before today not 24 hours ago)
45-
NSDateFormatter *midnightFormmatter = [[NSDateFormatter alloc] init];
46-
[midnightFormmatter setDateFormat:@"yyyy-MM-dd"];
47-
NSDate *midnightOnTargetDate = [midnightFormmatter dateFromString:[midnightFormmatter stringFromDate:date]];
48-
NSDate *midnightToday = [midnightFormmatter dateFromString:[midnightFormmatter stringFromDate:now]];
51+
NSDate *midnightOnTargetDate = [midnightFormatter dateFromString:[midnightFormatter stringFromDate:date]];
52+
NSDate *midnightToday = [midnightFormatter dateFromString:[midnightFormatter stringFromDate:now]];
4953

5054
// use NSCalendar so it will handle things like leap years correctly
5155
NSDateComponents *components = [[NSCalendar currentCalendar] components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay)

0 commit comments

Comments
 (0)