|
| 1 | +// |
| 2 | +// Time.swift |
| 3 | +// OpenSwiftUICore |
| 4 | +// |
| 5 | +// Audited for RELEASE_2024 |
| 6 | +// Status: Complete |
| 7 | + |
| 8 | +#if canImport(QuartzCore) |
| 9 | +import QuartzCore |
| 10 | +#endif |
| 11 | + |
| 12 | +@_spi(ForOpenSwiftUIOnly) |
| 13 | +public struct Time: Equatable, Hashable, Comparable { |
| 14 | + public var seconds: Double |
| 15 | + public init(seconds: Double) { |
| 16 | + self.seconds = seconds |
| 17 | + } |
| 18 | + public init() { |
| 19 | + self.seconds = .zero |
| 20 | + } |
| 21 | + public static let zero: Time = Time(seconds: .zero) |
| 22 | + public static let infinity: Time = Time(seconds: .infinity) |
| 23 | + |
| 24 | + #if canImport(QuartzCore) |
| 25 | + public static var systemUptime: Time { |
| 26 | + Time(seconds: CACurrentMediaTime()) |
| 27 | + } |
| 28 | + #endif |
| 29 | + |
| 30 | + @inlinable |
| 31 | + prefix public static func - (lhs: Time) -> Time { |
| 32 | + Time(seconds: -lhs.seconds) |
| 33 | + } |
| 34 | + |
| 35 | + @inlinable |
| 36 | + public static func + (lhs: Time, rhs: Double) -> Time { |
| 37 | + Time(seconds: lhs.seconds + rhs) |
| 38 | + } |
| 39 | + |
| 40 | + @inlinable |
| 41 | + public static func + (lhs: Double, rhs: Time) -> Time { |
| 42 | + rhs + lhs |
| 43 | + } |
| 44 | + |
| 45 | + @inlinable |
| 46 | + public static func - (lhs: Time, rhs: Double) -> Time { |
| 47 | + Time(seconds: lhs.seconds - rhs) |
| 48 | + } |
| 49 | + |
| 50 | + @inlinable |
| 51 | + public static func - (lhs: Time, rhs: Time) -> Double { |
| 52 | + lhs.seconds - rhs.seconds |
| 53 | + } |
| 54 | + |
| 55 | + @inlinable |
| 56 | + public static func * (lhs: Time, rhs: Double) -> Time { |
| 57 | + Time(seconds: lhs.seconds * rhs) |
| 58 | + } |
| 59 | + |
| 60 | + @inlinable |
| 61 | + public static func / (lhs: Time, rhs: Double) -> Time { |
| 62 | + return Time(seconds: lhs.seconds / rhs) |
| 63 | + } |
| 64 | + |
| 65 | + @inlinable |
| 66 | + public static func += (lhs: inout Time, rhs: Double) { |
| 67 | + lhs = lhs + rhs |
| 68 | + } |
| 69 | + |
| 70 | + @inlinable |
| 71 | + public static func -= (lhs: inout Time, rhs: Double) { |
| 72 | + lhs = lhs - rhs |
| 73 | + } |
| 74 | + |
| 75 | + @inlinable |
| 76 | + public static func *= (lhs: inout Time, rhs: Double) { |
| 77 | + lhs = lhs * rhs |
| 78 | + } |
| 79 | + |
| 80 | + @inlinable |
| 81 | + public static func /= (lhs: inout Time, rhs: Double) { |
| 82 | + lhs = lhs / rhs |
| 83 | + } |
| 84 | + |
| 85 | + @inlinable |
| 86 | + public static func < (lhs: Time, rhs: Time) -> Bool { |
| 87 | + lhs.seconds < rhs.seconds |
| 88 | + } |
| 89 | + |
| 90 | + @inlinable |
| 91 | + public static func == (a: Time, b: Time) -> Bool { |
| 92 | + a.seconds == b.seconds |
| 93 | + } |
| 94 | + |
| 95 | + @inlinable |
| 96 | + public func hash(into hasher: inout Hasher) { |
| 97 | + hasher.combine(seconds) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +@_spi(ForOpenSwiftUIOnly) |
| 102 | +extension Time: Sendable {} |
0 commit comments