Skip to content

Commit

Permalink
🚚 Update to new date apis
Browse files Browse the repository at this point in the history
  • Loading branch information
MadsBogeskov committed Oct 29, 2024
1 parent e3001f2 commit 88800c6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 40 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.7
// swift-tools-version: 5.9
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription
Expand Down
Original file line number Diff line number Diff line change
@@ -1,54 +1,25 @@
let dateDecodingStrategy = """
import Foundation
// first try decoding date time format (yyyy-MM-ddTHH:mm:ss.fffZ)
private let dateFractionalTimeFormatter: ISO8601DateFormatter = {
let formatter = ISO8601DateFormatter()
formatter.formatOptions = [
.withFullDate,
.withDashSeparatorInDate,
.withTime,
.withColonSeparatorInTime,
.withFractionalSeconds
]
return formatter
}()
// then try decoding date time format (yyyy-MM-ddTHH:mm:ssZ)
private let dateTimeFormatter: ISO8601DateFormatter = {
let formatter = ISO8601DateFormatter()
formatter.formatOptions = [
.withFullDate,
.withDashSeparatorInDate,
.withTime,
.withColonSeparatorInTime
]
return formatter
}()
// then try decoding date only format (yyyy-MM-dd)
private let dateFormatter: ISO8601DateFormatter = {
let formatter = ISO8601DateFormatter()
formatter.formatOptions = [
.withFullDate,
.withDashSeparatorInDate
]
return formatter
}()
@Sendable <ACCESSCONTROL> func dateDecodingStrategy(_ decoder: Decoder) throws -> Date {
@Sendable internal func dateDecodingStrategy(_ decoder: Decoder) throws -> Date {
let container = try decoder.singleValueContainer()
let stringValue = try container.decode(String.self)
if let date = dateFractionalTimeFormatter.date(from: stringValue) {
// first try decoding date time format (yyyy-MM-ddTHH:mm:ss.fffZ)
if let date = try? Date(
stringValue,
strategy: .iso8601.year().month().day().dateSeparator(.dash).time(includingFractionalSeconds: true)
) {
return date
}
if let date = dateTimeFormatter.date(from: stringValue) {
// then try decoding date time format (yyyy-MM-ddTHH:mm:ssZ)
if let date = try? Date(stringValue, strategy: .iso8601) {
return date
}
if let date = dateFormatter.date(from: stringValue) {
// then try decoding date only format (yyyy-MM-dd)
if let date = try? Date(stringValue, strategy: .iso8601.year().month().day().dateSeparator(.dash)) {
return date
}
Expand Down

0 comments on commit 88800c6

Please sign in to comment.