From b4af872b576b245325d7ee26e73452a5f1b4a08a Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Tue, 10 Dec 2019 10:54:26 +0300 Subject: Drop CocoaPods --- Juick/Helpers/NSDate+TimeAgo.h | 19 +++++++++++++++++ Juick/Helpers/NSDate+TimeAgo.m | 46 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 Juick/Helpers/NSDate+TimeAgo.h create mode 100644 Juick/Helpers/NSDate+TimeAgo.m (limited to 'Juick/Helpers') diff --git a/Juick/Helpers/NSDate+TimeAgo.h b/Juick/Helpers/NSDate+TimeAgo.h new file mode 100644 index 0000000..5ba31a7 --- /dev/null +++ b/Juick/Helpers/NSDate+TimeAgo.h @@ -0,0 +1,19 @@ +// +// TimeAgo.h +// Juick +// +// Created by Vitaly Takmazov on 10.12.2019. +// Copyright © 2019 com.juick. All rights reserved. +// + +#import + +NS_ASSUME_NONNULL_BEGIN + +@interface NSDate(TimeAgo) + +- (NSString *)timeAgo; + +@end + +NS_ASSUME_NONNULL_END diff --git a/Juick/Helpers/NSDate+TimeAgo.m b/Juick/Helpers/NSDate+TimeAgo.m new file mode 100644 index 0000000..860e1bc --- /dev/null +++ b/Juick/Helpers/NSDate+TimeAgo.m @@ -0,0 +1,46 @@ +// +// TimeAgo.m +// Juick +// +// Created by Vitaly Takmazov on 10.12.2019. +// Copyright © 2019 com.juick. All rights reserved. +// + +#import "NSDate+TimeAgo.h" + +@implementation NSDate(TimeAgo) + +- (NSString *)timeAgo { + NSDateComponentsFormatter *formatter = [[NSDateComponentsFormatter alloc] init]; + formatter.unitsStyle = NSDateComponentsFormatterUnitsStyleFull; + + NSDate *now = [NSDate date]; + + NSCalendar *calendar = [NSCalendar currentCalendar]; + NSDateComponents *components = [calendar components:(NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitWeekOfMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond) + fromDate:self + toDate:now + options:0]; + + if (components.year > 0) { + formatter.allowedUnits = NSCalendarUnitYear; + } else if (components.month > 0) { + formatter.allowedUnits = NSCalendarUnitMonth; + } else if (components.weekOfMonth > 0) { + formatter.allowedUnits = NSCalendarUnitWeekOfMonth; + } else if (components.day > 0) { + formatter.allowedUnits = NSCalendarUnitDay; + } else if (components.hour > 0) { + formatter.allowedUnits = NSCalendarUnitHour; + } else if (components.minute > 0) { + formatter.allowedUnits = NSCalendarUnitMinute; + } else { + formatter.allowedUnits = NSCalendarUnitSecond; + } + + NSString *formatString = NSLocalizedString(@"%@ ago", @"Used to say how much time has passed. e.g. '2 hours ago'"); + + return [NSString stringWithFormat:formatString, [formatter stringFromDateComponents:components]]; +} + +@end -- cgit v1.2.3