-
Notifications
You must be signed in to change notification settings - Fork 19
Draft: Proposed clocks improvements #71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
ptomato
wants to merge
12
commits into
WebAssembly:main
Choose a base branch
from
ptomato:proposed-clocks-improvements
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+111
−110
Draft
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
5c572de
Avoid nonstandard use of names for types in wit-0.3.0-draft
bakkot 59ac07e
Apply suggestions from code review
bakkot b340dbe
add new types.wit package to hold `duration`
bakkot dd44eb0
Change docs to refer to "currently configured time zone"
ptomato cb6a02f
Remove "in-daylight-saving-time" flag
ptomato 60f0404
Rename "timezone-display::name" to "id"
ptomato f8e73b8
Measure UTC offset in nanoseconds
ptomato 367ec46
Refactor timezone-display into separate methods
ptomato 0c3e042
Move Unix Time docs to instant type
ptomato 1d63a76
Make time zone methods return optional types
ptomato 42bfd8c
Add timezone::to-debug-string method
ptomato e183e89
Remove system-clock::duration
ptomato File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,35 +9,33 @@ package wasi:[email protected]; | |
/// successive reads of the clock will produce non-decreasing values. | ||
@since(version = 0.3.0-rc-2025-08-15) | ||
interface monotonic-clock { | ||
/// An instant in time, in nanoseconds. An instant is relative to an | ||
use types.{duration}; | ||
|
||
/// A mark on a monotonic clock is a number of nanoseconds since an | ||
/// unspecified initial value, and can only be compared to instances from | ||
/// the same monotonic-clock. | ||
@since(version = 0.3.0-rc-2025-08-15) | ||
type instant = u64; | ||
|
||
/// A duration of time, in nanoseconds. | ||
@since(version = 0.3.0-rc-2025-08-15) | ||
type duration = u64; | ||
type mark = u64; | ||
|
||
/// Read the current value of the clock. | ||
/// | ||
/// The clock is monotonic, therefore calling this function repeatedly will | ||
/// produce a sequence of non-decreasing values. | ||
@since(version = 0.3.0-rc-2025-08-15) | ||
now: func() -> instant; | ||
now: func() -> mark; | ||
|
||
/// Query the resolution of the clock. Returns the duration of time | ||
/// corresponding to a clock tick. | ||
@since(version = 0.3.0-rc-2025-08-15) | ||
get-resolution: func() -> duration; | ||
|
||
/// Wait until the specified instant has occurred. | ||
/// Wait until the specified mark has occurred. | ||
@since(version = 0.3.0-rc-2025-08-15) | ||
wait-until: async func( | ||
when: instant, | ||
when: mark, | ||
); | ||
|
||
/// Wait for the specified duration to elapse. | ||
/// Wait for the specified duration. | ||
@since(version = 0.3.0-rc-2025-08-15) | ||
wait-for: async func( | ||
how-long: duration, | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package wasi:[email protected]; | ||
/// WASI System Clock is a clock API intended to let users query the current | ||
/// time. The clock is not necessarily monotonic as it may be reset. | ||
/// | ||
/// It is intended to be portable at least between Unix-family platforms and | ||
/// Windows. | ||
/// | ||
/// External references may be reset, so this clock is not necessarily | ||
/// monotonic, making it unsuitable for measuring elapsed time. | ||
/// | ||
/// It is intended for reporting the current date and time for humans. | ||
@since(version = 0.3.0-rc-2025-08-15) | ||
interface system-clock { | ||
use types.{duration}; | ||
|
||
/// An "instant", or "exact time", is a point in time without regard to any | ||
/// time zone: just the time since a particular external reference point, | ||
/// often called an "epoch". | ||
/// | ||
/// Here, the epoch is 1970-01-01T00:00:00Z, also known as | ||
/// [POSIX's Seconds Since the Epoch], also known as [Unix Time]. | ||
/// | ||
/// Note that even if the seconds field is negative, incrementing | ||
/// nanoseconds always represents moving forwards in time. | ||
/// For example, `{ -1 seconds, 999999999 nanoseconds }` represents the | ||
/// instant one nanosecond before the epoch. | ||
/// For more on various different ways to represent time, see | ||
/// https://tc39.es/proposal-temporal/docs/timezone.html | ||
/// | ||
/// [POSIX's Seconds Since the Epoch]: https://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_xbd_chap04.html#tag_21_04_16 | ||
/// [Unix Time]: https://en.wikipedia.org/wiki/Unix_time | ||
@since(version = 0.3.0-rc-2025-08-15) | ||
record instant { | ||
seconds: s64, | ||
nanoseconds: u32, | ||
} | ||
|
||
/// Read the current value of the clock. | ||
/// | ||
/// This clock is not monotonic, therefore calling this function repeatedly | ||
/// will not necessarily produce a sequence of non-decreasing values. | ||
/// | ||
/// The nanoseconds field of the output is always less than 1000000000. | ||
@since(version = 0.3.0-rc-2025-08-15) | ||
now: func() -> instant; | ||
|
||
/// Query the resolution of the clock. Returns the smallest duration of time | ||
/// that the implementation permits distinguishing. | ||
@since(version = 0.3.0-rc-2025-08-15) | ||
get-resolution: func() -> duration; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,53 +3,44 @@ package wasi:[email protected]; | |
@unstable(feature = clocks-timezone) | ||
interface timezone { | ||
@unstable(feature = clocks-timezone) | ||
use wall-clock.{datetime}; | ||
use system-clock.{instant}; | ||
|
||
/// Return information needed to display the given `datetime`. This includes | ||
/// the UTC offset, the time zone name, and a flag indicating whether | ||
/// daylight saving time is active. | ||
/// Return the IANA identifier of the currently configured timezone. This | ||
/// should be an identifier from the IANA Time Zone Database. | ||
/// | ||
/// If the timezone cannot be determined for the given `datetime`, return a | ||
/// `timezone-display` for `UTC` with a `utc-offset` of 0 and no daylight | ||
/// saving time. | ||
/// For displaying to a user, the identifier should be converted into a | ||
/// localized name by means of an internationalization API. | ||
/// | ||
/// If the implementation does not expose an actual timezone, or is unable | ||
/// to provide mappings from times to deltas between the configured timezone | ||
/// and UTC, or determining the current timezone fails, or the timezone does | ||
/// not have an IANA identifier, this returns nothing. | ||
@unstable(feature = clocks-timezone) | ||
display: func(when: datetime) -> timezone-display; | ||
iana-id: func() -> option<string>; | ||
|
||
/// The same as `display`, but only return the UTC offset. | ||
/// The number of nanoseconds difference between UTC time and the local | ||
/// time of the currently configured timezone, at the exact time of | ||
/// `instant`. | ||
/// | ||
/// The magnitude of the returned value will always be less than | ||
/// 86,400,000,000,000 which is the number of nanoseconds in a day | ||
/// (24*60*60*1e9). | ||
/// | ||
/// If the implementation does not expose an actual timezone, or is unable | ||
/// to provide mappings from times to deltas between the configured timezone | ||
/// and UTC, or determining the current timezone fails, this returns | ||
/// nothing. | ||
@unstable(feature = clocks-timezone) | ||
utc-offset: func(when: datetime) -> s32; | ||
utc-offset: func(when: instant) -> option<s64>; | ||
|
||
/// Information useful for displaying the timezone of a specific `datetime`. | ||
/// Returns a string that is suitable to assist humans in debugging whether | ||
/// any timezone is available, and if so, which. This may be the same string | ||
/// as `iana-id`, or a formatted representation of the UTC offset such as | ||
/// `-04:00`, or something else. | ||
/// | ||
/// This information may vary within a single `timezone` to reflect daylight | ||
/// saving time adjustments. | ||
/// WARNING: The returned string should not be consumed mechanically! It may | ||
/// change across platforms, hosts, or other implementation details. Parsing | ||
/// this string is a major platform-compatibility hazard. | ||
@unstable(feature = clocks-timezone) | ||
record timezone-display { | ||
/// The number of seconds difference between UTC time and the local | ||
/// time of the timezone. | ||
/// | ||
/// The returned value will always be less than 86400 which is the | ||
/// number of seconds in a day (24*60*60). | ||
/// | ||
/// In implementations that do not expose an actual time zone, this | ||
/// should return 0. | ||
utc-offset: s32, | ||
|
||
/// The abbreviated name of the timezone to display to a user. The name | ||
/// `UTC` indicates Coordinated Universal Time. Otherwise, this should | ||
/// reference local standards for the name of the time zone. | ||
/// | ||
/// In implementations that do not expose an actual time zone, this | ||
/// should be the string `UTC`. | ||
/// | ||
/// In time zones that do not have an applicable name, a formatted | ||
/// representation of the UTC offset may be returned, such as `-04:00`. | ||
name: string, | ||
|
||
/// Whether daylight saving time is active. | ||
/// | ||
/// In implementations that do not expose an actual time zone, this | ||
/// should return false. | ||
in-daylight-saving-time: bool, | ||
} | ||
to-debug-string: func() -> string; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package wasi:clocks@0.3.0-rc-2025-08-15; | ||
/// This interface common types used throughout wasi:clocks. | ||
@since(version = 0.3.0-rc-2025-08-15) | ||
interface types { | ||
/// A duration of time, in nanoseconds. | ||
@since(version = 0.3.0-rc-2025-08-15) | ||
type duration = u64; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd be moderately inclined to leave this as seconds. There is some appeal to being consistent with
duration
but I think it makes more sense to matchinstant.seconds
here.From what I've been reading there shouldn't be any need for sub-second offsets.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd encourage sticking with nanoseconds here — we found a number of cases in the tzdata where there were sub-second offsets. See Justin's original recommendation at #61 (comment)
If the concern is that it would be easier to just do
instant.seconds - offset
or whatever to get the local wall-clock time, then I'd suggest instead adding a method that does that, because you probably want wall-clock time to be expressed in Y-M-D-H-M-S and not epoch nanoseconds...