Skip to content

Commit 6bca758

Browse files
committed
Simplify parsing
1 parent 2e5a8a1 commit 6bca758

4 files changed

Lines changed: 30 additions & 26 deletions

File tree

NaiveDate.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Pod::Spec.new do |s|
22
s.name = 'NaiveDate'
33
s.version = '0.1'
4-
s.summary = 'The missing naive date, time and datetime'
4+
s.summary = 'Naive date and time types'
55
s.homepage = 'https://github.com/kean/NaiveDate'
66
s.license = 'MIT'
77
s.author = 'Alexander Grebenyuk'

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
<a href="https://travis-ci.org/kean/NaiveDate"><img src="https://img.shields.io/travis/kean/NaiveDate/master.svg"></a>
88
</p>
99

10-
The missing **naive** date, time, and datetime types.
10+
**Naive** date, time, and datetime types.
1111

1212

1313
## Usage
1414

15-
Native `Date` type is great for working with dates along the time zones (e.g. "2017-09-29T15:00:00+0300"), however there are some scenarios in which naive dates and times are desirable.
15+
Native `Date` type is great for working with time zones (e.g. "2017-09-29T15:00:00+0300"), however there are scenarios in which naive dates and times are desirable.
1616

17-
The library implements three types for representing naive date:
17+
The library implements three types:
1818
- `NaiveDate` (e.g. "2017-09-29")
1919
- `NaiveTime` (e.g. "15:30:00")
2020
- `NaiveDateTime` (e.g. "2017-09-29T15:30:00" - no time zone and no offset).

Sources/NaiveDate.swift

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ public struct NaiveDate: Equatable, Hashable, Comparable, LosslessStringConverti
3838

3939
/// Creates a naive date from a given string (e.g. "2017-12-30").
4040
public init?(_ string: String) {
41-
let components = string.components(separatedBy: "-")
42-
guard components.count == 3 else { return nil } // must have 3 components
43-
guard let year = Int(components[0]), let month = Int(components[1]), let day = Int(components[2]) else { return nil }
44-
self = NaiveDate(year: year, month: month, day: day)
41+
// Not using `ISO8601DateFormatter` because it only works with `Date`
42+
guard let cmps = _components(from: string, separator: "-"),
43+
cmps.count == 3 else { return nil }
44+
self = NaiveDate(year: cmps[0], month: cmps[1], day: cmps[2])
4545
}
4646

4747
/// Returns a string representation of a naive date (e.g. "2017-12-30").
@@ -112,21 +112,9 @@ public struct NaiveTime: Equatable, Hashable, Comparable, LosslessStringConverti
112112

113113
/// Creates a naive time from a given string (e.g. "23:59", or "23:59:59").
114114
public init?(_ string: String) {
115-
let components = string.components(separatedBy: ":")
116-
117-
// has to have at least two components (hour and minute)
118-
guard components.count > 1 else { return nil }
119-
120-
guard let hour = Int(components[0]), let minute = Int(components[1]) else { return nil } // invalid input
121-
self.hour = hour
122-
self.minute = minute
123-
124-
if components.count > 2 { // also has seconds
125-
guard let second = Int(components[2]) else { return nil } // invalid input
126-
self.second = second
127-
} else {
128-
self.second = 0
129-
}
115+
guard let cmps = _components(from: string, separator: ":"),
116+
(2...3).contains(cmps.count) else { return nil }
117+
self.init(hour: cmps[0], minute: cmps[1], second: (cmps.count > 2 ? cmps[2] : 0))
130118
}
131119

132120
/// Returns a string representation of a naive time (e.g. "23:59:59").
@@ -289,3 +277,9 @@ private func _encode<T: LosslessStringConvertible>(_ value: T, to encoder: Encod
289277
var container = encoder.singleValueContainer()
290278
try container.encode(value.description)
291279
}
280+
281+
private func _components(from string: String, separator: String) -> [Int]? {
282+
let substrings = string.components(separatedBy: separator)
283+
let components = substrings.flatMap(Int.init)
284+
return components.count == substrings.count ? components : nil
285+
}

Tests/NaiveDateTests.swift

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ class NaiveDateTest: XCTestCase {
3737
func testFromString() {
3838
XCTAssertNil(NaiveDate("2017"))
3939
XCTAssertNil(NaiveDate("2017-10"))
40+
XCTAssertNil(NaiveDate("2017-AA-10-01"))
41+
XCTAssertNil(NaiveDate(" 2017-10-01"))
42+
XCTAssertNil(NaiveDate("2017- 10-01"))
43+
XCTAssertNil(NaiveDate("2017:10:01"))
44+
4045
XCTAssertEqual(NaiveDate("2017-10-01"), NaiveDate(year: 2017, month: 10, day: 1))
4146
XCTAssertEqual(NaiveDate("2017-10-1"), NaiveDate(year: 2017, month: 10, day: 1))
4247
}
@@ -175,11 +180,16 @@ class NaiveTimeTest: XCTestCase {
175180
// MARK: LosslessStringConvertible
176181

177182
func testFromString() {
178-
XCTAssertNil(NaiveTime("2klfdjafk"))
183+
XCTAssertNil(NaiveTime("AA"))
179184
XCTAssertNil(NaiveTime(""))
180-
XCTAssertNil(NaiveTime("23:fd"))
181-
XCTAssertNil(NaiveTime("23-26"))
185+
XCTAssertNil(NaiveTime("23:AA"))
186+
XCTAssertNil(NaiveTime("23-59"))
182187
XCTAssertNil(NaiveTime("23"))
188+
XCTAssertNil(NaiveTime("23:AA:59:59"))
189+
190+
XCTAssertNil(NaiveTime(" 23:59:59"))
191+
XCTAssertNil(NaiveTime("23:59:59 "))
192+
XCTAssertNil(NaiveTime("23:59 :59"))
183193

184194
XCTAssertEqual(NaiveTime("23:59:59"), NaiveTime(hour: 23, minute: 59, second: 59))
185195
XCTAssertEqual(NaiveTime("23:59"), NaiveTime(hour: 23, minute: 59, second: 0))

0 commit comments

Comments
 (0)