Skip to content

Commit 53a5400

Browse files
committed
Rename GregorianTimeOfDay to just GregorianTime + add more Date integration
1 parent f392789 commit 53a5400

File tree

4 files changed

+77
-25
lines changed

4 files changed

+77
-25
lines changed

Sources/HandySwift/Extensions/DateExt.swift

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,27 @@
11
import Foundation
22

33
extension Date {
4-
/// Returns a date offset by the specified time interval from this date to the past.
4+
/// Creates a new Date by combining a ``GregorianDay`` with a ``GregorianTime``.
5+
///
6+
/// This initializer allows you to create a Date instance from separate day and time components.
7+
/// The resulting date will be in the specified timezone (defaulting to the current timezone).
8+
///
9+
/// Example:
10+
/// ```swift
11+
/// let day = GregorianDay(year: 2024, month: 3, day: 21)
12+
/// let time = GregorianTime(hour: 14, minute: 30)
13+
/// let eventTime = Date(day: day, time: time)
14+
/// ```
15+
///
16+
/// - Parameters:
17+
/// - day: The GregorianDay representing the date components.
18+
/// - time: The GregorianTime representing the time components.
19+
/// - timeZone: The timezone to use for the date creation. Defaults to the current timezone.
20+
public init(day: GregorianDay, time: GregorianTime, timeZone: TimeZone = .current) {
21+
self = time.date(day: day, timeZone: timeZone)
22+
}
23+
24+
/// Returns a date offset by the specified time interval from this date to the past.
525
/// This method is useful when you need to calculate a date that occurred a certain duration before the current date instance.
626
/// For example, if you want to find out what the date was 2 hours ago from a given date, you can use this method by passing the time interval for 2 hours in seconds.
727
///

Sources/HandySwift/HandySwift.docc/Essentials/New Types.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ let todayNextWeek = GregorianDay.today.advanced(by: 7)
4747

4848
> Note: `GregorianDay` conforms to all the protocols you would expect, such as `Codable`, `Hashable`, and `Comparable`. For encoding/decoding, it uses the ISO format as in "2014-07-13".
4949
50-
``GregorianTimeOfDay`` is the counterpart:
50+
``GregorianTime`` is the counterpart:
5151

5252
```swift
53-
let iPhoneAnnounceTime = GregorianTimeOfDay(hour: 09, minute: 41)
54-
let anHourFromNow = GregorianTimeOfDay.now.advanced(by: .hours(1))
53+
let iPhoneAnnounceTime = GregorianTime(hour: 09, minute: 41)
54+
let anHourFromNow = GregorianTime.now.advanced(by: .hours(1))
5555

5656
let date = iPhoneAnnounceTime.date(day: GregorianDay.today) // => Date
5757
```
@@ -120,7 +120,7 @@ Note that the ``Debouncer`` was stored in a property so ``Debouncer/cancelAll()`
120120
### Date & Time
121121

122122
- ``GregorianDay``
123-
- ``GregorianTimeOfDay``
123+
- ``GregorianTime``
124124

125125
### UI Helpers
126126

Sources/HandySwift/Types/GregorianDay.swift

+15
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,21 @@ public struct GregorianDay {
218218
)
219219
return components.date!
220220
}
221+
222+
/// Returns a `Date` representing this day at the specified time.
223+
///
224+
/// - Parameters:
225+
/// - timeOfDay: The time of day to set for the resulting date.
226+
/// - timeZone: The time zone for which to calculate the date. Defaults to the users current timezone.
227+
/// - Returns: A `Date` representing this day at the specified time.
228+
///
229+
/// Example:
230+
/// ```swift
231+
/// let noonToday = GregorianDay.today.date(timeOfDay: .noon) // today at 12:00
232+
/// ```
233+
public func date(timeOfDay: GregorianTime, timeZone: TimeZone = .current) -> Date {
234+
timeOfDay.date(day: self, timeZone: timeZone)
235+
}
221236
}
222237

223238
extension GregorianDay: Codable {

Sources/HandySwift/Types/GregorianTimeOfDay.swift renamed to Sources/HandySwift/Types/GregorianTime.swift

+37-20
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ import Foundation
22

33
/// A time without date info.
44
///
5-
/// `GregorianTimeOfDay` represents a time of day without any associated date information. It provides functionalities to work with time components like hour, minute, and second, and perform operations such as initializing from a given date, calculating durations, advancing, and reversing time.
5+
/// `GregorianTime` represents a time of day without any associated date information. It provides functionalities to work with time components like hour, minute, and second, and perform operations such as initializing from a given date, calculating durations, advancing, and reversing time.
66
///
77
/// Example:
88
/// ```swift
99
/// // Initializing from a given date
1010
/// let date = Date()
11-
/// let timeOfDay = GregorianTimeOfDay(date: date)
11+
/// let timeOfDay = GregorianTime(date: date)
1212
///
1313
/// // Calculating duration since the start of the day
1414
/// let durationSinceStartOfDay: Duration = timeOfDay.durationSinceStartOfDay
@@ -20,7 +20,7 @@ import Foundation
2020
/// // Reversing time by a duration
2121
/// let reversedTime = timeOfDay.reversed(by: .minutes(15))
2222
/// ```
23-
public struct GregorianTimeOfDay {
23+
public struct GregorianTime {
2424
/// The number of days beyond the current day.
2525
public var overflowingDays: Int
2626
/// The hour component of the time.
@@ -30,7 +30,7 @@ public struct GregorianTimeOfDay {
3030
/// The second component of the time.
3131
public var second: Int
3232

33-
/// Initializes a `GregorianTimeOfDay` instance from a given date.
33+
/// Initializes a `GregorianTime` instance from a given date.
3434
///
3535
/// - Parameter date: The date from which to extract time components.
3636
public init(date: Date) {
@@ -41,7 +41,7 @@ public struct GregorianTimeOfDay {
4141
self.second = components.second!
4242
}
4343

44-
/// Initializes a `GregorianTimeOfDay` instance with the provided time components.
44+
/// Initializes a `GregorianTime` instance with the provided time components.
4545
///
4646
/// - Parameters:
4747
/// - hour: The hour component.
@@ -64,7 +64,7 @@ public struct GregorianTimeOfDay {
6464
/// - day: The day to which the time belongs.
6565
/// - timeZone: The time zone to use for the conversion (default is the current time zone).
6666
/// - Returns: A `Date` object representing the time.
67-
public func date(day: GregorianDay, timeZone: TimeZone = Calendar.current.timeZone) -> Date {
67+
public func date(day: GregorianDay, timeZone: TimeZone = .current) -> Date {
6868
let components = DateComponents(
6969
calendar: Calendar(identifier: .gregorian),
7070
timeZone: timeZone,
@@ -78,7 +78,7 @@ public struct GregorianTimeOfDay {
7878
return components.date!.addingTimeInterval(.days(Double(self.overflowingDays)))
7979
}
8080

81-
/// Initializes a `GregorianTimeOfDay` instance from the duration since the start of the day.
81+
/// Initializes a `GregorianTime` instance from the duration since the start of the day.
8282
///
8383
/// - Parameter durationSinceStartOfDay: The duration since the start of the day.
8484
@available(iOS 16, macOS 13, tvOS 16, visionOS 1, watchOS 9, *)
@@ -98,48 +98,65 @@ public struct GregorianTimeOfDay {
9898
/// Advances the time by the specified duration.
9999
///
100100
/// - Parameter duration: The duration by which to advance the time.
101-
/// - Returns: A new `GregorianTimeOfDay` instance advanced by the specified duration.
101+
/// - Returns: A new `GregorianTime` instance advanced by the specified duration.
102102
@available(iOS 16, macOS 13, tvOS 16, visionOS 1, watchOS 9, *)
103103
public func advanced(by duration: Duration) -> Self {
104-
GregorianTimeOfDay(durationSinceStartOfDay: self.durationSinceStartOfDay + duration)
104+
GregorianTime(durationSinceStartOfDay: self.durationSinceStartOfDay + duration)
105105
}
106106

107107
/// Reverses the time by the specified duration.
108108
///
109109
/// - Parameter duration: The duration by which to reverse the time.
110-
/// - Returns: A new `GregorianTimeOfDay` instance reversed by the specified duration.
110+
/// - Returns: A new `GregorianTime` instance reversed by the specified duration.
111111
@available(iOS 16, macOS 13, tvOS 16, visionOS 1, watchOS 9, *)
112112
public func reversed(by duration: Duration) -> Self {
113-
GregorianTimeOfDay(durationSinceStartOfDay: self.durationSinceStartOfDay - duration)
113+
GregorianTime(durationSinceStartOfDay: self.durationSinceStartOfDay - duration)
114114
}
115115
}
116116

117-
extension GregorianTimeOfDay: Codable, Hashable, Sendable {}
118-
extension GregorianTimeOfDay: Identifiable {
117+
extension GregorianTime: Codable, Hashable, Sendable {}
118+
extension GregorianTime: Identifiable {
119119
/// The unique identifier of the time, formatted as "hour:minute:second".
120120
public var id: String { "\(self.hour):\(self.minute):\(self.second)" }
121121
}
122122

123-
extension GregorianTimeOfDay: Comparable {
124-
/// Compares two `GregorianTimeOfDay` instances.
123+
extension GregorianTime: Comparable {
124+
/// Compares two `GregorianTime` instances.
125125
///
126126
/// - Parameters:
127127
/// - left: The left-hand side of the comparison.
128128
/// - right: The right-hand side of the comparison.
129129
/// - Returns: `true` if the left time is less than the right time; otherwise, `false`.
130-
public static func < (left: GregorianTimeOfDay, right: GregorianTimeOfDay) -> Bool {
130+
public static func < (left: GregorianTime, right: GregorianTime) -> Bool {
131131
guard left.overflowingDays == right.overflowingDays else { return left.overflowingDays < right.overflowingDays }
132132
guard left.hour == right.hour else { return left.hour < right.hour }
133133
guard left.minute == right.minute else { return left.minute < right.minute }
134134
return left.second < right.second
135135
}
136136
}
137137

138-
extension GregorianTimeOfDay {
138+
extension GregorianTime {
139139
/// The zero time of day (00:00:00).
140-
public static var zero: Self { GregorianTimeOfDay(hour: 0, minute: 0, second: 0) }
140+
public static var zero: Self { GregorianTime(hour: 0, minute: 0, second: 0) }
141141
/// The current time of day.
142-
public static var now: Self { GregorianTimeOfDay(date: Date()) }
142+
public static var now: Self { GregorianTime(date: Date()) }
143+
/// Noon (12:00:00).
144+
public static var noon: Self { GregorianTime(hour: 12, minute: 0, second: 0) }
143145
}
144146

145-
extension GregorianTimeOfDay: Withable {}
147+
extension GregorianTime: Withable {}
148+
149+
/// Provides backward compatibility for the renamed `GregorianTime` type.
150+
///
151+
/// This type has been renamed to ``GregorianTime`` to better reflect its purpose and maintain consistency with other types in the framework.
152+
///
153+
/// Instead of using `GregorianTimeOfDay`, use ``GregorianTime``:
154+
/// ```swift
155+
/// // Old code:
156+
/// let time = GregorianTimeOfDay(hour: 14, minute: 30)
157+
///
158+
/// // New code:
159+
/// let time = GregorianTime(hour: 14, minute: 30)
160+
/// ```
161+
@available(*, deprecated, renamed: "GregorianTime", message: "Use GregorianTime instead. This type has been renamed for better clarity and consistency.")
162+
public typealias GregorianTimeOfDay = GregorianTime

0 commit comments

Comments
 (0)