ProductPromotion
Logo

Swift

made by https://0x3d.site

GitHub - Ryu0118/swift-typed-date: Library for enhancing Swift's Date handling by enabling type-level customization of date components
Library for enhancing Swift's Date handling by enabling type-level customization of date components - Ryu0118/swift-typed-date
Visit Site

GitHub - Ryu0118/swift-typed-date: Library for enhancing Swift's Date handling by enabling type-level customization of date components

GitHub - Ryu0118/swift-typed-date: Library for enhancing Swift's Date handling by enabling type-level customization of date components

TypedDate

Language:Swift License:MIT Latest Release Twitter

TypedDate is a wrapper for Swift's Date that enhances date handling at the type level. It enables the construction of custom date structures, ranging from single components like year to combinations of year, month, day, time, etc., tailored to specific development requirements.

Key features of TypedDate include:

  • Explicit Clarity in Date Components Allows developers to specify precisely which date components (year, month, day, etc.) they are working with, leading to a better understanding and less likelihood of inconsistencies.
  • Flexible Customization Enables the creation of date objects with different levels of detail, from a simple year-only structure to more comprehensive ones including month, day, second, and nanosecond.
  • Modifiable Date Components Provides methods for modifying individual components such as year, month or day.
  • Seamless Conversion Enables effortless conversion between 'TypedDate' and Swift's standard Date object, adapting to different scenarios by filling in any missing components as needed.

Usage

Initialization

To initialize a TypedDate, you can use the following syntax:

import TypedDate

TypedDate(Year(2023), Month(11), Day(12))
TypedDate(Year(2023), Month(11), Day(12), calendar: Calendar(identifier: .gregorian))
TypedDate(Year(2023), Month(11), Day(12), Hour(11), Minute(12), Second(1), Nanosecond(10000000))
TypedDate(Year(2023), Month(11), Day(12), Hour(11), Minute(12), FractionalSecond(5.12))

This will create a TypedDate instance representing the date 2023/11/12. Date has the following components available: Year, Month, Day, Hour, Minute, Second, and Nanosecond.

Date to TypedDate conversion

To create a TypedDate from a Date, use Date.scope(to:calendar:).

let typedDate1: TypedDate<(Year, Month)> = Date().scope(to: \.month)
let typedDate2: TypedDate<(Year, Month, Day, Hour)> = Date().scope(to: \.hour)
let typedDate3: TypedDate<(Year, Month, Day, Hour, Minute)> = Date().scope(to: \.minute, calendar: Calendar(identifier: .gregorian))
let typedDate4: TypedDateOfYear = Date().scope(to: \.year)
let typedDate5: TypedDateOfDay = Date().scope(to: \.day)

TypedDate to Date conversion

To convert from TypedDate to Date, use date property.

typedDate.date // Date

Fill

The fill method allows you to fill in a specific part of a date. For example:

let typedDate = TypedDate(Year(2023), Month(11), Day(12))
// typedDate: TypedDate<(Year, Month, Day)>

typedDate.fill(
    to: \.second,
    arguments: (Hour(11), Minute(12), Second(10))
)
// TypedDate<(Year, Month, Day, Hour, Minute, Second)>

typedDate.fill(
    to: \.hour,
    arguments: (Hour(11)),
    calendar: Calendar(identifier: .gregorian)
)
// TypedDate<(Year, Month, Day, Hour)>

In this example, filledDate will represent the date 2023/11/12 11:12

Erase

The erase method allows you to erase a specific part of a date. For example:

let date = TypedDate(Year(2023), Month(11), Day(12), Hour(11), Minute(12))
let erasedDate1: TypedDate<(Year, Month, Day)> = date.erase(to: \.day)
let erasedDate2: TypedDate<(Year, Month)> = date.erase(to: \.month)
let erasedDate2: TypedDate<(Year)> = date.erase(to: \.year, calendar: Calendar(identifier: .gregorian)

In this example, erasedDate will be erased up to date specified in keyPath.

Modify

The modify method allows you to modify a specific part of a date. For example:

let typedDate = TypedDate(Year(2023), Month(11), Day(12), Hour(11), Minute(12))
// typedDate: 2023/11/12 11:12

let modifiedDate = typedDate.modifying(\.year) { $0 += 1 }
    .modifying(\.month) { $0 -= 2 }
    .modifying(\.day) { $0 += 3 }
    .modifying(\.hour) { $0 += 4 }
    .modifying(\.minute) { $0 += 5 }
    .add(\.year, 1)
    .add(\.month, -2)
    .add(\.day, 3)
    .add(\.hour, -2)
    .add(\.minute, -3)
// modifiedDate: 2025/07/18 13:14

or use TypedDate.modify(_:calendar:modify:) method

var typedDate = TypedDate(Year(2023), Month(11))

typedDate.modify(\.year) { $0 += 1 }
// typedDate: 2024/11 

Access to each date component

let typedDate = TypedDate(Year(2023), Month(11), Day(12), Hour(11), Minute(12), Second(1), Nanosecond(10000000))
typedDate.year() // 2023
typedDate.month(calendar: Calendar(identifier: .gregorian)) // 11
typedDate.day() // 12
typedDate.hour(calendar: Calendar(identifier: .gregorian)) // 11
typedDate.minute() // 12
typedDate.second(calendar: Calendar(identifier: .gregorian) // 1
typedDate.nanosecond() // 10000000

Simplifying TypedDate with TypedDateOf〇〇 Aliases

This library includes the following typealias:

public typealias TypedDateOfYear = TypedDate<Year>
public typealias TypedDateOfMonth = TypedDate<(Year, Month)>
public typealias TypedDateOfDay = TypedDate<(Year, Month, Day)>
public typealias TypedDateOfHour = TypedDate<(Year, Month, Day, Hour)>
public typealias TypedDateOfMinute = TypedDate<(Year, Month, Day, Hour, Minute)>
public typealias TypedDateOfSecond = TypedDate<(Year, Month, Day, Hour, Minute, Second)>
public typealias TypedDateOfNanosecond = TypedDate<(Year, Month, Day, Hour, Minute, Second, Nanosecond)>

Conformance to Standard Protocols

TypedDate conforms to the Comparable, Equatable, Hashable, and Codable protocols, which makes it even more powerful and convenient compared to traditional date handling:

Comparable and Equatable

These protocols allow for easy comparison of TypedDate instances. You can check if one date is equal to, less than, or greater than another date using standard comparison operators (==, <, >, etc.). This is much more intuitive and less error-prone than comparing individual date components.

let date1: Date // 2023/11/12
let date2: Date // 2023/11/11

// To check the date have the same year
date1.scope(to: \.year) == date2.scope(to: \.year) // -> true

// To check the date have the same year, month, and day
date1.scope(to: \.day) == date2.scope(to: \.day) // -> false

// Compare days
date1.scope(to: \.day) > date2.scope(to: \.day) // -> true

Codable

The Codable conformance means that TypedDate instances can be easily encoded to and decoded from various formats such as JSON. This is particularly useful when working with APIs or saving dates to persistent storage.

Installation

.package(url: "https://github.com/Ryu0118/swift-typed-date", exact: "0.6.0")

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory