Skip to content

Commit

Permalink
Corrections and updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
proggeramlug committed Jan 16, 2020
1 parent 161946c commit 174acb5
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 10 deletions.
7 changes: 7 additions & 0 deletions .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>SwiftyDates.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
</dict>
</plist>
19 changes: 12 additions & 7 deletions Sources/String+SwiftyDates.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ extension String {
var second: Double = 0
var offset: Double = 0

var cleanedTime = self.replacingOccurrences(of: "[a-zA-Z]", with: "", options: .regularExpression)
let sep: Character = self.contains(".") && !self.contains(":") ? "." : ":"

var cleanedTime = self.replacingOccurrences(of: "[a-zA-Z]", with: "", options: .regularExpression).replacingOccurrences(of: " ", with: "")
let sep: Character = (self.contains(".") && !self.contains(":")) ? "." : ":"
if (self.contains("-") || self.contains("+")) {
let sign: Double = self.contains("-") ? -1 : 1

Expand Down Expand Up @@ -50,10 +49,6 @@ extension String {
}

public func swiftyDate(calendar:Calendar = Calendar.current) -> Date? {
if let timestamp = TimeInterval(self) {
return Date(timeIntervalSince1970: timestamp)
}

// is this even a string we can work with?
if (self.count < 1) { return nil }

Expand Down Expand Up @@ -128,6 +123,7 @@ extension String {
}
} else {
let intValue = Int(self) ?? 0
print("int value is: ", intValue)
if (self.count == 4 || intValue > 12) {
year = intValue
} else {
Expand All @@ -142,6 +138,13 @@ extension String {
if (year! < 50) {
year = 2000 + year!
}

if year! > 5000 {
if let timestamp = TimeInterval(self) {
return Date(timeIntervalSince1970: timestamp)
}
}

let components = DateComponents(calendar: calendar, year: year, month: month, day: day)
return components.date
}
Expand All @@ -163,10 +166,12 @@ extension String {
if let pos = index(of: " ") {
date = String(self[..<pos]).swiftyDate()
time = String(self[pos...]).swiftyTime()
print("time: ", time, self)
} else {
date = swiftyDate()
}
} else if (cleanString.contains(":") || cleanString.contains("pm") || cleanString.contains("am")) { // if we have a ":" it is most likely a time

date = calendar.date(from: calendar.dateComponents([.year, .month, .day], from: baseDate))!
time = swiftyTime()
} else {
Expand Down
12 changes: 9 additions & 3 deletions SwiftyDatesTests/SwiftyDatesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class SwiftyDatesTests: XCTestCase {

override func setUp() {
super.setUp()

let currentYear = Calendar.current.component(.year, from: Date())

iso8601TestCases.append(TestCase("2018-03-08", "03/08/2018 00:00:00"))
iso8601TestCases.append(TestCase("2018-03-08T15:49:46+00:00", "03/08/2018 15:49:46"))
iso8601TestCases.append(TestCase("2018-03-08T15:49:46Z", "03/08/2018 15:49:46"))
Expand All @@ -49,19 +52,20 @@ class SwiftyDatesTests: XCTestCase {
iso8601TestCases.append(TestCase("2001-02-03T04:05:06-06:30","02/03/2001 10:35:06"))
iso8601TestCases.append(TestCase("2001-02-03T04:05:06.007+06:30","02/02/2001 21:35:06"))
iso8601TestCases.append(TestCase("2001-02-03T04:05:06.007-06:30","02/03/2001 10:35:06"))
iso8601TestCases.append(TestCase("2020-01-16T08:34:00Z", "01/16/2020 08:34:00"))

// DE (German) dates
dateTestCases.append(TestCase("10.12.2017", "12/10/2017 00:00:00"))
dateTestCases.append(TestCase("10.12.17", "12/10/2017 00:00:00"))
dateTestCases.append(TestCase("10.12", "12/10/2018 00:00:00"))
dateTestCases.append(TestCase("10.12", "12/10/\(currentYear) 00:00:00"))
dateTestCases.append(TestCase("12.2017", "12/01/2017 00:00:00")) // uncommon but we still support it
// EN (GB etc.)
dateTestCases.append(TestCase("10-12-2017", "12/10/2017 00:00:00"))
dateTestCases.append(TestCase("10-12-17", "12/10/2017 00:00:00"))
dateTestCases.append(TestCase("10-12", "12/10/2018 00:00:00"))
dateTestCases.append(TestCase("10-12", "12/10/\(currentYear) 00:00:00"))
dateTestCases.append(TestCase("12-2017", "12/01/2017 00:00:00"))
// US
dateTestCases.append(TestCase("12/10", "12/10/2018 00:00:00"))
dateTestCases.append(TestCase("12/10", "12/10/\(currentYear) 00:00:00"))
dateTestCases.append(TestCase("12/2017", "12/01/2017 00:00:00"))
dateTestCases.append(TestCase("12/10/2017", "12/10/2017 00:00:00"))
dateTestCases.append(TestCase("12/10/17", "12/10/2017 00:00:00"))
Expand Down Expand Up @@ -156,7 +160,9 @@ class SwiftyDatesTests: XCTestCase {


for tc in timeTestCases {
//print("Parsing: ", tc.input, tc.output)
let date:Date? = tc.input.swiftyDateTime(calendar: Calendar.current, baseDate: testDate)
//print("we got: ",dateFormatter.string(from: date!))
if (date == nil) {
XCTAssertNil(tc.output)
}
Expand Down

0 comments on commit 174acb5

Please sign in to comment.