You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
session-ios/Signal/src/util/DateUtil.m

101 lines
3.1 KiB
Matlab

//
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
//
11 years ago
#import "DateUtil.h"
#import <SignalServiceKit/NSDate+OWS.h>
11 years ago
static NSString *const DATE_FORMAT_WEEKDAY = @"EEEE";
@implementation DateUtil
+ (NSDateFormatter *)dateFormatter {
static NSDateFormatter *formatter;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
formatter = [NSDateFormatter new];
[formatter setLocale:[NSLocale currentLocale]];
[formatter setTimeStyle:NSDateFormatterNoStyle];
[formatter setDateStyle:NSDateFormatterShortStyle];
});
return formatter;
11 years ago
}
+ (NSDateFormatter *)weekdayFormatter {
static NSDateFormatter *formatter;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
formatter = [NSDateFormatter new];
[formatter setLocale:[NSLocale currentLocale]];
[formatter setDateFormat:DATE_FORMAT_WEEKDAY];
});
return formatter;
11 years ago
}
+ (NSDateFormatter *)timeFormatter {
static NSDateFormatter *formatter;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
formatter = [NSDateFormatter new];
[formatter setLocale:[NSLocale currentLocale]];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateStyle:NSDateFormatterNoStyle];
});
return formatter;
11 years ago
}
+ (BOOL)dateIsOlderThanOneDay:(NSDate *)date {
return [[NSDate date] timeIntervalSinceDate:date] > kDayInterval;
11 years ago
}
+ (BOOL)dateIsOlderThanOneWeek:(NSDate *)date {
return [[NSDate date] timeIntervalSinceDate:date] > kWeekInterval;
11 years ago
}
+ (BOOL)date:(NSDate *)date isEqualToDateIgnoringTime:(NSDate *)anotherDate {
static const unsigned componentFlags = (NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay);
NSDateComponents *components1 = [[NSCalendar autoupdatingCurrentCalendar] components:componentFlags fromDate:date];
NSDateComponents *components2 =
[[NSCalendar autoupdatingCurrentCalendar] components:componentFlags fromDate:anotherDate];
return ((components1.year == components2.year) && (components1.month == components2.month) &&
(components1.day == components2.day));
}
+ (BOOL)dateIsToday:(NSDate *)date {
return [self date:[NSDate date] isEqualToDateIgnoringTime:date];
}
+ (NSString *)formatPastTimestampRelativeToNow:(uint64_t)pastTimestamp
{
OWSCAssert(pastTimestamp > 0);
uint64_t nowTimestamp = [NSDate ows_millisecondTimeStamp];
if (pastTimestamp >= nowTimestamp) {
OWSFail(@"%@ Unexpected timestamp", self.tag);
return NSLocalizedString(@"TIME_NOW", @"Indicates that the event happened now.");
}
NSDate *pastDate = [NSDate ows_dateWithMillisecondsSince1970:pastTimestamp];
if ([self dateIsToday:pastDate]) {
return [[self timeFormatter] stringFromDate:pastDate];
} else if (![self dateIsOlderThanOneWeek:pastDate]) {
return [[self weekdayFormatter] stringFromDate:pastDate];
} else {
return [[self dateFormatter] stringFromDate:pastDate];
}
}
#pragma mark - Logging
+ (NSString *)tag
{
return [NSString stringWithFormat:@"[%@]", self.class];
}
- (NSString *)tag
{
return self.class.tag;
}
11 years ago
@end