Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overloaded -timePassed method with language agnostic implementation #486

Merged
merged 2 commits into from
Aug 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ All notable changes to this project will be documented in this file.

5. **UserDefaults**
- `public func date(forKey key: String) -> Date?` in [[PR]](https://github.com/goktugyil/EZSwiftExtensions/pull/459) by *Khalian*

6. **Date**
- `public func timePassed() -> TimePassed` in [[PR]](https://github.com/goktugyil/EZSwiftExtensions/pull/486) by *110100110101*

## [Release 1.10]

Expand Down
8 changes: 8 additions & 0 deletions EZSwiftExtensions.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
0B96F02E1E2FD32500236DDE /* UIEdgeInsets.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B96F02C1E2FD32000236DDE /* UIEdgeInsets.swift */; };
0B96F0301E2FD33C00236DDE /* UIEdgeInsetsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B96F02F1E2FD33C00236DDE /* UIEdgeInsetsTests.swift */; };
0B96F0311E2FD33C00236DDE /* UIEdgeInsetsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B96F02F1E2FD33C00236DDE /* UIEdgeInsetsTests.swift */; };
2D919FD0212FC9440024A3AE /* TimePassed.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2D919FCF212FC9440024A3AE /* TimePassed.swift */; };
2D919FD1212FC9440024A3AE /* TimePassed.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2D919FCF212FC9440024A3AE /* TimePassed.swift */; };
2D919FD2212FC9440024A3AE /* TimePassed.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2D919FCF212FC9440024A3AE /* TimePassed.swift */; };
31F868DB1C2B6E5E00542250 /* DoubleExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 31F868DA1C2B6E5E00542250 /* DoubleExtensions.swift */; };
50AD20191DDCE6FF00A5A863 /* FileManagerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50AD20181DDCE6FF00A5A863 /* FileManagerTests.swift */; };
50AD201A1DDCE6FF00A5A863 /* FileManagerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50AD20181DDCE6FF00A5A863 /* FileManagerTests.swift */; };
Expand Down Expand Up @@ -354,6 +357,7 @@
0B96C3CD1C8688CB00BB2A3B /* URLTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = URLTests.swift; sourceTree = "<group>"; };
0B96F02C1E2FD32000236DDE /* UIEdgeInsets.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIEdgeInsets.swift; sourceTree = "<group>"; };
0B96F02F1E2FD33C00236DDE /* UIEdgeInsetsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIEdgeInsetsTests.swift; sourceTree = "<group>"; };
2D919FCF212FC9440024A3AE /* TimePassed.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TimePassed.swift; sourceTree = "<group>"; };
31F868DA1C2B6E5E00542250 /* DoubleExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DoubleExtensions.swift; sourceTree = "<group>"; };
50AD20181DDCE6FF00A5A863 /* FileManagerTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FileManagerTests.swift; sourceTree = "<group>"; };
5A02399C1E6D345C00C235B8 /* CollectionsExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CollectionsExtension.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -662,6 +666,7 @@
E19AC0EE1DCBA2BF00908905 /* URLExtensions.swift */,
E19AC0EB1DCBA28A00908905 /* UserDefaultsExtension.swift */,
0B96F02C1E2FD32000236DDE /* UIEdgeInsets.swift */,
2D919FCF212FC9440024A3AE /* TimePassed.swift */,
);
path = Sources;
sourceTree = SOURCE_ROOT;
Expand Down Expand Up @@ -986,6 +991,7 @@
5AF577A51EEE52DC00B1C277 /* UIUserInterfaceSizeClassExtensions.swift in Sources */,
5AF577A61EEE52DC00B1C277 /* UIViewControllerExtensions.swift in Sources */,
5AF577A71EEE52DC00B1C277 /* UIViewExtensions.swift in Sources */,
2D919FD2212FC9440024A3AE /* TimePassed.swift in Sources */,
5AF577A81EEE52DC00B1C277 /* UIWindowExtensions.swift in Sources */,
5AF577A91EEE52DC00B1C277 /* URLExtensions.swift in Sources */,
5AF577AA1EEE52DC00B1C277 /* UserDefaultsExtension.swift in Sources */,
Expand Down Expand Up @@ -1103,6 +1109,7 @@
B5DC87341C0ED34300972D0A /* UIImageViewExtensions.swift in Sources */,
B5DC87351C0ED34300972D0A /* UILabelExtensions.swift in Sources */,
B5DC87361C0ED34300972D0A /* UITextViewExtensions.swift in Sources */,
2D919FD0212FC9440024A3AE /* TimePassed.swift in Sources */,
E19AC0E91DCBA26500908905 /* DateExtensions.swift in Sources */,
B5DC87371C0ED34300972D0A /* UIViewControllerExtensions.swift in Sources */,
B5DC87381C0ED34300972D0A /* UIViewExtensions.swift in Sources */,
Expand Down Expand Up @@ -1208,6 +1215,7 @@
CD4D30CE1CEEAFD900CB53BC /* UIButtonExtensions.swift in Sources */,
E19AC0F31DCBA2CE00908905 /* TimerExtensions.swift in Sources */,
CD4D30CF1CEEAFD900CB53BC /* UIColoredView.swift in Sources */,
2D919FD1212FC9440024A3AE /* TimePassed.swift in Sources */,
79AB4F331D03394F009183EC /* UIApplicationExtensions.swift in Sources */,
CD4D30D01CEEAFD900CB53BC /* UIColorExtensions.swift in Sources */,
CD4D30D11CEEAFD900CB53BC /* UIDeviceExtensions.swift in Sources */,
Expand Down
36 changes: 36 additions & 0 deletions EZSwiftExtensionsTests/DateTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import XCTest
@testable import EZSwiftExtensions

class DateTests: XCTestCase {
// note that Date uses UTC in Date(timeIntervalSince1970: _)
Expand Down Expand Up @@ -171,6 +172,41 @@ class DateTests: XCTestCase {
XCTAssertEqual(fifteenYearsAgo?.timePassed(), "15 years ago")
}

func testTimePassedEnumBetweenDates() {

let today = Date()
let timePassedToday: TimePassed = today.timePassed()
XCTAssertEqual(timePassedToday, TimePassed.now)

let fifteenYearsAgo = Calendar.current.date(byAdding: .year, value: -15, to: today)
let timePassedFifteenYearsAgo: TimePassed = fifteenYearsAgo!.timePassed()
XCTAssertEqual(timePassedFifteenYearsAgo, TimePassed.year(15))

let twoMonthsAgo = Calendar.current.date(byAdding: .month, value: -2, to: today)
let timePassedTwoMonthsAgo: TimePassed = twoMonthsAgo!.timePassed()
XCTAssertEqual(timePassedTwoMonthsAgo, TimePassed.month(2))

let yesterday = Calendar.current.date(byAdding: .day, value: -1, to: today)
let timePassedYesterday: TimePassed = yesterday!.timePassed()
XCTAssertEqual(timePassedYesterday, TimePassed.day(1))

let sixHoursAgo = Calendar.current.date(byAdding: .hour, value: -6, to: today)
let timePassedSixHoursAgo: TimePassed = sixHoursAgo!.timePassed()
XCTAssertEqual(timePassedSixHoursAgo, TimePassed.hour(6))

let thirteenMinutesAgo = Calendar.current.date(byAdding: .minute, value: -13, to: today)
let timePassedThirteenMinutesAgo: TimePassed = thirteenMinutesAgo!.timePassed()
XCTAssertEqual(timePassedThirteenMinutesAgo, TimePassed.minute(13))

let fiveSecondsAgo = Calendar.current.date(byAdding: .second, value: -5, to: today)
let timePassedFiveSecondsAgo: TimePassed = fiveSecondsAgo!.timePassed()
XCTAssertEqual(timePassedFiveSecondsAgo, TimePassed.second(5))

let oneSecondAgo = Calendar.current.date(byAdding: .second, value: -1, to: today)
let timePassedOneSecondAgo: TimePassed = oneSecondAgo!.timePassed()
XCTAssertEqual(timePassedOneSecondAgo, TimePassed.second(1))
}

func testIsPast() {
let beginningOfUnixTime = Date(timeIntervalSince1970: 0)
XCTAssertTrue(beginningOfUnixTime.isPast)
Expand Down
26 changes: 25 additions & 1 deletion Sources/DateExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Foundation

extension Date {

public static let minutesInAWeek = 24 * 60 * 7

/// EZSE: Initializes Date from string and format
Expand Down Expand Up @@ -136,6 +136,30 @@ extension Date {
}
}

/// EZSE: Easy creation of time passed String. Can be Years, Months, days, hours, minutes or seconds. Useful for localization
public func timePassed() -> TimePassed {

let date = Date()
let calendar = Calendar.autoupdatingCurrent
let components = (calendar as NSCalendar).components([.year, .month, .day, .hour, .minute, .second], from: self, to: date, options: [])

if components.year! >= 1 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if you have to represent something like 1 year and 6 months. IMO it should be both TimePassed.year(1) and TimePassed.month(6). Here it would just round up to 1 year.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, it would just round up to the largest time unit, because we retained the original behaviour of the first -timePassed extension method. The original method would return "1 year ago", instead of "1 year and 6 months ago".

On developers' (consumers') perspective, you're still right on what the -timePassed method should return.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yes fair point. I just looked up the impl.

The logic given that impl is fine, but that is something that might be construed as incorrect. I will think about how to deprecate that version and actually fix that impl with a better impl, but this is fine for now.

return TimePassed.year(components.year!)
} else if components.month! >= 1 {
return TimePassed.month(components.month!)
} else if components.day! >= 1 {
return TimePassed.day(components.day!)
} else if components.hour! >= 1 {
return TimePassed.hour(components.hour!)
} else if components.minute! >= 1 {
return TimePassed.minute(components.minute!)
} else if components.second! >= 1 {
return TimePassed.second(components.second!)
} else {
return TimePassed.now
}
}

/// EZSE: Check if date is in future.
public var isFuture: Bool {
return self > Date()
Expand Down
53 changes: 53 additions & 0 deletions Sources/TimePassed.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//
// TimePassed.swift
// EZSwiftExtensions
//
// Created by Jaja Yting on 24/08/2018.
// Copyright © 2018 Goktug Yilmaz. All rights reserved.
//

import Foundation

public enum TimePassed {

case year(Int)
case month(Int)
case day(Int)
case hour(Int)
case minute(Int)
case second(Int)
case now
}

extension TimePassed: Equatable {

public static func ==(lhs: TimePassed, rhs: TimePassed) -> Bool {

switch(lhs, rhs) {

case (.year(let a), .year(let b)):
return a == b

case (.month(let a), .month(let b)):
return a == b

case (.day(let a), .day(let b)):
return a == b

case (.hour(let a), .hour(let b)):
return a == b

case (.minute(let a), .minute(let b)):
return a == b

case (.second(let a), .second(let b)):
return a == b

case (.now, .now):
return true

default:
return false
}
}
}