Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion DateTools/DateTools/NSDate+DateTools.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ NSLocalizedStringFromTableInBundle(key, @"DateTools", [NSBundle bundleWithPath:[
- (NSString *)timeAgoSinceDate:(NSDate *)date;
- (NSString *)timeAgoSinceDate:(NSDate *)date numericDates:(BOOL)useNumericDates;
- (NSString *)timeAgoSinceDate:(NSDate *)date numericDates:(BOOL)useNumericDates numericTimes:(BOOL)useNumericTimes;

- (NSString *)timeAgoSinceDate:(NSDate *)date numericDates:(BOOL)useNumericDates numericTimes:(BOOL)useNumericTimes minimumHoursAgo:(NSInteger)minimumHoursAgo;

- (NSString *)shortTimeAgoSinceDate:(NSDate *)date;
- (NSString *)weekTimeAgoSinceDate:(NSDate *)date;
Expand Down
37 changes: 24 additions & 13 deletions DateTools/DateTools/NSDate+DateTools.m
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,18 @@ - (NSString *)timeAgoSinceDate:(NSDate *)date numericDates:(BOOL)useNumericDates
}

- (NSString *)timeAgoSinceDate:(NSDate *)date numericDates:(BOOL)useNumericDates numericTimes:(BOOL)useNumericTimes{
return [self timeAgoSinceDate:date numericDates:useNumericDates numericTimes:useNumericTimes minimumHoursAgo:1];
}

- (NSString *)timeAgoSinceDate:(NSDate *)date numericDates:(BOOL)useNumericDates numericTimes:(BOOL)useNumericTimes minimumHoursAgo:(NSInteger)minimumHoursAgo{
if (useNumericDates && useNumericTimes) {
return [self timeAgoSinceDate:date format:DateAgoLongUsingNumericDatesAndTimes];
return [self timeAgoSinceDate:date format:DateAgoLongUsingNumericDatesAndTimes minimumHoursAgo:minimumHoursAgo];
} else if (useNumericDates) {
return [self timeAgoSinceDate:date format:DateAgoLongUsingNumericDates];
return [self timeAgoSinceDate:date format:DateAgoLongUsingNumericDates minimumHoursAgo:minimumHoursAgo];
} else if (useNumericTimes) {
return [self timeAgoSinceDate:date format:DateAgoLongUsingNumericDates];
return [self timeAgoSinceDate:date format:DateAgoLongUsingNumericDates minimumHoursAgo:minimumHoursAgo];
} else {
return [self timeAgoSinceDate:date format:DateAgoLong];
return [self timeAgoSinceDate:date format:DateAgoLong minimumHoursAgo:minimumHoursAgo];
}
}

Expand All @@ -152,25 +156,32 @@ - (NSString *)weekTimeAgoSinceDate:(NSDate *)date{
return [self timeAgoSinceDate:date format:DateAgoWeek];
}

- (NSString *)timeAgoSinceDate:(NSDate *)date format:(DateAgoFormat)format {
- (NSString *)timeAgoSinceDate:(NSDate *)date format:(DateAgoFormat)format{
[self timeAgoSinceDate:date format:format minimumHoursAgo:1];
}

- (NSString *)timeAgoSinceDate:(NSDate *)date format:(DateAgoFormat)format minimumHoursAgo:(NSInteger)minimumHoursAgo {

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *earliest = [self earlierDate:date];
NSDate *latest = (earliest == self) ? date : self;

// if timeAgo < 24h => compare DateTime else compare Date only
NSUInteger upToHours = NSCalendarUnitSecond | NSCalendarUnitMinute | NSCalendarUnitHour;
NSDateComponents *difference = [calendar components:upToHours fromDate:earliest toDate:latest options:0];

if (difference.hour < 24) {
if (difference.hour >= 1) {
return [self localizedStringFor:format valueType:HoursAgo value:difference.hour];
} else if (difference.minute >= 1) {
return [self localizedStringFor:format valueType:MinutesAgo value:difference.minute];
if(difference.hour < minimumHoursAgo){
upToHours = NSCalendarUnitSecond | NSCalendarUnitMinute;
difference = [calendar components:upToHours fromDate:earliest toDate:latest options:0];
if (difference.minute >= 1) {
return [self localizedStringFor:format valueType:MinutesAgo value:difference.minute];
} else {
return [self localizedStringFor:format valueType:SecondsAgo value:difference.second];
}
} else {
return [self localizedStringFor:format valueType:SecondsAgo value:difference.second];
return [self localizedStringFor:format valueType:HoursAgo value:difference.hour];
}

} else {
NSUInteger bigUnits = NSCalendarUnitTimeZone | NSCalendarUnitDay | NSCalendarUnitWeekOfYear | NSCalendarUnitMonth | NSCalendarUnitYear;

Expand All @@ -179,7 +190,7 @@ - (NSString *)timeAgoSinceDate:(NSDate *)date format:(DateAgoFormat)format {

components = [calendar components:bigUnits fromDate:latest];
latest = [calendar dateFromComponents:components];

difference = [calendar components:bigUnits fromDate:earliest toDate:latest options:0];

if (difference.year >= 1) {
Expand Down