|
9 | 9 | public import QuartzCore
|
10 | 10 | #endif
|
11 | 11 |
|
| 12 | +/// A type that represents a time value in seconds. |
| 13 | +/// |
| 14 | +/// Use `Time` to represent durations or time intervals within the OpenSwiftUI framework. |
| 15 | +/// The structure provides various operators and methods for time-related calculations. |
| 16 | +/// |
| 17 | +/// Example: |
| 18 | +/// |
| 19 | +/// let duration = Time(seconds: 2.5) |
| 20 | +/// let delay = Time.systemUptime + 1.0 |
| 21 | +/// |
12 | 22 | @_spi(ForOpenSwiftUIOnly)
|
13 | 23 | public struct Time: Equatable, Hashable, Comparable {
|
| 24 | + /// The time value in seconds. |
14 | 25 | public var seconds: Double
|
| 26 | + |
| 27 | + /// Creates a time value from the specified number of seconds. |
| 28 | + /// - Parameter seconds: The number of seconds. |
15 | 29 | public init(seconds: Double) {
|
16 | 30 | self.seconds = seconds
|
17 | 31 | }
|
| 32 | + |
| 33 | + /// Creates a time value initialized to zero seconds. |
18 | 34 | public init() {
|
19 | 35 | self.seconds = .zero
|
20 | 36 | }
|
| 37 | + |
| 38 | + /// A time value of zero seconds. |
21 | 39 | public static let zero: Time = Time(seconds: .zero)
|
22 |
| - public static let infinity: Time = Time(seconds: .infinity) |
23 | 40 |
|
24 |
| - #if canImport(QuartzCore) |
| 41 | + /// A time value representing infinity. |
| 42 | + public static let infinity: Time = Time(seconds: .infinity) |
| 43 | + |
| 44 | + /// The current system uptime as a `Time` value. |
| 45 | + /// |
| 46 | + /// This property provides the time since system boot using platform-specific |
| 47 | + /// implementation. |
25 | 48 | @inlinable
|
26 | 49 | public static var systemUptime: Time {
|
| 50 | + // NOTE: ProcessInfo.systemUptime is a general API available on all platforms via Foundation. |
| 51 | + // The implementation result of ProcessInfo().systemUptime and CACurrentMediaTime() is the same on Darwin platforms. |
| 52 | + // Both of them is using `mach_absolute_time` under the hood for Darwin platforms. |
| 53 | + // On non-Darwin platforms, `CACurrentMediaTime` is not available. But `ProcessInfo.systemUptime` is available. |
| 54 | + #if canImport(QuartzCore) |
27 | 55 | Time(seconds: CACurrentMediaTime())
|
| 56 | + #else |
| 57 | + Time(seconds: ProcessInfo().systemUptime) |
| 58 | + #endif |
28 | 59 | }
|
29 |
| - #endif |
30 | 60 |
|
| 61 | + /// Returns the negation of the specified time value. |
| 62 | + /// - Parameter lhs: A time value. |
| 63 | + /// - Returns: A time value that is the negation of the input. |
31 | 64 | @inlinable
|
32 |
| - prefix public static func - (lhs: Time) -> Time { |
| 65 | + public static prefix func - (lhs: Time) -> Time { |
33 | 66 | Time(seconds: -lhs.seconds)
|
34 | 67 | }
|
35 |
| - |
| 68 | + |
| 69 | + /// Adds a time value and a number of seconds. |
| 70 | + /// - Parameters: |
| 71 | + /// - lhs: A time value. |
| 72 | + /// - rhs: The number of seconds to add. |
| 73 | + /// - Returns: A new time value representing the sum. |
36 | 74 | @inlinable
|
37 | 75 | public static func + (lhs: Time, rhs: Double) -> Time {
|
38 | 76 | Time(seconds: lhs.seconds + rhs)
|
39 | 77 | }
|
40 | 78 |
|
| 79 | + /// Adds a number of seconds to a time value. |
| 80 | + /// - Parameters: |
| 81 | + /// - lhs: The number of seconds to add. |
| 82 | + /// - rhs: A time value. |
| 83 | + /// - Returns: A new time value representing the sum. |
41 | 84 | @inlinable
|
42 | 85 | public static func + (lhs: Double, rhs: Time) -> Time {
|
43 | 86 | rhs + lhs
|
44 | 87 | }
|
45 | 88 |
|
| 89 | + /// Subtracts a number of seconds from a time value. |
| 90 | + /// - Parameters: |
| 91 | + /// - lhs: A time value. |
| 92 | + /// - rhs: The number of seconds to subtract. |
| 93 | + /// - Returns: A new time value representing the difference. |
46 | 94 | @inlinable
|
47 | 95 | public static func - (lhs: Time, rhs: Double) -> Time {
|
48 | 96 | Time(seconds: lhs.seconds - rhs)
|
49 | 97 | }
|
50 | 98 |
|
| 99 | + /// Calculates the time difference between two time values. |
| 100 | + /// - Parameters: |
| 101 | + /// - lhs: The first time value. |
| 102 | + /// - rhs: The second time value. |
| 103 | + /// - Returns: The difference in seconds between the two time values. |
51 | 104 | @inlinable
|
52 | 105 | public static func - (lhs: Time, rhs: Time) -> Double {
|
53 | 106 | lhs.seconds - rhs.seconds
|
54 | 107 | }
|
55 |
| - |
| 108 | + |
| 109 | + /// Multiplies a time value by a scalar. |
| 110 | + /// - Parameters: |
| 111 | + /// - lhs: A time value. |
| 112 | + /// - rhs: The scalar to multiply by. |
| 113 | + /// - Returns: A new time value representing the product. |
56 | 114 | @inlinable
|
57 | 115 | public static func * (lhs: Time, rhs: Double) -> Time {
|
58 | 116 | Time(seconds: lhs.seconds * rhs)
|
59 | 117 | }
|
60 |
| - |
| 118 | + |
| 119 | + /// Divides a time value by a scalar. |
| 120 | + /// - Parameters: |
| 121 | + /// - lhs: A time value. |
| 122 | + /// - rhs: The scalar to divide by. |
| 123 | + /// - Returns: A new time value representing the quotient. |
61 | 124 | @inlinable
|
62 | 125 | public static func / (lhs: Time, rhs: Double) -> Time {
|
63 | 126 | return Time(seconds: lhs.seconds / rhs)
|
64 | 127 | }
|
65 |
| - |
| 128 | + |
| 129 | + /// Adds a number of seconds to a time value, storing the result in the left-hand operand. |
| 130 | + /// - Parameters: |
| 131 | + /// - lhs: The time value to modify. |
| 132 | + /// - rhs: The number of seconds to add. |
66 | 133 | @inlinable
|
67 | 134 | public static func += (lhs: inout Time, rhs: Double) {
|
68 | 135 | lhs = lhs + rhs
|
69 | 136 | }
|
70 | 137 |
|
| 138 | + /// Subtracts a number of seconds from a time value, storing the result in the left-hand operand. |
| 139 | + /// - Parameters: |
| 140 | + /// - lhs: The time value to modify. |
| 141 | + /// - rhs: The number of seconds to subtract. |
71 | 142 | @inlinable
|
72 | 143 | public static func -= (lhs: inout Time, rhs: Double) {
|
73 | 144 | lhs = lhs - rhs
|
74 | 145 | }
|
75 | 146 |
|
| 147 | + /// Multiplies a time value by a scalar, storing the result in the left-hand operand. |
| 148 | + /// - Parameters: |
| 149 | + /// - lhs: The time value to modify. |
| 150 | + /// - rhs: The scalar to multiply by. |
76 | 151 | @inlinable
|
77 | 152 | public static func *= (lhs: inout Time, rhs: Double) {
|
78 | 153 | lhs = lhs * rhs
|
79 | 154 | }
|
80 |
| - |
| 155 | + |
| 156 | + /// Divides a time value by a scalar, storing the result in the left-hand operand. |
| 157 | + /// - Parameters: |
| 158 | + /// - lhs: The time value to modify. |
| 159 | + /// - rhs: The scalar to divide by. |
81 | 160 | @inlinable
|
82 | 161 | public static func /= (lhs: inout Time, rhs: Double) {
|
83 | 162 | lhs = lhs / rhs
|
84 | 163 | }
|
85 | 164 |
|
| 165 | + /// Returns a Boolean value indicating whether the first time value is less than the second. |
| 166 | + /// - Parameters: |
| 167 | + /// - lhs: A time value to compare. |
| 168 | + /// - rhs: Another time value to compare. |
| 169 | + /// - Returns: `true` if the first value is less than the second value; otherwise, `false`. |
86 | 170 | @inlinable
|
87 | 171 | public static func < (lhs: Time, rhs: Time) -> Bool {
|
88 | 172 | lhs.seconds < rhs.seconds
|
89 | 173 | }
|
90 |
| - |
| 174 | + |
91 | 175 | @inlinable
|
92 | 176 | public static func == (a: Time, b: Time) -> Bool {
|
93 | 177 | a.seconds == b.seconds
|
94 | 178 | }
|
95 |
| - |
| 179 | + |
96 | 180 | @inlinable
|
97 | 181 | public func hash(into hasher: inout Hasher) {
|
98 | 182 | hasher.combine(seconds)
|
|
0 commit comments