0N/A/*
0N/A * The contents of this file are subject to the terms of the Common Development and
0N/A * Distribution License (the License). You may not use this file except in compliance with the
0N/A * License.
0N/A *
157N/A * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
0N/A * specific language governing permission and limitations under the License.
0N/A *
0N/A * When distributing Covered Software, include this CDDL Header Notice in each file and include
0N/A * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
157N/A * Header, with the fields enclosed by brackets [] replaced by your own identifying
0N/A * information: "Portions copyright [year] [name of copyright owner]".
157N/A *
0N/A * Copyright 2016 ForgeRock AS.
0N/A */
0N/A
0N/A#import "FRADateUtils.h"
0N/A
0N/A@implementation FRADateUtils
0N/A
0N/A- (NSString *)ageOfEventTime:(NSDate *)eventTime {
0N/A NSDate *currentTime = [NSDate date];
0N/A NSLocale *locale = [NSLocale currentLocale];
0N/A NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
157N/A NSString *shortTimeFormat = [NSDateFormatter dateFormatFromTemplate:@"HHmm" options:0 locale:locale];
157N/A [timeFormatter setDateFormat:shortTimeFormat];
157N/A
0N/A NSDateFormatter *dayOfWeekFormatter = [[NSDateFormatter alloc] init];
0N/A [dayOfWeekFormatter setDateFormat:@"EEEE"];
0N/A // Although iOS will give day of the week in the correct language for the device's locale/language settings
0N/A // we should instead perform explicit translation ourselves. This ensures that the app's localizable strings
0N/A // are fully translated or not translated at all - It would seem odd to only show day of the week in the
0N/A // device's language while all other strings are shown in English.
0N/A [dayOfWeekFormatter setLocale:[NSLocale localeWithLocaleIdentifier:@"en_GB"]];
0N/A
0N/A NSDateFormatter *localeDateFormatter = [[NSDateFormatter alloc] init];
0N/A NSString *shortDateFormat = [NSDateFormatter dateFormatFromTemplate:@"ddMMyyyy" options:0 locale:locale];
0N/A [localeDateFormatter setDateFormat:shortDateFormat];
0N/A
0N/A NSDate *now = currentTime;
0N/A NSDate *midnight = [self midnightOfDate:now];
NSTimeInterval secondsSinceMidnight = [now timeIntervalSinceDate:midnight];
NSTimeInterval secondsSinceNotification = [now timeIntervalSinceDate:eventTime];
NSTimeInterval secondsInDay = (NSTimeInterval) 24.0 * 60.0 * 60.0;
if (secondsSinceNotification < secondsSinceMidnight) {
return [timeFormatter stringFromDate:eventTime];
} else if (secondsSinceNotification < (secondsSinceMidnight + secondsInDay)) {
return NSLocalizedString(@"yesterday", nil);
} else if (secondsSinceNotification < (secondsSinceMidnight + (7 * secondsInDay))) {
return NSLocalizedString([dayOfWeekFormatter stringFromDate:eventTime], nil);
}
return [localeDateFormatter stringFromDate:eventTime];
}
- (NSDate *)midnightOfDate:(NSDate *)date {
NSCalendar *calendar = [NSCalendar currentCalendar];
NSCalendarUnit preservedComponents = (NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay);
NSDateComponents *components = [calendar components:preservedComponents fromDate:date];
return [calendar dateFromComponents:components];
}
@end