Duration
A representation of high precision time.
- iOS
- 16.0+
- macOS
- 13.0+
- tvOS
- 16.0+
- watchOS
- 9.0+
@frozen struct Duration
Duration
represents an elapsed time value with high precision in an integral form. It may be used for measurements of varying clock sources. In those cases it represents the elapsed time measured by that clock. Calculations using Duration
may span from a negative value to a positive value and have a suitable range to at least cover attosecond scale for both small elapsed durations like sub-second precision to durations that span centuries.
Typical construction of Duration
values should be created via the static methods for specific time values.
var d: Duration = .seconds(3)
d += .milliseconds(33)
print(d) // 3.033 seconds
Duration
itself does not ferry any additional information other than the temporal measurement component; specifically leap seconds should be represented as an additional accessor since that is specific only to certain clock implementations.
Citizens in Swift
Conformances
protocol AdditiveArithmetic
A type with values that support addition and subtraction.
protocol Comparable
A type that can be compared using the relational operators
<
,<=
,>=
, and>
.protocol CustomStringConvertible
A type with a customized textual representation.
protocol Decodable
A type that can decode itself from an external representation.
protocol DurationProtocol
A type that defines a duration for a given
InstantProtocol
type.protocol Encodable
A type that can encode itself to an external representation.
protocol Equatable
A type that can be compared for value equality.
protocol Hashable
A type that can be hashed into a
Hasher
to produce an integer hash value.protocol Sendable
A type whose values can safely be passed across concurrency domains by copying.
Members
init(from: Decoder
) throws init(secondsComponent: Int64, attosecondsComponent: Int64
) Construct a
Duration
by adding attoseconds to a seconds value.static var zero: Duration
static func microseconds(Double
) -> Duration Construct a
Duration
given a number of seconds microseconds as aDouble
by converting the value into the closest attosecond scale value.static func microseconds<T>(T
) -> Duration Construct a
Duration
given a number of microseconds represented as aBinaryInteger
.static func milliseconds(Double
) -> Duration Construct a
Duration
given a number of seconds milliseconds as aDouble
by converting the value into the closest attosecond scale value.static func milliseconds<T>(T
) -> Duration Construct a
Duration
given a number of milliseconds represented as aBinaryInteger
.static func nanoseconds<T>(T
) -> Duration Construct a
Duration
given a number of nanoseconds represented as aBinaryInteger
.static func seconds(Double
) -> Duration Construct a
Duration
given a number of seconds represented as aDouble
by converting the value into the closest attosecond scale value.static func seconds<T>(T
) -> Duration Construct a
Duration
given a number of seconds represented as aBinaryInteger
.var components: (seconds: Int64, attoseconds: Int64)
The composite components of the
Duration
.var description: String
static func * (Duration, Double
) -> Duration static func * <T>(Duration, T
) -> Duration static func *= <T>(inout Duration, T
) static func + (Duration, Duration
) -> Duration static func += (inout Duration, Duration
) static func - (Duration, Duration
) -> Duration static func -= (inout Duration, Duration
) static func / (Duration, Double
) -> Duration static func / <T>(Duration, T
) -> Duration static func / (Duration, Duration
) -> Double static func /= (inout Duration, Double
) static func /= <T>(inout Duration, T
) static func < (Duration, Duration
) -> Bool static func == (Duration, Duration
) -> Bool func encode(to: Encoder
) throws func hash(into: inout Hasher
)
Features
static func != (Self, Self
) -> Bool static func + (Self
) -> Self Returns the given number unchanged.
static func ... (Self
) -> PartialRangeFrom<Self> Returns a partial range extending upward from a lower bound.
static func ... (Self
) -> PartialRangeThrough<Self> Returns a partial range up to, and including, its upper bound.
static func ... (Self, Self
) -> ClosedRange<Self> Returns a closed range that contains both of its bounds.
static func ..< (Self
) -> PartialRangeUpTo<Self> Returns a partial range up to, but not including, its upper bound.
static func ..< (Self, Self
) -> Range<Self> Returns a half-open range that contains its lower bound but not its upper bound.
Extension in Durations
Members
Extension in NIOCore
Members
init(TimeAmount
) Construct a
Duration
given a number of nanoseconds represented as aTimeAmount
.