|
3 | 3 | * Proposal: [SE-0329](0329-clock-instant-duration.md)
|
4 | 4 | * Author: [Philippe Hausler](https://github.com/phausler)
|
5 | 5 | * Review Manager: [John McCall](https://github.com/rjmccall)
|
6 |
| -* Status: **Active Review (January 19th...24th, 2022)** |
| 6 | +* Status: **Active Review (January 19th...February 7th, 2022)** |
7 | 7 | * Implementation: [apple/swift#40609](https://github.com/apple/swift/pull/40609)
|
8 | 8 | * Review: ([first review](https://forums.swift.org/t/se-0329-clock-instant-date-and-duration/53309)) ([returned for revision](https://forums.swift.org/t/returned-for-revision-se-0329-clock-instant-date-and-duration/53635)) ([second review](https://forums.swift.org/t/se-0329-second-review-clock-instant-and-duration/54509)) ([third review](https://forums.swift.org/t/se-0329-third-review-clock-instant-and-duration/54727))
|
9 | 9 |
|
|
52 | 52 | * **v3.0**
|
53 | 53 | * Moved `measure` into a category from a protocol requirement
|
54 | 54 | * Renamed the `nanoseconds` and `seconds` property of `Duration` to `nanosecondsPortion` and `secondsPortion` to indicate their fractional composition to types like `timespec`
|
| 55 | +* **v3.1** |
| 56 | + * Adjust the portion accessors to one singular `components` based accessor and add an initializer for raw value construction from components. |
55 | 57 |
|
56 | 58 | </details>
|
57 | 59 |
|
@@ -203,14 +205,15 @@ The naming of `DurationProtocol` was chosen because we feel that the canonical d
|
203 | 205 |
|
204 | 206 | Meaningful durations can always be expressed in terms of nanoseconds plus a number of seconds, either a duration before a reference point or after. They can be constructed from meaningful human measured (or machine measured precision) but should not account for any calendrical calculations (e.g., a measure of days, months or years distinctly need a calendar to be meaningful). Durations should able to be serialized, compared, and stored as keys, but also should be able to be added and subtracted (and zero is meaningful). They are distinctly NOT `Numeric` due to the aforementioned issue with regards to multiplying two `TimeInterval` variables. That being said, there is utility for ad-hoc division and multiplication to calculate back-offs.
|
205 | 207 |
|
206 |
| -The `Duration` must be able to account for high scale resolution of calculation; the storage will under the hood ensure proper rounding for division (by likely storing higher precision than exposed) and enough range to span the full range of potential reasonable instants. This means that spanning the full range of +/- thousands of years at a non lossy scale can be accomplished by storing the seconds and nanoseconds. Not all systems will need that full range, however in order to properly represent nanosecond precision across the full range of times expressed in the operating systems that Swift works on a full 128 bit storage is needed to represent these values. That in turn necessitates exposing the conversion to existing types as breaking the duration into two portions. These portions of a duration are exposed for interoperability with existing APIs such as `timespec` as a seconds portion and nanoseconds portion. |
| 208 | +The `Duration` must be able to account for high scale resolution of calculation; the storage will under the hood ensure proper rounding for division (by likely storing higher precision than exposed) and enough range to span the full range of potential reasonable instants. This means that spanning the full range of +/- thousands of years at a non lossy scale can be accomplished by storing the seconds and nanoseconds. Not all systems will need that full range, however in order to properly represent nanosecond precision across the full range of times expressed in the operating systems that Swift works on a full 128 bit storage is needed to represent these values. That in turn necessitates exposing the conversion to existing types as breaking the duration into two components. These components of a duration are exposed for interoperability with existing APIs such as `timespec` as a seconds portion and an attoseconds portion (used to ensure full precision is not lost). If the Swift language gains a signed integer type that can support 128 bits of storage then `Duration` should be considered to replace the components accessor and initializer with a direct access and initialization to that stored attoseconds value. |
207 | 209 |
|
208 | 210 | ```swift
|
209 | 211 | public struct Duration: Sendable {
|
210 |
| - public var secondsPortion: Int64 { get } |
211 |
| - public var nanosecondsPortion: Int64 { get } |
| 212 | + public var components: (seconds: Int64, attoseconds: Int64) { get } |
| 213 | + public init(secondsComponent: Int64, attosecondsComponent: Int64) |
212 | 214 | }
|
213 | 215 |
|
| 216 | + |
214 | 217 | extension Duration {
|
215 | 218 | public static func seconds<T: BinaryInteger>(_ seconds: T) -> Duration
|
216 | 219 | public static func seconds(_ seconds: Double) -> Duration
|
|
0 commit comments