Skip to content

Commit ad29210

Browse files
committed
Date conversion now supports optional timezone parameters
1 parent fe22ac7 commit ad29210

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

Sources/NaiveDate.swift

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -211,41 +211,46 @@ public extension Calendar {
211211
// MARK: Naive* -> Date
212212

213213
/// Returns a date in calendar's time zone created from the naive date.
214-
public func date(from date: NaiveDate) -> Date? {
215-
return _date(from: date)
214+
public func date(from date: NaiveDate, in timeZone: TimeZone? = nil) -> Date? {
215+
return _date(from: date, in: timeZone)
216216
}
217217

218218
/// Returns a date in calendar's time zone created from the naive time.
219-
public func date(from time: NaiveTime) -> Date? {
220-
return _date(from: time)
219+
public func date(from time: NaiveTime, in timeZone: TimeZone? = nil) -> Date? {
220+
return _date(from: time, in: timeZone)
221221
}
222222

223223
/// Returns a date in calendar's time zone created from the naive datetime.
224-
public func date(from dateTime: NaiveDateTime) -> Date? {
225-
return _date(from: dateTime)
224+
public func date(from dateTime: NaiveDateTime, in timeZone: TimeZone? = nil) -> Date? {
225+
return _date(from: dateTime, in: timeZone)
226226
}
227227

228-
internal func _date<T: _DateComponentsConvertible>(from value: T) -> Date? {
229-
return self.date(from: value.dateComponents)
228+
internal func _date<T: _DateComponentsConvertible>(from value: T, in timeZone: TimeZone? = nil) -> Date? {
229+
var components = value.dateComponents
230+
components.timeZone = timeZone
231+
return self.date(from: components)
230232
}
231233

232234
// MARK: Date -> Naive*
233235

234236
/// Returns naive date from a date, as if in a given time zone. User calendar's time zone.
235-
public func naiveDate(from date: Date) -> NaiveDate {
236-
let components = self.dateComponents(in: timeZone, from: date)
237+
/// - parameter timeZone: By default uses calendar's time zone.
238+
public func naiveDate(from date: Date, in timeZone: TimeZone? = nil) -> NaiveDate {
239+
let components = self.dateComponents(in: timeZone ?? self.timeZone, from: date)
237240
return NaiveDate(year: components.year!, month: components.month!, day: components.day!)
238241
}
239242

240243
/// Returns naive time from a date, as if in a given time zone. User calendar's time zone.
241-
public func naiveTime(from date: Date) -> NaiveTime {
242-
let components = self.dateComponents(in: timeZone, from: date)
244+
/// - parameter timeZone: By default uses calendar's time zone.
245+
public func naiveTime(from date: Date, in timeZone: TimeZone? = nil) -> NaiveTime {
246+
let components = self.dateComponents(in: timeZone ?? self.timeZone, from: date)
243247
return NaiveTime(hour: components.hour!, minute: components.minute!, second: components.second!)
244248
}
245249

246250
/// Returns naive time from a date, as if in a given time zone. User calendar's time zone.
247-
public func naiveDateTime(from date: Date) -> NaiveDateTime {
248-
let components = self.dateComponents(in: timeZone, from: date)
251+
/// - parameter timeZone: By default uses calendar's time zone.
252+
public func naiveDateTime(from date: Date, in timeZone: TimeZone? = nil) -> NaiveDateTime {
253+
let components = self.dateComponents(in: timeZone ?? self.timeZone, from: date)
249254
return NaiveDateTime(
250255
date: NaiveDate(year: components.year!, month: components.month!, day: components.day!),
251256
time: NaiveTime(hour: components.hour!, minute: components.minute!, second: components.second!)

0 commit comments

Comments
 (0)