Skip to content

Commit

Permalink
Merge pull request MatthewYork#30 from bengotow/master
Browse files Browse the repository at this point in the history
Use -weekOfYear instead of -week to remove iOS 7 deprecation warnings.
  • Loading branch information
Matthew York committed Sep 3, 2014
2 parents a3141f0 + 203f72d commit 2a310da
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions DateTools/NSDate+DateTools.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ typedef NS_ENUM(NSUInteger, DTDateComponent){
DTDateComponentDayOfYear
};

static const unsigned int allCalendarUnitFlags = NSYearCalendarUnit | NSQuarterCalendarUnit | NSMonthCalendarUnit | NSWeekOfYearCalendarUnit | NSWeekOfMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSEraCalendarUnit | NSWeekdayCalendarUnit | NSWeekdayOrdinalCalendarUnit | NSWeekCalendarUnit;
static const unsigned int allCalendarUnitFlags = NSYearCalendarUnit | NSQuarterCalendarUnit | NSMonthCalendarUnit | NSWeekOfYearCalendarUnit | NSWeekOfMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSEraCalendarUnit | NSWeekdayCalendarUnit | NSWeekdayOrdinalCalendarUnit | NSWeekOfYearCalendarUnit;

static NSString *defaultCalendarIdentifier = nil;
static NSCalendar *implicitCalendar = nil;
Expand Down Expand Up @@ -104,7 +104,7 @@ - (NSString *)timeAgoSinceDate:(NSDate *)date{
- (NSString *)timeAgoSinceDate:(NSDate *)date numericDates:(BOOL)useNumericDates{

NSCalendar *calendar = [NSCalendar currentCalendar];
NSUInteger unitFlags = NSMinuteCalendarUnit | NSHourCalendarUnit | NSDayCalendarUnit | NSWeekCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit | NSSecondCalendarUnit;
NSUInteger unitFlags = NSMinuteCalendarUnit | NSHourCalendarUnit | NSDayCalendarUnit | NSWeekOfYearCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit | NSSecondCalendarUnit;
NSDate *earliest = [self earlierDate:date];
NSDate *latest = (earliest == self) ? date : self;
NSDateComponents *components = [calendar components:unitFlags fromDate:earliest toDate:latest options:0];
Expand Down Expand Up @@ -136,10 +136,10 @@ - (NSString *)timeAgoSinceDate:(NSDate *)date numericDates:(BOOL)useNumericDates

return DateToolsLocalizedStrings(@"Last month");
}
else if (components.week >= 2) {
return [self logicLocalizedStringFromFormat:@"%%d %@weeks ago" withValue:components.week];
else if (components.weekOfYear >= 2) {
return [self logicLocalizedStringFromFormat:@"%%d %@weeks ago" withValue:components.weekOfYear];
}
else if (components.week >= 1) {
else if (components.weekOfYear >= 1) {

if (useNumericDates) {
return DateToolsLocalizedStrings(@"1 week ago");
Expand Down Expand Up @@ -185,7 +185,7 @@ - (NSString *)shortTimeAgoSinceDate:(NSDate *)date{
//use abbreviated unit names

NSCalendar *calendar = [NSCalendar currentCalendar];
NSUInteger unitFlags = NSMinuteCalendarUnit | NSHourCalendarUnit | NSDayCalendarUnit | NSWeekCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit | NSSecondCalendarUnit;
NSUInteger unitFlags = NSMinuteCalendarUnit | NSHourCalendarUnit | NSDayCalendarUnit | NSWeekOfYearCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit | NSSecondCalendarUnit;
NSDate *earliest = [self earlierDate:date];
NSDate *latest = (earliest == self) ? date : self;
NSDateComponents *components = [calendar components:unitFlags fromDate:earliest toDate:latest options:0];
Expand All @@ -197,8 +197,8 @@ - (NSString *)shortTimeAgoSinceDate:(NSDate *)date{
else if (components.month >= 1) {
return [self logicLocalizedStringFromFormat:@"%%d%@M" withValue:components.month];
}
else if (components.week >= 1) {
return [self logicLocalizedStringFromFormat:@"%%d%@w" withValue:components.week];
else if (components.weekOfYear >= 1) {
return [self logicLocalizedStringFromFormat:@"%%d%@w" withValue:components.weekOfYear];
}
else if (components.day >= 1) {
return [self logicLocalizedStringFromFormat:@"%%d%@d" withValue:components.day];
Expand Down Expand Up @@ -628,7 +628,7 @@ -(NSInteger)componentForDate:(NSDate *)date type:(DTDateComponent)component cale
unsigned int unitFlags = 0;

if (component == DTDateComponentYearForWeekOfYear) {
unitFlags = NSYearCalendarUnit | NSQuarterCalendarUnit | NSMonthCalendarUnit | NSWeekOfYearCalendarUnit | NSWeekOfMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSEraCalendarUnit | NSWeekdayCalendarUnit | NSWeekdayOrdinalCalendarUnit | NSWeekCalendarUnit | NSYearForWeekOfYearCalendarUnit;
unitFlags = NSYearCalendarUnit | NSQuarterCalendarUnit | NSMonthCalendarUnit | NSWeekOfYearCalendarUnit | NSWeekOfMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSEraCalendarUnit | NSWeekdayCalendarUnit | NSWeekdayOrdinalCalendarUnit | NSWeekOfYearCalendarUnit | NSYearForWeekOfYearCalendarUnit;
}
else {
unitFlags = allCalendarUnitFlags;
Expand Down Expand Up @@ -714,7 +714,7 @@ - (NSDate *)dateByAddingMonths:(NSInteger)months{
- (NSDate *)dateByAddingWeeks:(NSInteger)weeks{
NSCalendar *calendar = [[self class] implicitCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setWeek:weeks];
[components setWeekOfYear:weeks];

return [calendar dateByAddingComponents:components toDate:self options:0];
}
Expand Down Expand Up @@ -820,7 +820,7 @@ - (NSDate *)dateBySubtractingMonths:(NSInteger)months{
- (NSDate *)dateBySubtractingWeeks:(NSInteger)weeks{
NSCalendar *calendar = [[self class] implicitCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setWeek:-1*weeks];
[components setWeekOfYear:-1*weeks];

return [calendar dateByAddingComponents:components toDate:self options:0];
}
Expand Down Expand Up @@ -1035,8 +1035,8 @@ -(NSInteger)weeksFrom:(NSDate *)date calendar:(NSCalendar *)calendar{
NSDate *earliest = [self earlierDate:date];
NSDate *latest = (earliest == self) ? date : self;
NSInteger multiplier = (earliest == self) ? -1 : 1;
NSDateComponents *components = [calendar components:NSWeekCalendarUnit fromDate:earliest toDate:latest options:0];
return multiplier*components.week;
NSDateComponents *components = [calendar components:NSWeekOfYearCalendarUnit fromDate:earliest toDate:latest options:0];
return multiplier*components.weekOfYear;
}

/**
Expand Down

0 comments on commit 2a310da

Please sign in to comment.