Skip to content

Commit

Permalink
✨ Updating to new date APIs (#148)
Browse files Browse the repository at this point in the history
* 🚚 Update to new date apis

* 🚚 Update to new date apis
  • Loading branch information
MadsBogeskov authored Oct 30, 2024
1 parent e3001f2 commit 3da4e37
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 42 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
Expand Up @@ -62,14 +62,14 @@ class SwiftPackageBuilder {

let packageFile =
"""
// swift-tools-version:5.5
// swift-tools-version: 6.0
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "PROJECT_NAME",
platforms: [.iOS(.v14), .macOS(.v12)],
platforms: [.iOS(.v15), .macOS(.v12)],
products: [
PRODUCTS
],
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 3da4e37

Please sign in to comment.