Skip to content

Commit 40c92b5

Browse files
committed
Remove time zones from most of the APIs
1 parent 94ded1d commit 40c92b5

4 files changed

Lines changed: 62 additions & 178 deletions

File tree

README.md

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,6 @@ let date = NaiveDate(year: 2017, month: 10, day: 1)
6767
// Creates `Date` in a calendar's time zone
6868
// "2017-10-01T00:00:00+0300" if user is in MSK
6969
Calendar.current.date(from: date)
70-
71-
// Creates `Date` with +0000 offset
72-
// "2017-10-01T00:00:00Z"
73-
Calendar.current.date(from: date, in: TimeZone(secondsFromGMT: 0)!)
7470
```
7571

7672
```swift
@@ -82,10 +78,6 @@ let dateTime = NaiveDateTime(
8278
// Creates `Date` in a calendar's time zone
8379
// "2017-10-01T15:30:00+0300" if user is in MSK
8480
Calendar.current.date(from: dateTime)
85-
86-
// Creates `Date` with +0000 offset
87-
// "2017-10-01T15:30:00Z"
88-
Calendar.current.date(from: dateTime, in: TimeZone(secondsFromGMT: 0)!),
8981
```
9082

9183

Sources/NaiveDate.swift

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ public struct NaiveDate: Equatable, Hashable, Comparable, LosslessStringConverti
6161

6262
// MARK: _DateComponentsConvertible
6363

64-
public func dateComponents(timeZone: TimeZone? = nil) -> DateComponents {
65-
return DateComponents(timeZone: timeZone, year: year, month: month, day: day)
64+
public var dateComponents: DateComponents {
65+
return DateComponents(year: year, month: month, day: day)
6666
}
6767
}
6868

@@ -134,8 +134,8 @@ public struct NaiveTime: Equatable, Hashable, Comparable, LosslessStringConverti
134134

135135
// MARK: _DateComponentsConvertible
136136

137-
public func dateComponents(timeZone: TimeZone? = nil) -> DateComponents {
138-
return DateComponents(timeZone: timeZone, hour: hour, minute: minute, second: second)
137+
public var dateComponents: DateComponents {
138+
return DateComponents(hour: hour, minute: minute, second: second)
139139
}
140140
}
141141

@@ -196,8 +196,8 @@ public struct NaiveDateTime: Equatable, Hashable, Comparable, LosslessStringConv
196196

197197
// MARK: _DateComponentsConvertible
198198

199-
public func dateComponents(timeZone: TimeZone? = nil) -> DateComponents {
200-
return DateComponents(timeZone: timeZone, year: date.year, month: date.month, day: date.day, hour: time.hour, minute: time.minute, second: time.second)
199+
public var dateComponents: DateComponents {
200+
return DateComponents(year: date.year, month: date.month, day: date.day, hour: time.hour, minute: time.minute, second: time.second)
201201
}
202202
}
203203

@@ -207,48 +207,42 @@ public struct NaiveDateTime: Equatable, Hashable, Comparable, LosslessStringConv
207207
public extension Calendar {
208208
// MARK: Naive* -> Date
209209

210-
/// Returns a date created from the specified naive date in a given time zone.
211-
/// - parameter timeZone: `nil` by default (uses Calendar time zone).
212-
public func date(from date: NaiveDate, in timeZone: TimeZone? = nil) -> Date? {
213-
return _date(from: date, in: timeZone)
210+
/// Returns a date in calendar's time zone created from the naive date.
211+
public func date(from date: NaiveDate) -> Date? {
212+
return _date(from: date)
214213
}
215214

216-
/// Returns a date created from the specified naive time in a given time zone.
217-
/// - parameter timeZone: `nil` by default (uses Calendar time zone).
218-
public func date(from time: NaiveTime, in timeZone: TimeZone? = nil) -> Date? {
219-
return _date(from: time, in: timeZone)
215+
/// Returns a date in calendar's time zone created from the naive time.
216+
public func date(from time: NaiveTime) -> Date? {
217+
return _date(from: time)
220218
}
221219

222-
/// Returns a date created from the specified naive datetime in a given time zone.
223-
/// - parameter timeZone: `nil` by default (uses Calendar time zone).
224-
public func date(from dateTime: NaiveDateTime, in timeZone: TimeZone? = nil) -> Date? {
225-
return _date(from: dateTime, in: timeZone)
220+
/// Returns a date in calendar's time zone created from the naive datetime.
221+
public func date(from dateTime: NaiveDateTime) -> Date? {
222+
return _date(from: dateTime)
226223
}
227224

228-
internal func _date<T: _DateComponentsConvertible>(from value: T, in timeZone: TimeZone? = nil) -> Date? {
229-
return self.date(from: value.dateComponents(timeZone: timeZone))
225+
internal func _date<T: _DateComponentsConvertible>(from value: T) -> Date? {
226+
return self.date(from: value.dateComponents)
230227
}
231228

232229
// MARK: Date -> Naive*
233230

234-
/// Returns naive date from a date, as if in a given time zone.
235-
/// - parameter timeZone: `nil` by default (uses Calendar time zone).
236-
public func naiveDate(from date: Date, in timeZone: TimeZone? = nil) -> NaiveDate {
237-
let components = self.dateComponents(in: timeZone ?? self.timeZone, from: date)
231+
/// Returns naive date from a date, as if in a given time zone. User calendar's time zone.
232+
public func naiveDate(from date: Date) -> NaiveDate {
233+
let components = self.dateComponents(in: timeZone, from: date)
238234
return NaiveDate(year: components.year!, month: components.month!, day: components.day!)
239235
}
240236

241-
/// Returns naive time from a date, as if in a given time zone.
242-
/// - parameter timeZone: `nil` by default (uses Calendar time zone).
243-
public func naiveTime(from date: Date, in timeZone: TimeZone? = nil) -> NaiveTime {
244-
let components = self.dateComponents(in: timeZone ?? self.timeZone, from: date)
237+
/// Returns naive time from a date, as if in a given time zone. User calendar's time zone.
238+
public func naiveTime(from date: Date) -> NaiveTime {
239+
let components = self.dateComponents(in: timeZone, from: date)
245240
return NaiveTime(hour: components.hour!, minute: components.minute!, second: components.second!)
246241
}
247242

248-
/// Returns naive time from a date, as if in a given time zone.
249-
/// - parameter timeZone: `nil` by default (uses Calendar time zone).
250-
public func naiveDateTime(from date: Date, in timeZone: TimeZone? = nil) -> NaiveDateTime {
251-
let components = self.dateComponents(in: timeZone ?? self.timeZone, from: date)
243+
/// Returns naive time from a date, as if in a given time zone. User calendar's time zone.
244+
public func naiveDateTime(from date: Date) -> NaiveDateTime {
245+
let components = self.dateComponents(in: timeZone, from: date)
252246
return NaiveDateTime(
253247
date: NaiveDate(year: components.year!, month: components.month!, day: components.day!),
254248
time: NaiveTime(hour: components.hour!, minute: components.minute!, second: components.second!)
@@ -261,7 +255,7 @@ public extension Calendar {
261255

262256
/// A type that can be converted to DateComponents (and in turn to Date).
263257
internal protocol _DateComponentsConvertible {
264-
func dateComponents(timeZone: TimeZone?) -> DateComponents
258+
var dateComponents: DateComponents { get }
265259
}
266260

267261
private func _decode<T: LosslessStringConvertible>(from decoder: Decoder) throws -> T {

Sources/NaiveDateFormatter.swift

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ public final class NaiveDateFormatter {
1313

1414
public init(_ closure: (_ formatter: DateFormatter) -> Void) {
1515
closure(formatter)
16-
self.formatter.timeZone = TimeZone._utc // important! UTC to UTC
1716
}
1817

1918
public convenience init(format: String) {
@@ -30,15 +29,15 @@ public final class NaiveDateFormatter {
3029
}
3130

3231
public func string(from value: NaiveDate) -> String? {
33-
return formatter.calendar._utcDate(from: value).map { formatter.string(from: $0) }
32+
return formatter.calendar._date(from: value).map { formatter.string(from: $0) }
3433
}
3534

3635
public func string(from value: NaiveTime) -> String? {
37-
return formatter.calendar._utcDate(from: value).map { formatter.string(from: $0) }
36+
return formatter.calendar._date(from: value).map { formatter.string(from: $0) }
3837
}
3938

4039
public func string(from value: NaiveDateTime) -> String? {
41-
return formatter.calendar._utcDate(from: value).map { formatter.string(from: $0) }
40+
return formatter.calendar._date(from: value).map { formatter.string(from: $0) }
4241
}
4342
}
4443

@@ -51,7 +50,6 @@ public final class NaiveDateRangeFormatter {
5150

5251
public init(_ closure: (_ formatter: DateIntervalFormatter) -> Void) {
5352
closure(formatter)
54-
self.formatter.timeZone = TimeZone._utc // important! UTC to UTC
5553
}
5654

5755
public convenience init(format: String) {
@@ -68,31 +66,23 @@ public final class NaiveDateRangeFormatter {
6866
}
6967

7068
public func string(from start: NaiveDate, to end: NaiveDate) -> String? {
71-
return formatter.calendar._utcDateRange(from: start, to: end).map { formatter.string(from: $0, to: $1) }
69+
return formatter.calendar._dateRange(from: start, to: end).map { formatter.string(from: $0, to: $1) }
7270
}
7371

7472
public func string(from start: NaiveTime, to end: NaiveTime) -> String? {
75-
return formatter.calendar._utcDateRange(from: start, to: end).map { formatter.string(from: $0, to: $1) }
73+
return formatter.calendar._dateRange(from: start, to: end).map { formatter.string(from: $0, to: $1) }
7674
}
7775

7876
public func string(from start: NaiveDateTime, to end: NaiveDateTime) -> String? {
79-
return formatter.calendar._utcDateRange(from: start, to: end).map { formatter.string(from: $0, to: $1) }
77+
return formatter.calendar._dateRange(from: start, to: end).map { formatter.string(from: $0, to: $1) }
8078
}
8179
}
8280

8381

8482
// MARK: - Private -
8583

86-
private extension TimeZone {
87-
static let _utc = TimeZone(secondsFromGMT: 0)!
88-
}
89-
9084
private extension Calendar {
91-
func _utcDate<T: _DateComponentsConvertible>(from date: T) -> Date? {
92-
return self._date(from: date, in: TimeZone._utc)
93-
}
94-
95-
func _utcDateRange<T: _DateComponentsConvertible>(from start: T, to end: T) -> (Date, Date)? {
85+
func _dateRange<T: _DateComponentsConvertible>(from start: T, to end: T) -> (Date, Date)? {
9686
guard let start = _date(from: start), let end = _date(from: end) else { return nil }
9787
return (start, end)
9888
}

Tests/NaiveDateTests.swift

Lines changed: 28 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,10 @@ class NaiveDateTest: XCTestCase {
7070

7171
func testFromDate() {
7272
let date = ISO8601DateFormatter().date(from: "2017-12-01T12:00:00Z")!
73-
let calendar = Calendar.current
73+
var calendar = Calendar.current
74+
calendar.timeZone = TimeZone(identifier: "GMT")!
7475
XCTAssertEqual(
75-
calendar.naiveDate(from: date, in: TimeZone(identifier: "GMT")!),
76+
calendar.naiveDate(from: date),
7677
NaiveDate("2017-12-01")
7778
)
7879
}
@@ -89,50 +90,13 @@ class NaiveDateTest: XCTestCase {
8990
_date(from: "2017-10-01T00:00:00+0100")
9091
)
9192
}
92-
93-
test("converting to date with 0 offset") {
94-
let date = NaiveDate(year: 2017, month: 10, day: 1)
95-
XCTAssertEqual(
96-
Calendar.current.date(from: date, in: TimeZone(secondsFromGMT: 0)!),
97-
_date(from: "2017-10-01T00:00:00Z")
98-
)
99-
}
100-
101-
test("converting to date with +3h offset") {
102-
let date = NaiveDate(year: 2017, month: 10, day: 1)
103-
XCTAssertEqual(
104-
Calendar.current.date(from: date, in: TimeZone(secondsFromGMT: 3 * 3600)!),
105-
_date(from: "2017-10-01T00:00:00+0300")
106-
)
107-
}
10893
}
10994

11095
func testToDateComponents() {
111-
test("converting to date components in current time zone") {
112-
let date = NaiveDate(year: 2017, month: 10, day: 1)
113-
let components = DateComponents(
114-
calendar: nil,
115-
timeZone: nil,
116-
year: 2017, month: 10, day: 1
117-
)
118-
XCTAssertEqual(
119-
date.dateComponents(),
120-
components
121-
)
122-
}
123-
124-
test("converting to date components with 0 offset") {
125-
let date = NaiveDate(year: 2017, month: 10, day: 1)
126-
let components = DateComponents(
127-
calendar: nil,
128-
timeZone: TimeZone(secondsFromGMT: 0)!,
129-
year: 2017, month: 10, day: 1
130-
)
131-
XCTAssertEqual(
132-
date.dateComponents(timeZone: TimeZone(secondsFromGMT: 0)),
133-
components
134-
)
135-
}
96+
XCTAssertEqual(
97+
NaiveDate(year: 2017, month: 10, day: 1).dateComponents,
98+
DateComponents(year: 2017, month: 10, day: 1)
99+
)
136100
}
137101
}
138102

@@ -397,95 +361,39 @@ class NaiveDateTimeTest: XCTestCase {
397361

398362
func testFromDate() {
399363
let date = ISO8601DateFormatter().date(from: "2017-12-01T12:00:00Z")!
400-
test("converting in gmt") {
401-
let calendar = Calendar.current
402-
XCTAssertEqual(
403-
calendar.naiveDateTime(from: date, in: TimeZone(identifier: "GMT")!),
404-
NaiveDateTime("2017-12-01T12:00:00")!
405-
)
406-
}
407-
test("converting to calendar time zone") {
408-
var calendar = Calendar.current
409-
calendar.timeZone = TimeZone(secondsFromGMT: 3600)!
410-
XCTAssertEqual(
411-
calendar.naiveDateTime(from: date),
412-
NaiveDateTime("2017-12-01T13:00:00")!
413-
)
414-
}
364+
var calendar = Calendar.current
365+
calendar.timeZone = TimeZone(secondsFromGMT: 3600)!
366+
XCTAssertEqual(
367+
calendar.naiveDateTime(from: date),
368+
NaiveDateTime("2017-12-01T13:00:00")!
369+
)
415370
}
416371

417372
func testToDate() {
418-
test("converting to date in current time zone") {
419-
let dateTime = NaiveDateTime(
420-
date: NaiveDate(year: 2017, month: 10, day: 1),
421-
time: NaiveTime(hour: 15, minute: 30, second: 0)
422-
)
423-
var calendar = Calendar.current
424-
calendar.timeZone = TimeZone(secondsFromGMT: 3600)!
425-
426-
XCTAssertEqual(
427-
calendar.date(from: dateTime),
428-
_date(from: "2017-10-01T15:30:00+0100")
429-
)
430-
}
431-
432-
test("converting to date with 0 offset") {
433-
let dateTime = NaiveDateTime(
434-
date: NaiveDate(year: 2017, month: 10, day: 1),
435-
time: NaiveTime(hour: 15, minute: 30, second: 00)
436-
)
437-
XCTAssertEqual(
438-
Calendar.current.date(from: dateTime, in: TimeZone(secondsFromGMT: 0)!),
439-
_date(from: "2017-10-01T15:30:00Z")
440-
)
441-
}
373+
let dateTime = NaiveDateTime(
374+
date: NaiveDate(year: 2017, month: 10, day: 1),
375+
time: NaiveTime(hour: 15, minute: 30, second: 0)
376+
)
377+
var calendar = Calendar.current
378+
calendar.timeZone = TimeZone(secondsFromGMT: 3600)!
442379

443-
test("converting to date with +3h offset") {
444-
let dateTime = NaiveDateTime(
445-
date: NaiveDate(year: 2017, month: 10, day: 1),
446-
time: NaiveTime(hour: 15, minute: 30, second: 00)
447-
)
448-
XCTAssertEqual(
449-
Calendar.current.date(from: dateTime, in: TimeZone(secondsFromGMT: 3 * 3600)!),
450-
_date(from: "2017-10-01T15:30:00+0300")
451-
)
452-
}
380+
XCTAssertEqual(
381+
calendar.date(from: dateTime),
382+
_date(from: "2017-10-01T15:30:00+0100")
383+
)
453384
}
454385

455386
func testToDateComponents() {
456-
test("converting to date components in current time zone") {
457-
let dateTime = NaiveDateTime(
387+
XCTAssertEqual(
388+
NaiveDateTime(
458389
date: NaiveDate(year: 2017, month: 10, day: 1),
459390
time: NaiveTime(hour: 15, minute: 30, second: 0)
460-
)
461-
let components = DateComponents(
462-
calendar: nil,
463-
timeZone: nil,
391+
).dateComponents,
392+
DateComponents(
464393
year: 2017, month: 10, day: 1,
465394
hour: 15, minute: 30, second: 0
466395
)
467-
XCTAssertEqual(
468-
dateTime.dateComponents(),
469-
components
470-
)
471-
}
472-
473-
test("converting to date components with 0 offset") {
474-
let dateTime = NaiveDateTime(
475-
date: NaiveDate(year: 2017, month: 10, day: 1),
476-
time: NaiveTime(hour: 15, minute: 30, second: 00)
477-
)
478-
let components = DateComponents(
479-
calendar: nil,
480-
timeZone: TimeZone(secondsFromGMT: 0)!,
481-
year: 2017, month: 10, day: 1,
482-
hour: 15, minute: 30, second: 0
483-
)
484-
XCTAssertEqual(
485-
dateTime.dateComponents(timeZone: TimeZone(secondsFromGMT: 0)),
486-
components
487-
)
488-
}
396+
)
489397
}
490398
}
491399

0 commit comments

Comments
 (0)