RFC: Temporal API bindings
Les mainteneurs répondent en général sous 1 jour
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 5/5
- Temps estimé
- Plus d'une semaine
- Accessibilité débutants
- 12/100
- Type d'issue
- Fonctionnalité
- Clarté
- Plutôt claire
- Activité
- Active
- Stack technique
- javascript, typescript
- Domaine
- developer-experience
Piste de recherche
This is an RFC, not a task: the author already has ~270 bindings in 11 files and is asking for a direction decision on three points (stdlib placement, the == change, CI on Node.js 22). Start by reading how Intl is laid out in the stdlib (Stdlib_Intl.res and Stdlib_Intl_Common.res) since the proposal mirrors it with Stdlib_Temporal.res and Stdlib_Temporal_Common.res, and look at the Date equality fix in #3455 before judging the == change. Done means a maintainer answers the three questions and the Node.js 26 test-step question is settled, not a patch.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
Summary
Add bindings to JavaScript's Temporal API to the standard library as Temporal. There is one module per Temporal type, plus Temporal.Now and Date.toTemporalInstant. The bindings are externals that compile to direct calls on the global Temporal. They need a runtime that provides it, such as Node.js 26, or a polyfill that defines globalThis.Temporal.
let meeting = Temporal.ZonedDateTime.fromStringOrThrow(
"2024-03-09T09:30:00-05:00[America/New_York]",
)
let nextMeeting = meeting->Temporal.ZonedDateTime.addFields({days: 1.}) // JS: meeting.add({ days: 1 })
nextMeeting->Temporal.ZonedDateTime.toString
// "2024-03-10T09:30:00-04:00[America/New_York]"
// The day daylight saving time starts is 23 hours long
meeting->Temporal.ZonedDateTime.until(nextMeeting)->Temporal.Duration.toString
// "PT23H"
I have a working implementation: around 270 bindings in 11 files, with 126 doc examples. I can push it to a branch of my fork if seeing the code helps. Before finishing it and opening a PR, I'd like feedback on the direction, especially on these three points:
- adding Temporal to the standard library;
- the change to
==; - testing while CI runs on Node.js 22.
Motivation
- Temporal replaces
Date. It covers exact times, wall-clock dates and times, time zones, calendars and durations. Node.js 26 ships it without a flag. - The standard library binds the ECMAScript built-ins, like
Date,IntlandBigInt. Temporal is one of them. - Its signatures are heavily overloaded. Hand-written externals are hard to get right, and each project would type them differently.
- No ReScript bindings for Temporal exist on npm.
Design
Modules
| Module | Represents |
|---|---|
Temporal.Instant |
An exact time. |
Temporal.ZonedDateTime |
An exact time in a time zone and calendar. |
Temporal.PlainDateTime, PlainDate, PlainTime, PlainYearMonth, PlainMonthDay |
Wall-clock values without a time zone. |
Temporal.Duration |
A duration. |
Temporal.Now |
Reads the current time. |
Temporal.Common |
Types shared by the modules. |
Each module is a file, like Stdlib_Temporal_PlainDate.res, aliased from Stdlib_Temporal.res, as with Intl. Each module has an abstract type t and data-first functions.
Shared types
Temporal types convert into each other in both directions, like PlainDate.toPlainDateTime and PlainDateTime.toPlainDate. If each file defined its own type, the files would form a dependency cycle. So the value types are defined once, in Temporal.Common, and each module re-exports its own, as Intl.Common does for Intl:
// Stdlib_Temporal_Common.res
@notUndefined @editor.completeFrom(Stdlib.Temporal.PlainDate)
type plainDate
// Stdlib_Temporal_PlainDate.res
type t = Stdlib_Temporal_Common.plainDate
@editor.completeFrom keeps pipe completion working on a Common.plainDate.
Splitting JavaScript overloads
A ReScript function has one type, so each JavaScript overload is bound as its own function. The functions for one overloaded method compile to the same JavaScript call:
| JavaScript | ReScript |
|---|---|
new Temporal.PlainDate(2024, 2, 29) |
PlainDate.make(~year=2024, ~month=2, ~day=29) |
Temporal.PlainDate.from("2024-02-29") |
PlainDate.fromStringOrThrow("2024-02-29"), or fromString, which returns an option |
Temporal.PlainDate.from({year: 2024, month: 2, day: 29}) |
PlainDate.fromFields({year: 2024, month: 2, day: 29}) |
date.add(duration) |
date->PlainDate.add(duration) |
date.add({days: 1}) |
date->PlainDate.addFields({days: 1.}) |
Temporal.Duration.compare(a, b, {relativeTo}) |
Duration.compareWithOptions(a, b, {relativeTo: …}) |
All modules follow this pattern. A gap means JavaScript has no such function:
| Module | make |
fromStringOrThrow, fromString |
fromFields |
add, subtract |
addFields, subtractFields |
compare |
|---|---|---|---|---|---|---|
Duration |
✓ | ✓ | ✓ | ✓ | ✓ | ✓, and compareWithOptions |
Instant |
fromEpochNanoseconds, fromEpochMilliseconds |
✓ | — | ✓ | ✓ | ✓ |
PlainDate |
✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
PlainTime |
✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
PlainDateTime |
✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
PlainYearMonth |
✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
PlainMonthDay |
✓ | ✓ | ✓ | — | — | — |
ZonedDateTime |
fromEpochNanoseconds |
✓ | ✓ | ✓ | ✓ | ✓ |
- Options are an optional labeled
~optionsargument. comparereturnsOrdering.t, soArray.toSorted(Temporal.PlainDate.compare)works.
Types
- Calendars are a closed polymorphic variant of the 16 calendars Temporal accepts. They are a subset of
Intl.Common.calendar, so(calendar :> Intl.Common.calendar)works. - Time zones and eras are strings.
- Units are singular polymorphic variants. Each function takes only the units its type supports.
- Numbers. Duration fields are
float, date and time fields areint, andepochNanosecondsisbigint. relativeTois an abstract type, created from aPlainDateor aZonedDateTime.- Nullable results are options.
Errors
The bindings let Temporal's errors through. fromString is the exception: it returns None for a RangeError and rethrows any other error. That way, a missing Temporal global isn't reported as an invalid string.
Equality
ReScript's == compared objects by their own keys. Temporal values have none, so any two of them were equal. Date had the same bug (#3455). Following that fix, == now uses the type's equals, and compares durations field by field.
compare, < and > are unchanged, because Temporal has no single order for them to use. For ordering, each module has its own compare.
Compiler and tooling
- JS globals.
Temporalis added to the JS globals, so a nested module namedTemporalis emitted as$$Temporal, as withIntl. - GenType maps the types to TypeScript's
Temporal.*types from theesnext.temporallib.
Breaking changes
- Shadowing. The stdlib
Temporalmodule shadows a project's own top-levelTemporalmodule, such as bindings to the Temporal.io SDK. - Export name. A nested module named
Temporalis exported to JavaScript as$$Temporal.
Testing
- CI on Node.js 22. Node.js 22 doesn't have Temporal, so the Temporal tests in the stdlib suite don't run there. The doc examples are still type checked: doc examples that need a newer Node.js are now emitted as
test.skipinstead of being dropped. - Node.js 26. Locally, all 871 doc examples and the stdlib suite pass.
Not included
Duration.toLocaleStringoptions, which areIntl.DurationFormatoptions.largestUnit: "auto", andrelativeToas aPlainDateTimeor a string.- String arguments to
add,subtractandcompare,fromwith another Temporal value, and plural unit names. Intl.DateTimeFormat.formatwith Temporal values.valueOf, which always throws.- A CI step that runs the tests on Node.js 26. This could be a separate PR.
- Langage dominant
- OCaml
- Étoiles
- 7.5k
- Forks
- 483
- Merge moyen
- 20 h
- PR mergées (30 j)
- 219
Préparer son environnement
Lance le conteneur de développement du projet dans votre navigateur, avec votre propre compte GitHub.
- Aucun Dockerfile ni fichier Docker Compose
- Propose un modèle de pull request
- Lire le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de rescript-lang/rescript
-
Difficulté 2/5 1-3 heures Accessibilité débutants 75/100
rescript-lang/rescript#9037 ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 5/5 Plus d'une semaine Accessibilité débutants 35/100
rescript-lang/rescript#9006 · 1 réaction ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 3/5 1-2 jours Accessibilité débutants 70/100
rescript-lang/rescript#8997 ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 3/5 1-2 jours Accessibilité débutants 68/100
rescript-lang/rescript#8994 ·
Les mainteneurs répondent en général sous 1 jour
-
Have formatter insert newlines around multi-line expressionsPeut-être pris @rolandpeelen l’a pris il y a 4 jours. Ouverte
Difficulté 4/5 3-5 jours Accessibilité débutants 25/100
rescript-lang/rescript#8960 ·
Les mainteneurs répondent en général sous 1 jour
Toutes les issues de rescript-lang/rescript
Issues similaires
-
Difficulté 1/5 Moins d'une heure Accessibilité débutants 76/100
Les mainteneurs répondent en général sous 1 jour
-
lesson-submission pending-review
Difficulté 1/5 Moins d'une heure Accessibilité débutants 74/100
Ikalus1988/MisakaNet#3899 ·
Les mainteneurs répondent en général sous 1 jour
-
follow-up
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
Les mainteneurs répondent en général sous 1 jour
-
[BUG] jackson3 JacksonJsonpMapper(ObjectMapper) fails to deserialize `_source` with "Trailing token" errorPeut-être pris @junyeokk l’a pris aujourd’hui. Ouvertebug untriaged
Difficulté 2/5 1-3 heures Accessibilité débutants 82/100
opensearch-project/opensearch-java#2236 ·
Les mainteneurs répondent en général sous 1 jour
-
用户目录有单引号时,热加载时YAML解析崩溃Ouvertebug
Difficulté 2/5 1-3 heures Accessibilité débutants 68/100
dsh-market/dsh-market#1027 · 1 commentaire ·
Les mainteneurs répondent en général sous 1 jour