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