@@ -3,48 +3,55 @@ import Foundation
3
3
// MARK: - NaiveDate
4
4
5
5
/// Calendar date without a timezone.
6
- public struct NaiveDate : Equatable , Hashable , Comparable , LosslessStringConvertible , Codable , _DateComponentsConvertible {
7
- public let year : Int , month : Int , day : Int
6
+ public struct NaiveDate : Sendable , Equatable , Hashable , Comparable , LosslessStringConvertible , Codable , _DateComponentsConvertible {
7
+ public var year : Int , month : Int , day : Int
8
8
9
9
/// Initializes the naive date with a given date components.
10
10
/// - important: The naive types don't validate input components. For any
11
11
/// precise manipulations with time use native `Date` and `Calendar` types.
12
+ @inlinable
12
13
public init ( year: Int , month: Int , day: Int ) {
13
14
self . year = year; self . month = month; self . day = day
14
15
}
15
16
16
17
// MARK: Comparable
17
18
19
+ @inlinable
18
20
public static func < ( lhs: NaiveDate , rhs: NaiveDate ) -> Bool {
19
21
return ( lhs. year, lhs. month, lhs. day) < ( rhs. year, rhs. month, rhs. day)
20
22
}
21
23
22
24
// MARK: LosslessStringConvertible
23
25
24
26
/// Creates a naive date from a given string (e.g. "2017-12-30").
27
+ @inlinable
25
28
public init ? ( _ string: String ) {
26
29
// Not using `ISO8601DateFormatter` because it only works with `Date`
27
30
guard let cmps = _components ( from: string, separator: " - " ) , cmps. count == 3 else { return nil }
28
31
self = NaiveDate ( year: cmps [ 0 ] , month: cmps [ 1 ] , day: cmps [ 2 ] )
29
32
}
30
33
31
34
/// Returns a string representation of a naive date (e.g. "2017-12-30").
35
+ @inlinable
32
36
public var description : String {
33
37
return String ( format: " %i-%.2i-%.2i " , year, month, day)
34
38
}
35
39
36
40
// MARK: Codable
37
41
42
+ @inlinable
38
43
public init ( from decoder: Decoder ) throws {
39
44
self = try _decode ( from: decoder)
40
45
}
41
46
47
+ @inlinable
42
48
public func encode( to encoder: Encoder ) throws {
43
49
try _encode ( self , to: encoder)
44
50
}
45
51
46
52
// MARK: _DateComponentsConvertible
47
53
54
+ @inlinable
48
55
public var dateComponents : DateComponents {
49
56
return DateComponents ( year: year, month: month, day: day)
50
57
}
@@ -53,8 +60,8 @@ public struct NaiveDate: Equatable, Hashable, Comparable, LosslessStringConverti
53
60
// MARK: - NaiveTime
54
61
55
62
/// Time without a timezone. Allows for second precision.
56
- public struct NaiveTime : Equatable , Hashable , Comparable , LosslessStringConvertible , Codable , _DateComponentsConvertible {
57
- public let hour : Int , minute : Int , second : Int
63
+ public struct NaiveTime : Sendable , Equatable , Hashable , Comparable , LosslessStringConvertible , Codable , _DateComponentsConvertible {
64
+ public var hour : Int , minute : Int , second : Int
58
65
59
66
/// Initializes the naive time with a given date components.
60
67
/// - important: The naive types don't validate input components. For any
@@ -116,9 +123,9 @@ public struct NaiveTime: Equatable, Hashable, Comparable, LosslessStringConverti
116
123
// MARK: - NaiveDateTime
117
124
118
125
/// Combined date and time without timezone.
119
- public struct NaiveDateTime : Equatable , Hashable , Comparable , LosslessStringConvertible , Codable , _DateComponentsConvertible {
120
- public let date : NaiveDate
121
- public let time : NaiveTime
126
+ public struct NaiveDateTime : Sendable , Equatable , Hashable , Comparable , LosslessStringConvertible , Codable , _DateComponentsConvertible {
127
+ public var date : NaiveDate
128
+ public var time : NaiveTime
122
129
123
130
/// Initializes the naive datetime with a given date components.
124
131
/// - important: The naive types don't validate input components. For any
@@ -175,26 +182,31 @@ public struct NaiveDateTime: Equatable, Hashable, Comparable, LosslessStringConv
175
182
}
176
183
177
184
185
+
178
186
// MARK: - Calendar Extensions
179
187
180
188
public extension Calendar {
181
189
// MARK: Naive* -> Date
182
190
183
191
/// Returns a date in calendar's time zone created from the naive date.
192
+ @inlinable
184
193
func date( from date: NaiveDate , in timeZone: TimeZone ? = nil ) -> Date ? {
185
194
return _date ( from: date, in: timeZone)
186
195
}
187
196
188
197
/// Returns a date in calendar's time zone created from the naive time.
198
+ @inlinable
189
199
func date( from time: NaiveTime , in timeZone: TimeZone ? = nil ) -> Date ? {
190
200
return _date ( from: time, in: timeZone)
191
201
}
192
202
193
203
/// Returns a date in calendar's time zone created from the naive datetime.
204
+ @inlinable
194
205
func date( from dateTime: NaiveDateTime , in timeZone: TimeZone ? = nil ) -> Date ? {
195
206
return _date ( from: dateTime, in: timeZone)
196
207
}
197
208
209
+ @usableFromInline
198
210
internal func _date< T: _DateComponentsConvertible > ( from value: T , in timeZone: TimeZone ? = nil ) -> Date ? {
199
211
var components = value. dateComponents
200
212
components. timeZone = timeZone
@@ -205,20 +217,23 @@ public extension Calendar {
205
217
206
218
/// Returns naive date from a date, as if in a given time zone. User calendar's time zone.
207
219
/// - parameter timeZone: By default uses calendar's time zone.
220
+ @inlinable
208
221
func naiveDate( from date: Date , in timeZone: TimeZone ? = nil ) -> NaiveDate {
209
222
let components = self . dateComponents ( in: timeZone ?? self . timeZone, from: date)
210
223
return NaiveDate ( year: components. year!, month: components. month!, day: components. day!)
211
224
}
212
225
213
226
/// Returns naive time from a date, as if in a given time zone. User calendar's time zone.
214
227
/// - parameter timeZone: By default uses calendar's time zone.
228
+ @inlinable
215
229
func naiveTime( from date: Date , in timeZone: TimeZone ? = nil ) -> NaiveTime {
216
230
let components = self . dateComponents ( in: timeZone ?? self . timeZone, from: date)
217
231
return NaiveTime ( hour: components. hour!, minute: components. minute!, second: components. second!)
218
232
}
219
233
220
234
/// Returns naive time from a date, as if in a given time zone. User calendar's time zone.
221
235
/// - parameter timeZone: By default uses calendar's time zone.
236
+ @inlinable
222
237
func naiveDateTime( from date: Date , in timeZone: TimeZone ? = nil ) -> NaiveDateTime {
223
238
let components = self . dateComponents ( in: timeZone ?? self . timeZone, from: date)
224
239
return NaiveDateTime (
@@ -231,11 +246,13 @@ public extension Calendar {
231
246
// MARK: - Private
232
247
233
248
/// A type that can be converted to DateComponents (and in turn to Date).
234
- internal protocol _DateComponentsConvertible {
249
+ @usableFromInline
250
+ protocol _DateComponentsConvertible {
235
251
var dateComponents : DateComponents { get }
236
252
}
237
253
238
- private func _decode< T: LosslessStringConvertible > ( from decoder: Decoder ) throws -> T {
254
+ @usableFromInline
255
+ func _decode< T: LosslessStringConvertible > ( from decoder: Decoder ) throws -> T {
239
256
let container = try decoder. singleValueContainer ( )
240
257
let string = try container. decode ( String . self)
241
258
guard let value = T ( string) else {
@@ -244,12 +261,14 @@ private func _decode<T: LosslessStringConvertible>(from decoder: Decoder) throws
244
261
return value
245
262
}
246
263
247
- private func _encode< T: LosslessStringConvertible > ( _ value: T , to encoder: Encoder ) throws {
264
+ @usableFromInline
265
+ func _encode< T: LosslessStringConvertible > ( _ value: T , to encoder: Encoder ) throws {
248
266
var container = encoder. singleValueContainer ( )
249
267
try container. encode ( value. description)
250
268
}
251
269
252
- private func _components( from string: String , separator: String ) -> [ Int ] ? {
270
+ @usableFromInline
271
+ func _components( from string: String , separator: String ) -> [ Int ] ? {
253
272
let substrings = string. components ( separatedBy: separator)
254
273
let components = substrings. compactMap ( Int . init)
255
274
return components. count == substrings. count ? components : nil
0 commit comments