Corrections and updated tests

This commit is contained in:
Ralph Kuepper 2020-01-16 10:20:17 +01:00
parent 161946cb6c
commit 174acb50a5
5 changed files with 42 additions and 10 deletions

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<Workspace
version = "1.0">
<FileRef
location = "self:">
</FileRef>
</Workspace>

View File

@ -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>

View File

@ -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
@ -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 }
@ -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 {
@ -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
}
@ -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 {

View File

@ -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"))
@ -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"))
@ -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)
}