@@ -21,7 +21,7 @@ extension Test {
2121 /// An instant on the testing clock.
2222 public struct Instant : Sendable {
2323 /// The suspending-clock time corresponding to this instant.
24- fileprivate( set) var suspending = TimeValue ( SuspendingClock . Instant . now)
24+ fileprivate( set) var suspending = TimeValue ( rawValue : SuspendingClock ( ) . systemEpoch . duration ( to : . now) )
2525
2626#if !SWT_NO_UTC_CLOCK
2727 /// The wall-clock time corresponding to this instant.
@@ -35,10 +35,10 @@ extension Test {
3535#else
3636 timespec_get ( & wall, TIME_UTC)
3737#endif
38- return TimeValue ( wall)
38+ return TimeValue ( rawValue : . seconds ( wall. tv_sec ) + . nanoseconds ( wall . tv_nsec ) )
3939#else
4040#warning("Platform-specific implementation missing: UTC time unavailable (no timespec)")
41- return TimeValue ( ( 0 , 0 ) )
41+ return TimeValue ( rawValue : . zero )
4242#endif
4343 } ( )
4444#endif
@@ -63,52 +63,32 @@ extension SuspendingClock.Instant {
6363 /// - Parameters:
6464 /// - testClockInstant: The equivalent instant on ``Test/Clock``.
6565 public init ( _ testClockInstant: Test . Clock . Instant ) {
66- self . init ( testClockInstant. suspending)
66+ self = SuspendingClock ( ) . systemEpoch + testClockInstant. suspending. rawValue
6767 }
6868}
6969
70- extension Test . Clock . Instant {
7170#if !SWT_NO_UTC_CLOCK
72- /// The duration since 1970 represented by this instance as a tuple of seconds
73- /// and attoseconds.
74- ///
75- /// The value of this property is the equivalent of `self` on the wall clock.
76- /// It is suitable for display to the user, but not for fine timing
77- /// calculations.
78- public var timeComponentsSince1970 : ( seconds: Int64 , attoseconds: Int64 ) {
79- wall. components
80- }
81-
71+ extension Test . Clock . Instant {
8272 /// The duration since 1970 represented by this instance.
8373 ///
84- /// The value of this property is the equivalent of `self` on the wall clock.
85- /// It is suitable for display to the user, but not for fine timing
86- /// calculations.
87- public var durationSince1970 : Duration {
88- Duration ( wall)
74+ /// The Foundation overlay uses this property to implement `Date.init(_:)`.
75+ package var durationSince1970 : Duration {
76+ wall. rawValue
8977 }
78+ }
9079#endif
9180
92- /// Get the number of nanoseconds from this instance to another.
93- ///
94- /// - Parameters:
95- /// - other: The later instant.
96- ///
97- /// - Returns: The number of nanoseconds between `self` and `other`. If
98- /// `other` is ordered before this instance, the result is negative.
99- func nanoseconds( until other: Self ) -> Int64 {
100- if other < self {
101- return - other. nanoseconds ( until: self )
102- }
103- let otherNanoseconds = ( other. suspending. seconds * 1_000_000_000 ) + ( other. suspending. attoseconds / 1_000_000_000 )
104- let selfNanoseconds = ( suspending. seconds * 1_000_000_000 ) + ( suspending. attoseconds / 1_000_000_000 )
105- return otherNanoseconds - selfNanoseconds
81+ // MARK: - Clock
82+
83+ extension Test . Clock : _Concurrency . Clock {
84+ public var now : Instant {
85+ . now
10686 }
107- }
10887
109- // MARK: - Sleeping
88+ public var minimumResolution : Duration {
89+ SuspendingClock ( ) . minimumResolution
90+ }
11091
111- extension Test . Clock {
11292 /// Suspend the current task for the given duration.
11393 ///
11494 /// - Parameters:
@@ -124,35 +104,14 @@ extension Test.Clock {
124104#if !SWT_NO_UNSTRUCTURED_TASKS
125105 return try await SuspendingClock ( ) . sleep ( for: duration)
126106#elseif !SWT_NO_TIMESPEC
127- let timeValue = TimeValue ( duration)
128- var ts = timespec ( timeValue)
107+ var ts = timespec ( tv_sec: . init( duration. components. seconds) , tv_nsec: . init( duration. components. attoseconds / 1_000_000_000 ) )
129108 var tsRemaining = ts
130109 while 0 != nanosleep ( & ts, & tsRemaining) {
131110 try Task . checkCancellation ( )
132111 ts = tsRemaining
133112 }
134113#else
135114#warning("Platform-specific implementation missing: task sleep unavailable")
136- #endif
137- }
138- }
139-
140- // MARK: - Clock
141-
142- extension Test . Clock : _Concurrency . Clock {
143- public typealias Duration = SuspendingClock . Duration
144-
145- public var now : Instant {
146- . now
147- }
148-
149- public var minimumResolution : Duration {
150- #if SWT_TARGET_OS_APPLE
151- var res = timespec ( )
152- _ = clock_getres ( CLOCK_UPTIME_RAW, & res)
153- return Duration ( TimeValue ( res) )
154- #else
155- SuspendingClock ( ) . minimumResolution
156115#endif
157116 }
158117
@@ -170,59 +129,39 @@ extension Test.Clock: _Concurrency.Clock {
170129
171130extension Test . Clock . Instant : Equatable , Hashable , Comparable {
172131 public static func == ( lhs: Self , rhs: Self ) -> Bool {
173- lhs. suspending == rhs. suspending
132+ lhs. suspending. rawValue == rhs. suspending. rawValue
174133 }
175134
176135 public func hash( into hasher: inout Hasher ) {
177- hasher. combine ( suspending)
136+ hasher. combine ( suspending. rawValue )
178137 }
179138
180139 public static func < ( lhs: Self , rhs: Self ) -> Bool {
181- lhs. suspending < rhs. suspending
140+ lhs. suspending. rawValue < rhs. suspending. rawValue
182141 }
183142}
184143
185144// MARK: - InstantProtocol
186145
187146extension Test . Clock . Instant : InstantProtocol {
188- public typealias Duration = Swift . Duration
189-
190147 public func advanced( by duration: Duration ) -> Self {
191148 var result = self
192149
193- result. suspending = TimeValue ( Duration ( result. suspending) + duration)
150+ result. suspending = TimeValue ( rawValue : result. suspending. rawValue + duration)
194151#if !SWT_NO_UTC_CLOCK
195- result. wall = TimeValue ( Duration ( result. wall) + duration)
152+ result. wall = TimeValue ( rawValue : result. wall. rawValue + duration)
196153#endif
197154
198155 return result
199156 }
200157
201158 public func duration( to other: Test . Clock . Instant ) -> Duration {
202- Duration ( other. suspending) - Duration( suspending)
203- }
204- }
205-
206- // MARK: - Duration descriptions
207-
208- extension Test . Clock . Instant {
209- /// Get a description of the duration between this instance and another.
210- ///
211- /// - Parameters:
212- /// - other: The later instant.
213- ///
214- /// - Returns: A string describing the duration between `self` and `other`,
215- /// up to millisecond accuracy.
216- func descriptionOfDuration( to other: Test . Clock . Instant ) -> String {
217- #if SWT_TARGET_OS_APPLE
218- let ( seconds, nanosecondsRemaining) = nanoseconds ( until: other) . quotientAndRemainder ( dividingBy: 1_000_000_000 )
219- return String ( describing: TimeValue ( ( seconds, nanosecondsRemaining * 1_000_000_000 ) ) )
220- #else
221- return String ( describing: TimeValue ( Duration ( other. suspending) - Duration( suspending) ) )
222- #endif
159+ other. suspending. rawValue - suspending. rawValue
223160 }
224161}
225162
163+ #if !SWT_NO_SNAPSHOT_TYPES
226164// MARK: - Codable
227165
228166extension Test . Clock . Instant : Codable { }
167+ #endif
0 commit comments