|
| 1 | +# Clock Epochs |
| 2 | + |
| 3 | +* Proposal: [SE-NNNN](NNNN-ClockEpochs.md) |
| 4 | +* Authors: [Philippe Hausler](https://github.com/phausler) |
| 5 | +* Review Manager: TBD |
| 6 | +* Status: **Awaiting implementation** |
| 7 | +* Implementation: |
| 8 | + |
| 9 | +* Previous Proposal: *if applicable* [SE-0329](0329-clock-instant-duration.md) |
| 10 | +* Review: ([pitch](https://forums.swift.org/t/pitch-suspendingclock-and-continuousclock-epochs/78017)) |
| 11 | + |
| 12 | +## Introduction |
| 13 | + |
| 14 | +[The proposal for Clock, Instant and Duration](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0329-clock-instant-duration.md) brought in two primary clock types: `SuspendingClock` and `ContinuousClock`. These both have a concept of a reference point for their `Instant` types. |
| 15 | + |
| 16 | +## Motivation |
| 17 | + |
| 18 | +Not all clocks have a starting point, however in these cases they do. Generally, it cannot required for a clock's instant definition to have a start or the start may not be a fixed point. However it can be useful that a given instant can be constructed to determine the elapsed duration from the starting point of that clock if it does have it. |
| 19 | + |
| 20 | +## Proposed solution |
| 21 | + |
| 22 | +Two new properties will be added, one to `SuspendingClock` and another to `ContinuousClock`. Both of these properties will be the epoch for which all `Instant` types are derived from; practically speaking this is the "zero" point for these clocks. |
| 23 | + |
| 24 | +## Detailed design |
| 25 | + |
| 26 | +```swift |
| 27 | +extension ContinousClock { |
| 28 | + public var epoch: Instant { get } |
| 29 | +} |
| 30 | + |
| 31 | +extension SuspendingClock { |
| 32 | + public var epoch: Instant { get } |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +These can be used to gather information like for example the uptime of a system, or the active time of a system; |
| 37 | + |
| 38 | +```swift |
| 39 | +let clock = ContinousClock() |
| 40 | +let uptime = clock.now - clock.epoch |
| 41 | +``` |
| 42 | + |
| 43 | +Or likewise; |
| 44 | + |
| 45 | +```swift |
| 46 | +let clock = SuspendingClock() |
| 47 | +let activeTime = clock.now - clock.epoch |
| 48 | +``` |
| 49 | + |
| 50 | +## ABI compatibility |
| 51 | + |
| 52 | +This is a purely additive change and provides no direct impact to existing ABI. It only carries the ABI impact of new properties being added to an existing type. |
| 53 | + |
| 54 | +## Alternatives considered |
| 55 | + |
| 56 | +It was considered to add a constructor or static member to the `SuspendingClock.Instant` and `ContinousClock.Instant` however the home on the clock itself provides a more discoverable and nameable location. |
| 57 | + |
| 58 | +It is suggested that this be used as an informal protocol for other clocks. It was considered as an additional protocol but that was ultimately rejected because no generic function made much sense that would not be better served with generic specialization or explicit clock parameter types. |
0 commit comments