From 12c97722e74346d8adcfbe52233cfc5efbe739d5 Mon Sep 17 00:00:00 2001 From: Jason Ji Date: Thu, 5 Apr 2018 22:08:47 -0400 Subject: [PATCH 1/2] Bug fix for timeAgo where a date that should be "yesterday" is instead reading an incorrect number of hours apart. --- DateToolsSwift/DateTools/Date+TimeAgo.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DateToolsSwift/DateTools/Date+TimeAgo.swift b/DateToolsSwift/DateTools/Date+TimeAgo.swift index 527d02de..78435fd3 100644 --- a/DateToolsSwift/DateTools/Date+TimeAgo.swift +++ b/DateToolsSwift/DateTools/Date+TimeAgo.swift @@ -68,8 +68,8 @@ public extension Date { let components = calendar.dateComponents(unitFlags, from: earliest, to: latest) - let yesterday = date.subtract(1.days) - let isYesterday = yesterday.day == self.day + let yesterday = latest.subtract(1.days) + let isYesterday = yesterday.day == earliest.day //Not Yet Implemented/Optional //The following strings are present in the translation files but lack logic as of 2014.04.05 From 145c343cb6cff5c676fd27df3d9bb8a4cef9d736 Mon Sep 17 00:00:00 2001 From: Jason Ji Date: Sat, 21 Apr 2018 14:47:36 -0400 Subject: [PATCH 2/2] Patch for a situation where the two dates are less than 2 days apart, but the day component is 2 apart. (Example: April 19, 10PM and April 21, 2PM) --- DateToolsSwift/DateTools/Date+TimeAgo.swift | 23 +++++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/DateToolsSwift/DateTools/Date+TimeAgo.swift b/DateToolsSwift/DateTools/Date+TimeAgo.swift index 78435fd3..80972bf6 100644 --- a/DateToolsSwift/DateTools/Date+TimeAgo.swift +++ b/DateToolsSwift/DateTools/Date+TimeAgo.swift @@ -112,12 +112,17 @@ public extension Date { else if (components.day! >= 2) { return self.logicalLocalizedStringFromFormat(format: "%%d %@days ago", value: components.day!) } - else if (isYesterday) { - if (numericDates) { - return DateToolsLocalizedStrings("1 day ago"); + else if (components.day! >= 1) { + if (isYesterday) { + if (numericDates) { + return DateToolsLocalizedStrings("1 day ago"); + } + + return DateToolsLocalizedStrings("Yesterday"); + } + else { + return DateToolsLocalizedStrings("1 day ago") } - - return DateToolsLocalizedStrings("Yesterday"); } else if (components.hour! >= 2) { return self.logicalLocalizedStringFromFormat(format: "%%d %@hours ago", value: components.hour!) @@ -200,9 +205,9 @@ public extension Date { private func logicalLocalizedStringFromFormat(format: String, value: Int) -> String{ #if os(Linux) - let localeFormat = String.init(format: format, getLocaleFormatUnderscoresWithValue(Double(value)) as! CVarArg) // this may not work, unclear!! + let localeFormat = String.init(format: format, getLocaleFormatUnderscoresWithValue(Double(value)) as! CVarArg) // this may not work, unclear!! #else - let localeFormat = String.init(format: format, getLocaleFormatUnderscoresWithValue(Double(value))) + let localeFormat = String.init(format: format, getLocaleFormatUnderscoresWithValue(Double(value))) #endif return String.init(format: DateToolsLocalizedStrings(localeFormat), value) @@ -241,9 +246,9 @@ public extension Date { #if os(Linux) // NSLocalizedString() is not available yet, see: https://github.com/apple/swift-corelibs-foundation/blob/16f83ddcd311b768e30a93637af161676b0a5f2f/Foundation/NSData.swift // However, a seemingly-equivalent method from NSBundle is: https://github.com/apple/swift-corelibs-foundation/blob/master/Foundation/NSBundle.swift - return Bundle.main.localizedString(forKey: string, value: "", table: "DateTools") + return Bundle.main.localizedString(forKey: string, value: "", table: "DateTools") #else - return NSLocalizedString(string, tableName: "DateTools", bundle: Bundle.dateToolsBundle(), value: "", comment: "") + return NSLocalizedString(string, tableName: "DateTools", bundle: Bundle.dateToolsBundle(), value: "", comment: "") #endif }