Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Commit

Permalink
update to swift3
Browse files Browse the repository at this point in the history
  • Loading branch information
kgn committed Jul 17, 2016
1 parent 4c832ee commit d0df01d
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 37 deletions.
58 changes: 29 additions & 29 deletions NSDate+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,30 @@

import Foundation

func NSDateTimeAgoLocalizedStrings(key: String) -> String {
func NSDateTimeAgoLocalizedStrings(_ key: String) -> String {
let resourcePath: String?

if let frameworkBundle = NSBundle(identifier: "com.kevinlawler.NSDateTimeAgo") {
if let frameworkBundle = Bundle(identifier: "com.kevinlawler.NSDateTimeAgo") {
// Load from Framework
resourcePath = frameworkBundle.resourcePath
} else {
// Load from Main Bundle
resourcePath = NSBundle.mainBundle().resourcePath
resourcePath = Bundle.main().resourcePath
}

if resourcePath == nil {
return ""
}

let path = NSURL(fileURLWithPath: resourcePath!).URLByAppendingPathComponent("NSDateTimeAgo.bundle")
guard let bundle = NSBundle(URL: path) else {
let path = try! URL(fileURLWithPath: resourcePath!).appendingPathComponent("NSDateTimeAgo.bundle")
guard let bundle = Bundle(url: path) else {
return ""
}

return NSLocalizedString(key, tableName: "NSDateTimeAgo", bundle: bundle, comment: "")
}

extension NSDate {
extension Date {

// shows 1 or two letter abbreviation for units.
// does not include 'ago' text ... just {value}{unit-abbreviation}
Expand All @@ -41,33 +41,33 @@ extension NSDate {
let components = self.dateComponents()

if components.year > 0 {
return stringFromFormat("%%d%@yr", withValue: components.year)
return self.string(fromFormat: "%%d%@yr", withValue: components.year!)
}

if components.month > 0 {
return stringFromFormat("%%d%@mo", withValue: components.month)
return self.string(fromFormat: "%%d%@mo", withValue: components.month!)
}

// TODO: localize for other calanders
if components.day >= 7 {
let value = components.day/7
return stringFromFormat("%%d%@w", withValue: value)
let value = components.day!/7
return self.string(fromFormat: "%%d%@w", withValue: value)
}

if components.day > 0 {
return stringFromFormat("%%d%@d", withValue: components.day)
return self.string(fromFormat: "%%d%@d", withValue: components.day!)
}

if components.hour > 0 {
return stringFromFormat("%%d%@h", withValue: components.hour)
return self.string(fromFormat: "%%d%@h", withValue: components.hour!)
}

if components.minute > 0 {
return stringFromFormat("%%d%@m", withValue: components.minute)
return self.string(fromFormat: "%%d%@m", withValue: components.minute!)
}

if components.second > 0 {
return stringFromFormat("%%d%@s", withValue: components.second )
return self.string(fromFormat: "%%d%@s", withValue: components.second! )
}

return ""
Expand All @@ -80,75 +80,75 @@ extension NSDate {
if components.year < 2 {
return NSDateTimeAgoLocalizedStrings("Last year")
} else {
return stringFromFormat("%%d %@years ago", withValue: components.year)
return self.string(fromFormat: "%%d %@years ago", withValue: components.year!)
}
}

if components.month > 0 {
if components.month < 2 {
return NSDateTimeAgoLocalizedStrings("Last month")
} else {
return stringFromFormat("%%d %@months ago", withValue: components.month)
return self.string(fromFormat: "%%d %@months ago", withValue: components.month!)
}
}

// TODO: localize for other calanders
if components.day >= 7 {
let week = components.day/7
let week = components.day!/7
if week < 2 {
return NSDateTimeAgoLocalizedStrings("Last week")
} else {
return stringFromFormat("%%d %@weeks ago", withValue: week)
return self.string(fromFormat: "%%d %@weeks ago", withValue: week)
}
}

if components.day > 0 {
if components.day < 2 {
return NSDateTimeAgoLocalizedStrings("Yesterday")
} else {
return stringFromFormat("%%d %@days ago", withValue: components.day)
return self.string(fromFormat: "%%d %@days ago", withValue: components.day!)
}
}

if components.hour > 0 {
if components.hour < 2 {
return NSDateTimeAgoLocalizedStrings("An hour ago")
} else {
return stringFromFormat("%%d %@hours ago", withValue: components.hour)
return self.string(fromFormat: "%%d %@hours ago", withValue: components.hour!)
}
}

if components.minute > 0 {
if components.minute < 2 {
return NSDateTimeAgoLocalizedStrings("A minute ago")
} else {
return stringFromFormat("%%d %@minutes ago", withValue: components.minute)
return self.string(fromFormat: "%%d %@minutes ago", withValue: components.minute!)
}
}

if components.second > 0 {
if components.second < 5 {
return NSDateTimeAgoLocalizedStrings("Just now")
} else {
return stringFromFormat("%%d %@seconds ago", withValue: components.second)
return self.string(fromFormat: "%%d %@seconds ago", withValue: components.second!)
}
}

return ""
}

private func dateComponents() -> NSDateComponents {
let calander = NSCalendar.currentCalendar()
return calander.components([.Second, .Minute, .Hour, .Day, .Month, .Year], fromDate: self, toDate: NSDate(), options: [])
private func dateComponents() -> DateComponents {
let calander = Calendar.current()
return calander.components([.second, .minute, .hour, .day, .month, .year], from: self, to: Date(), options: [])
}

private func stringFromFormat(format: String, withValue value: Int) -> String {
let localeFormat = String(format: format, getLocaleFormatUnderscoresWithValue(Double(value)))
private func string(fromFormat format: String, withValue value: Int) -> String {
let localeFormat = String(format: format, getLocaleFormatUnderscores(withValue: Double(value)))
return String(format: NSDateTimeAgoLocalizedStrings(localeFormat), value)
}

private func getLocaleFormatUnderscoresWithValue(value: Double) -> String {
guard let localeCode = NSLocale.preferredLanguages().first else {
private func getLocaleFormatUnderscores(withValue value: Double) -> String {
guard let localeCode = Locale.preferredLanguages().first else {
return ""
}

Expand Down
11 changes: 10 additions & 1 deletion NSDateTimeAgo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -187,17 +187,19 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0700;
LastUpgradeCheck = 0700;
LastUpgradeCheck = 0800;
ORGANIZATIONNAME = "Kevin Lawler";
TargetAttributes = {
535788311CD03AAB00F72193 = {
CreatedOnToolsVersion = 7.3;
};
833CD3771BCD9BD90085A751 = {
CreatedOnToolsVersion = 7.0.1;
LastSwiftMigration = 0800;
};
83AC39081BD066C500A9C7F7 = {
CreatedOnToolsVersion = 7.0.1;
LastSwiftMigration = 0800;
};
};
};
Expand Down Expand Up @@ -323,6 +325,7 @@
PRODUCT_NAME = NSDateTimeAgo;
SDKROOT = macosx;
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
};
name = Release;
};
Expand Down Expand Up @@ -431,6 +434,7 @@
PRODUCT_NAME = NSDateTimeAgo;
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -449,6 +453,8 @@
PRODUCT_BUNDLE_IDENTIFIER = com.kevinlawler.NSDateTimeAgo;
PRODUCT_NAME = NSDateTimeAgo;
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand All @@ -459,6 +465,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.kevinlawler.NSDateTimeAgoTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -469,6 +476,8 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.kevinlawler.NSDateTimeAgoTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0730"
LastUpgradeVersion = "0800"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0710"
LastUpgradeVersion = "0800"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
10 changes: 5 additions & 5 deletions Tests/NSDateTimeAgoTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ class NSDateTimeAgoTests: XCTestCase {
super.tearDown()
}

func dateForComponents(block: (components: NSDateComponents) -> Void) -> NSDate? {
let calander = NSCalendar.currentCalendar()
let components = NSDateComponents()
block(components: components)
return calander.dateByAddingComponents(components, toDate: NSDate(), options: [])
func dateForComponents(block: (inout components: DateComponents) -> Void) -> Date? {
let calander = Calendar.current()
var components = DateComponents()
block(components: &components)
return calander.date(byAdding: components, to: Date(), options: [])
}

// MARK: - timeAgoSimple
Expand Down

0 comments on commit d0df01d

Please sign in to comment.