From 8e65c49592dab2880fa597bbc2f64479f01b080c Mon Sep 17 00:00:00 2001 From: Jan Lubeck Date: Tue, 18 Sep 2018 15:57:05 -0400 Subject: [PATCH] Added option for minimumHours on timeAgoSinceDate --- DateTools/DateTools/NSDate+DateTools.h | 2 +- DateTools/DateTools/NSDate+DateTools.m | 37 +++++++++++++++++--------- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/DateTools/DateTools/NSDate+DateTools.h b/DateTools/DateTools/NSDate+DateTools.h index 94231a75..42f88820 100644 --- a/DateTools/DateTools/NSDate+DateTools.h +++ b/DateTools/DateTools/NSDate+DateTools.h @@ -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; diff --git a/DateTools/DateTools/NSDate+DateTools.m b/DateTools/DateTools/NSDate+DateTools.m index 63d16a85..9e664653 100644 --- a/DateTools/DateTools/NSDate+DateTools.m +++ b/DateTools/DateTools/NSDate+DateTools.m @@ -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]; } } @@ -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; @@ -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) {