Skip to content

Commit a429d82

Browse files
authored
Merge pull request #6 from lvalenta/master
FormatStyle & Mutability & Sendability & Inlinable
2 parents 97176a2 + 9c4f9be commit a429d82

4 files changed

+373
-14
lines changed

Sources/NaiveDate.swift

+30-11
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,55 @@ import Foundation
33
// MARK: - NaiveDate
44

55
/// 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
88

99
/// Initializes the naive date with a given date components.
1010
/// - important: The naive types don't validate input components. For any
1111
/// precise manipulations with time use native `Date` and `Calendar` types.
12+
@inlinable
1213
public init(year: Int, month: Int, day: Int) {
1314
self.year = year; self.month = month; self.day = day
1415
}
1516

1617
// MARK: Comparable
1718

19+
@inlinable
1820
public static func <(lhs: NaiveDate, rhs: NaiveDate) -> Bool {
1921
return (lhs.year, lhs.month, lhs.day) < (rhs.year, rhs.month, rhs.day)
2022
}
2123

2224
// MARK: LosslessStringConvertible
2325

2426
/// Creates a naive date from a given string (e.g. "2017-12-30").
27+
@inlinable
2528
public init?(_ string: String) {
2629
// Not using `ISO8601DateFormatter` because it only works with `Date`
2730
guard let cmps = _components(from: string, separator: "-"), cmps.count == 3 else { return nil }
2831
self = NaiveDate(year: cmps[0], month: cmps[1], day: cmps[2])
2932
}
3033

3134
/// Returns a string representation of a naive date (e.g. "2017-12-30").
35+
@inlinable
3236
public var description: String {
3337
return String(format: "%i-%.2i-%.2i", year, month, day)
3438
}
3539

3640
// MARK: Codable
3741

42+
@inlinable
3843
public init(from decoder: Decoder) throws {
3944
self = try _decode(from: decoder)
4045
}
4146

47+
@inlinable
4248
public func encode(to encoder: Encoder) throws {
4349
try _encode(self, to: encoder)
4450
}
4551

4652
// MARK: _DateComponentsConvertible
4753

54+
@inlinable
4855
public var dateComponents: DateComponents {
4956
return DateComponents(year: year, month: month, day: day)
5057
}
@@ -53,8 +60,8 @@ public struct NaiveDate: Equatable, Hashable, Comparable, LosslessStringConverti
5360
// MARK: - NaiveTime
5461

5562
/// 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
5865

5966
/// Initializes the naive time with a given date components.
6067
/// - important: The naive types don't validate input components. For any
@@ -116,9 +123,9 @@ public struct NaiveTime: Equatable, Hashable, Comparable, LosslessStringConverti
116123
// MARK: - NaiveDateTime
117124

118125
/// 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
122129

123130
/// Initializes the naive datetime with a given date components.
124131
/// - important: The naive types don't validate input components. For any
@@ -175,26 +182,31 @@ public struct NaiveDateTime: Equatable, Hashable, Comparable, LosslessStringConv
175182
}
176183

177184

185+
178186
// MARK: - Calendar Extensions
179187

180188
public extension Calendar {
181189
// MARK: Naive* -> Date
182190

183191
/// Returns a date in calendar's time zone created from the naive date.
192+
@inlinable
184193
func date(from date: NaiveDate, in timeZone: TimeZone? = nil) -> Date? {
185194
return _date(from: date, in: timeZone)
186195
}
187196

188197
/// Returns a date in calendar's time zone created from the naive time.
198+
@inlinable
189199
func date(from time: NaiveTime, in timeZone: TimeZone? = nil) -> Date? {
190200
return _date(from: time, in: timeZone)
191201
}
192202

193203
/// Returns a date in calendar's time zone created from the naive datetime.
204+
@inlinable
194205
func date(from dateTime: NaiveDateTime, in timeZone: TimeZone? = nil) -> Date? {
195206
return _date(from: dateTime, in: timeZone)
196207
}
197208

209+
@usableFromInline
198210
internal func _date<T: _DateComponentsConvertible>(from value: T, in timeZone: TimeZone? = nil) -> Date? {
199211
var components = value.dateComponents
200212
components.timeZone = timeZone
@@ -205,20 +217,23 @@ public extension Calendar {
205217

206218
/// Returns naive date from a date, as if in a given time zone. User calendar's time zone.
207219
/// - parameter timeZone: By default uses calendar's time zone.
220+
@inlinable
208221
func naiveDate(from date: Date, in timeZone: TimeZone? = nil) -> NaiveDate {
209222
let components = self.dateComponents(in: timeZone ?? self.timeZone, from: date)
210223
return NaiveDate(year: components.year!, month: components.month!, day: components.day!)
211224
}
212225

213226
/// Returns naive time from a date, as if in a given time zone. User calendar's time zone.
214227
/// - parameter timeZone: By default uses calendar's time zone.
228+
@inlinable
215229
func naiveTime(from date: Date, in timeZone: TimeZone? = nil) -> NaiveTime {
216230
let components = self.dateComponents(in: timeZone ?? self.timeZone, from: date)
217231
return NaiveTime(hour: components.hour!, minute: components.minute!, second: components.second!)
218232
}
219233

220234
/// Returns naive time from a date, as if in a given time zone. User calendar's time zone.
221235
/// - parameter timeZone: By default uses calendar's time zone.
236+
@inlinable
222237
func naiveDateTime(from date: Date, in timeZone: TimeZone? = nil) -> NaiveDateTime {
223238
let components = self.dateComponents(in: timeZone ?? self.timeZone, from: date)
224239
return NaiveDateTime(
@@ -231,11 +246,13 @@ public extension Calendar {
231246
// MARK: - Private
232247

233248
/// A type that can be converted to DateComponents (and in turn to Date).
234-
internal protocol _DateComponentsConvertible {
249+
@usableFromInline
250+
protocol _DateComponentsConvertible {
235251
var dateComponents: DateComponents { get }
236252
}
237253

238-
private func _decode<T: LosslessStringConvertible>(from decoder: Decoder) throws -> T {
254+
@usableFromInline
255+
func _decode<T: LosslessStringConvertible>(from decoder: Decoder) throws -> T {
239256
let container = try decoder.singleValueContainer()
240257
let string = try container.decode(String.self)
241258
guard let value = T(string) else {
@@ -244,12 +261,14 @@ private func _decode<T: LosslessStringConvertible>(from decoder: Decoder) throws
244261
return value
245262
}
246263

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 {
248266
var container = encoder.singleValueContainer()
249267
try container.encode(value.description)
250268
}
251269

252-
private func _components(from string: String, separator: String) -> [Int]? {
270+
@usableFromInline
271+
func _components(from string: String, separator: String) -> [Int]? {
253272
let substrings = string.components(separatedBy: separator)
254273
let components = substrings.compactMap(Int.init)
255274
return components.count == substrings.count ? components : nil

0 commit comments

Comments
 (0)