Skip to content

Commit

Permalink
Add documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
lvalenta committed Sep 12, 2024
1 parent e761250 commit 9c4f9be
Showing 1 changed file with 98 additions and 6 deletions.
104 changes: 98 additions & 6 deletions Sources/NaiveDateFormatStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,55 @@ public extension NaiveDate {
var timeZone: TimeZone
var capitalizationContext: FormatStyleCapitalizationContext

/// Creates a format style for a NaiveDate.
///
/// - Parameters:
/// - date: The style to use for formatting the date component. Defaults to `nil`.
/// - time: The style to use for formatting the time component. Defaults to `nil`.
/// - locale: The locale to use for formatting. Defaults to `autoupdatingCurrent`.
/// - calendar: The calendar to use for formatting. Defaults to `autoupdatingCurrent`.
/// - timeZone: The time zone to use for formatting. Defaults to `autoupdatingCurrent`.
/// - capitalizationContext: The context for capitalization. Defaults to `unknown`.
public init(date: Date.FormatStyle.DateStyle? = nil,
time: Date.FormatStyle.TimeStyle? = nil,
locale: Locale = .autoupdatingCurrent,
calendar: Calendar = .autoupdatingCurrent,
timeZone: TimeZone = .autoupdatingCurrent,
capitalizationContext: FormatStyleCapitalizationContext = .unknown) {
self.date = date
self.time = time
self.locale = locale
self.calendar = calendar
self.timeZone = timeZone
self.capitalizationContext = capitalizationContext
}

/// Formats the given NaiveDate value.
///
/// - Parameter value: The NaiveDate value to format.
/// - Returns: A formatted string representing the NaiveDate.
public func format(_ value: NaiveDate) -> String {
calendar.date(from: value).map { date in
let dateStyle = Date.FormatStyle(
date: self.date,
time: time,
time: self.time,
locale: locale,
calendar: calendar,
timeZone: timeZone,
capitalizationContext: capitalizationContext
)

return date.formatted(dateStyle)
} ?? ""
}

/// Returns a new format style with the specified locale.
///
/// - Parameter locale: The locale to apply to the format style.
/// - Returns: A new `NaiveDate.FormatStyle` with the given locale.
public func locale(_ locale: Locale) -> NaiveDate.FormatStyle {
.init(
date: date,
time: time,
locale: locale,
calendar: calendar,
timeZone: timeZone,
Expand All @@ -49,14 +67,27 @@ public extension NaiveDate {
}
}

/// Formats the NaiveDate using the provided format style.
///
/// - Parameter format: The format style to apply.
/// - Returns: The formatted string output.
func formatted<F: Foundation.FormatStyle>(_ format: F) -> F.FormatOutput where F.FormatInput == NaiveDate {
format.format(self)
}

/// Formats the NaiveDate using the default format style.
///
/// - Returns: A formatted string representation of the NaiveDate.
func formatted() -> String {
formatted(FormatStyle())
}

/// Formats the NaiveDate with specified date and time styles.
///
/// - Parameters:
/// - date: The style to use for formatting the date component.
/// - time: The style to use for formatting the time component.
/// - Returns: A formatted string representation of the NaiveDate.
func formatted(date: Date.FormatStyle.DateStyle, time: Date.FormatStyle.TimeStyle = .omitted) -> String {
formatted(FormatStyle(date: date, time: time))
}
Expand All @@ -72,37 +103,55 @@ public extension NaiveTime {
var timeZone: TimeZone
var capitalizationContext: FormatStyleCapitalizationContext

/// Creates a format style for a NaiveTime.
///
/// - Parameters:
/// - dateStyle: The style to use for formatting the date component. Defaults to `nil`.
/// - timeStyle: The style to use for formatting the time component. Defaults to `nil`.
/// - locale: The locale to use for formatting. Defaults to `autoupdatingCurrent`.
/// - calendar: The calendar to use for formatting. Defaults to `autoupdatingCurrent`.
/// - timeZone: The time zone to use for formatting. Defaults to `autoupdatingCurrent`.
/// - capitalizationContext: The context for capitalization. Defaults to `unknown`.
public init(date: Date.FormatStyle.DateStyle? = nil,
time: Date.FormatStyle.TimeStyle? = nil,
locale: Locale = .autoupdatingCurrent,
calendar: Calendar = .autoupdatingCurrent,
timeZone: TimeZone = .autoupdatingCurrent,
capitalizationContext: FormatStyleCapitalizationContext = .unknown) {
self.date = date
self.time = time
self.locale = locale
self.calendar = calendar
self.timeZone = timeZone
self.capitalizationContext = capitalizationContext
}

/// Formats the given NaiveTime value.
///
/// - Parameter value: The NaiveTime value to format.
/// - Returns: A formatted string representing the NaiveTime.
public func format(_ value: NaiveTime) -> String {
calendar.date(from: value).map { date in
let dateStyle = Date.FormatStyle(
date: self.date,
time: time,
time: self.time,
locale: locale,
calendar: calendar,
timeZone: timeZone,
capitalizationContext: capitalizationContext
)

return date.formatted(dateStyle)
} ?? ""
}

public func locale(_ locale: Locale) -> NaiveDate.FormatStyle {
/// Returns a new format style with the specified locale.
///
/// - Parameter locale: The locale to apply to the format style.
/// - Returns: A new `NaiveTime.FormatStyle` with the given locale.
public func locale(_ locale: Locale) -> NaiveTime.FormatStyle {
.init(
date: date,
time: time,
locale: locale,
calendar: calendar,
timeZone: timeZone,
Expand All @@ -111,14 +160,27 @@ public extension NaiveTime {
}
}

/// Formats the NaiveTime using the provided format style.
///
/// - Parameter format: The format style to apply.
/// - Returns: The formatted string output.
func formatted<F: Foundation.FormatStyle>(_ format: F) -> F.FormatOutput where F.FormatInput == NaiveTime {
format.format(self)
}

/// Formats the NaiveTime using the default format style.
///
/// - Returns: A formatted string representation of the NaiveTime.
func formatted() -> String {
formatted(FormatStyle())
}

/// Formats the NaiveTime with specified date and time styles.
///
/// - Parameters:
/// - date: The style to use for formatting the date component.
/// - time: The style to use for formatting the time component.
/// - Returns: A formatted string representation of the NaiveTime.
func formatted(date: Date.FormatStyle.DateStyle = .omitted, time: Date.FormatStyle.TimeStyle) -> String {
formatted(FormatStyle(date: date, time: time))
}
Expand All @@ -134,6 +196,15 @@ public extension NaiveDateTime {
var timeZone: TimeZone
var capitalizationContext: FormatStyleCapitalizationContext

/// Creates a format style for a NaiveDateTime.
///
/// - Parameters:
/// - dateStyle: The style to use for formatting the date component. Defaults to `nil`.
/// - timeStyle: The style to use for formatting the time component. Defaults to `nil`.
/// - locale: The locale to use for formatting. Defaults to `autoupdatingCurrent`.
/// - calendar: The calendar to use for formatting. Defaults to `autoupdatingCurrent`.
/// - timeZone: The time zone to use for formatting. Defaults to `autoupdatingCurrent`.
/// - capitalizationContext: The context for capitalization. Defaults to `unknown`.
public init(date: Date.FormatStyle.DateStyle? = nil,
time: Date.FormatStyle.TimeStyle? = nil,
locale: Locale = .autoupdatingCurrent,
Expand All @@ -147,7 +218,11 @@ public extension NaiveDateTime {
self.timeZone = timeZone
self.capitalizationContext = capitalizationContext
}


/// Formats the given NaiveDateTime value.
///
/// - Parameter value: The NaiveDateTime value to format.
/// - Returns: A formatted string representing the NaiveDateTime.
public func format(_ value: NaiveDateTime) -> String {
calendar.date(from: value).map { date in
let dateStyle = Date.FormatStyle(
Expand All @@ -163,6 +238,10 @@ public extension NaiveDateTime {
} ?? ""
}

/// Returns a new format style with the specified locale.
///
/// - Parameter locale: The locale to apply to the format style.
/// - Returns: A new `NaiveDateTime.FormatStyle` with the given locale.
public func locale(_ locale: Locale) -> NaiveDate.FormatStyle {
.init(
date: date,
Expand All @@ -174,14 +253,27 @@ public extension NaiveDateTime {
}
}

/// Formats the NaiveDateTime using the provided format style.
///
/// - Parameter format: The format style to apply.
/// - Returns: The formatted string output.
func formatted<F: Foundation.FormatStyle>(_ format: F) -> F.FormatOutput where F.FormatInput == NaiveDateTime {
format.format(self)
}

/// Formats the NaiveDateTime using the default format style.
///
/// - Returns: A formatted string representation of the NaiveDateTime.
func formatted() -> String {
formatted(FormatStyle())
}

/// Formats the NaiveDateTime with specified date and time styles.
///
/// - Parameters:
/// - date: The style to use for formatting the date component.
/// - time: The style to use for formatting the time component.
/// - Returns: A formatted string representation of the NaiveDateTime.
func formatted(date: Date.FormatStyle.DateStyle, time: Date.FormatStyle.TimeStyle) -> String {
formatted(FormatStyle(date: date, time: time))
}
Expand Down

0 comments on commit 9c4f9be

Please sign in to comment.