Swiftpack.co - MrAsterisco/SmartISODateFormatter as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by MrAsterisco.
MrAsterisco/SmartISODateFormatter v2.0.0
A date formatter based on ISO8601DateFormatter that really understands.
⭐️ 1
🕓 1 year ago
iOS macOS watchOS tvOS macCatalyst
.package(url: "https://github.com/MrAsterisco/SmartISODateFormatter.git", from: "v2.0.0")

SmartISODateFormatter

A date formatter based on ISO8601DateFormatter that can parse all ISO dates. This includes:

  • Just dates (eg. 2022-11-11),
  • Full dates (eg 2022-11-11T11:30:00Z),
  • Full dates with milliseconds (eg. 2022-11-11T11:30:00.344Z).

The ISO 8601 standard defines a representation of dates and times as an international standard to exchange date and time data in an unambigous way. This standard is widely used to exchange dates with and without times between APIs and client applications, such as iOS apps.

Apple provides an implementation, as part of Foundation, to parse and write ISO 8601 dates and times called ISO8601DateFormatter. This implementation is not very flexible: it requires a set of options to be passed that defines how the date is written and doesn't support parsing multiple formats at the same time.

This subclass fixes this problem by making changes to the formatOptions property on the fly before parsing the input string.

Installation

Starting with version 2, SmartISODateFormatter is available through Swift Package Manager.

.package(url: "https://github.com/MrAsterisco/SmartISODateFormatter", from: "<see GitHub releases>")

Latest Release

To find out the latest version, look at the Releases tab of this repository.

Usage

SmartISODateFormatter is a subclass of ISO8601DateFormatter, so you can use it the same way as the original class.

let string = "2022-11-11T11:30:00.344Z"
let date = SmartISODateFormatter().date(from: string)

JSONDecoder

SmartISODateFormatter also comes bundled with a custom dateDecodingStrategy for JSONDecoder. You can set it as follows:

let jsonDecoder = JSONDecoder()
jsonDecoder.dateDecodingStrategy = .smartISO8601

This will empower the JSONDecoder to parse all the formats that are supported by SmartISODateFormatter.

Contributing

You are very welcome to fork this repository, make changes and create a pull request.

License

SmartISODateFormatter is released under the MIT license. See LICENSE for more information.

GitHub

link
Stars: 1
Last commit: 1 year ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

2.0.0
1 year ago

This version converts the library to a Swift Package. The library is no longer available on CocoaPods. Please, migrate to Swift Package Manager.

New Features

  • SmartISODateFormatter now recognizes "just dates" and sets the correct formatOptions.
  • New smartISO8601 dateDecodingStrategy available for JSONDecoder.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics