Skip to content
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

Temporal bindings #76

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Webapi.res
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module ReadableStream = Webapi__ReadableStream
module IntersectionObserver = Webapi__IntersectionObserver
module ResizeObserver = Webapi__ResizeObserver
module Url = Webapi__Url
module Temporal = Webapi__Temporal
module WebSocket = Webapi__WebSocket

type rafId
Expand Down
2 changes: 2 additions & 0 deletions src/Webapi/Temporal/Webapi__Temporal__ArithmaticOptions.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
type t
@obj external make: (~overflow: [#constrain | #reject]=?) => t = ""
1 change: 1 addition & 0 deletions src/Webapi/Temporal/Webapi__Temporal__Calendar.res
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type t = Webapi__Temporal__Types.calendar
21 changes: 21 additions & 0 deletions src/Webapi/Temporal/Webapi__Temporal__Difference.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module DifferenceOptions = {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This submodule doesn't seem to add anything? I think it would work just as well without the submodule wrapper.

type t

type durationUnit = [
| #auto
| #hour
| #minute
| #second
| #millisecond
| #microsecond
| #nanosecond
]

@obj
external make: (
~largestUnit: durationUnit=?,
~smallestUnit: durationUnit=?,
~roundingIncrement: int=?,
~roundingMode: [#halfExpand | #ceil | #trunc | #floor]=?,
) => t = ""
}
1 change: 1 addition & 0 deletions src/Webapi/Temporal/Webapi__Temporal__Duration.res
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type t = Webapi__Temporal__Types.duration
90 changes: 90 additions & 0 deletions src/Webapi/Temporal/Webapi__Temporal__Instant.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
type t = Webapi__Temporal__Types.instant

// @new external make: BigInt.t => t = "Temporal.Instant"
@scope("Temporal.Instant") @val external fromString: string => t = "from"
@scope("Temporal.Instant") @val external fromInstant: t => t = "from"
@scope("Temporal.Instant") @val external fromEpochSeconds: float => t = "fromEpochSeconds"
@scope("Temporal.Instant") @val
external fromEpochMilliseconds: float => t = "fromEpochMilliseconds"
// @scope("Temporal.Instant") @val external fromEpochMicroseconds: BigInt.t => t = "fromEpochMicroseconds"
// @scope("Temporal.Instant") @val external fromEpochNanoseconds: BigInt.t => t = "fromEpochNanoseconds"
@scope("Temporal.Instant") @val external compare: (t, t) => int = "compare"
@scope("Temporal.Instant") @val external compareStrings: (string, string) => int = "compare"

@get external epochSeconds: t => float = "epochSeconds"
@get external epochMilliseconds: t => float = "epochMilliseconds"
// @get external epochMicroseconds: t => BigInt.t = "epochMicroseconds"
// @get external epochNanoseconds: t => BigInt.t = "epochNanoseconds"
@send
external toZonedDateTimeISO: (
t,
Webapi__Temporal__Types.timeZone,
) => Webapi__Temporal__ZonedDateTime.t = "toZonedDateTimeISO"
@send
external toZonedDateTime: (
t,
{"timeZone": Webapi__Temporal__Types.timeZone, "calendar": Webapi__Temporal__Types.calendar},
) => Webapi__Temporal__ZonedDateTime.t = "toZonedDateTime"
@send external add: (t, Webapi__Temporal__Types.duration) => t = "add"
@send external subtract: (t, Webapi__Temporal__Types.duration) => t = "subtract"

@send external until: (t, t) => Webapi__Temporal__Types.duration = "until"
@send
external untilWithOptions: (
t,
t,
Webapi__Temporal__Difference.DifferenceOptions.t,
) => Webapi__Temporal__Types.duration = "until"
@send external since: (t, t) => Webapi__Temporal__Types.duration = "since"
@send
external sinceWithOptions: (
t,
t,
Webapi__Temporal__Difference.DifferenceOptions.t,
) => Webapi__Temporal__Types.duration = "since"

type roundTo = [
| #hour
| #minute
| #second
| #millisecond
| #microsecond
| #nanosecond
]

module RoundOptions = {
type t

@obj
external make: (
~smallestUnit: roundTo,
~roundingIncrement: int=?,
~roundingMode: [#halfExpand | #ceil | #trunc | #floor]=?,
) => t = ""
}

@send external round: (t, ~to: roundTo) => t = "round"
@send external roundWithOptions: (t, RoundOptions.t) => t = "round"
@send external equals: (t, t) => bool = "equals"

module ToStringOptions = {
type t

@obj
external make: (
~timeZone: Webapi__Temporal__Types.timeZone=?,
~fractionalSecondDigits: [#auto | #0 | #1 | #2 | #3 | #4 | #5 | #6 | #7 | #8 | #9]=?,
~smallestUnit: [
| #minute
| #second
| #millisecond
| #microsecond
| #nanosecond
]=?,
~roundingMode: [#halfExpand | #ceil | #trunc | #floor]=?,
) => t = ""
}

@send external toString: t => string = "toString"
@send external toStringWithOptions: (t, ToStringOptions.t) => string = "toString"
// TODO: toLocaleString, need Intl bindings
49 changes: 49 additions & 0 deletions src/Webapi/Temporal/Webapi__Temporal__Now.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
@scope("Temporal.Now") @val
external zonedDateTimeISO: unit => Webapi__Temporal__Types.zonedDateTime = "zonedDateTimeISO"
@scope("Temporal.Now") @val
external zonedDateTimeISOWithTimeZone: Webapi__Temporal__Types.timeZone => Webapi__Temporal__Types.zonedDateTime =
"zonedDateTimeISO"
@scope("Temporal.Now") @val
external zonedDateTime: Webapi__Temporal__Types.calendar => Webapi__Temporal__Types.zonedDateTime =
"zonedDateTime"
@scope("Temporal.Now") @val
external zonedDateTimeWithTimeZone: (
Webapi__Temporal__Types.calendar,
Webapi__Temporal__Types.timeZone,
) => Webapi__Temporal__Types.zonedDateTime = "zonedDateTimeWithTimeZone"
@scope("Temporal.Now") @val external instant: unit => Webapi__Temporal__Types.instant = "instant"
@scope("Temporal.Now") @val external timeZone: unit => Webapi__Temporal__Types.timeZone = "timeZone"

@scope("Temporal.Now") @val
external plainDateTimeISO: unit => Webapi__Temporal__Types.plainDateTime = "plainDateTimeISO"
@scope("Temporal.Now") @val
external plainDateTimeISOWithTimeZone: Webapi__Temporal__Types.timeZone => Webapi__Temporal__Types.plainDateTime =
"plainDateTimeISO"
@scope("Temporal.Now") @val
external plainDateTime: Webapi__Temporal__Types.calendar => Webapi__Temporal__Types.plainDateTime =
"plainDateTime"
@scope("Temporal.Now") @val
external plainDateTimeWithTimeZone: (
Webapi__Temporal__Types.calendar,
Webapi__Temporal__Types.timeZone,
) => Webapi__Temporal__Types.plainDateTime = "plainDateTime"

@scope("Temporal.Now") @val
external plainDateISO: unit => Webapi__Temporal__Types.plainDate = "plainDateISO"
@scope("Temporal.Now") @val
external plainDateISOWithTimeZone: Webapi__Temporal__Types.timeZone => Webapi__Temporal__Types.plainDate =
"plainDateISO"
@scope("Temporal.Now") @val
external plainDate: Webapi__Temporal__Types.calendar => Webapi__Temporal__Types.plainDate =
"plainDate"
@scope("Temporal.Now") @val
external plainDateWithTimeZone: (
Webapi__Temporal__Types.calendar,
Webapi__Temporal__Types.timeZone,
) => Webapi__Temporal__Types.plainDate = "plainDate"

@scope("Temporal.Now") @val
external plainTimeISO: unit => Webapi__Temporal__Types.plainTime = "plainTimeISO"
@scope("Temporal.Now") @val
external plainTimeISOWithTimeZone: Webapi__Temporal__Types.timeZone => Webapi__Temporal__Types.plainTime =
"plainTimeISO"
120 changes: 120 additions & 0 deletions src/Webapi/Temporal/Webapi__Temporal__PlainDate.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
type t = Webapi__Temporal__Types.plainDate

@new
external make: (
~isoYear: int,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if these (as well as the params for PlainTime.make) should be prefixed with iso, what do you think?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The from methods don't prefix the keys with iso. So I think even though the spec describes them as isoYear etc we should stay consistent and not prefix the parameters.

~isoMonth: int,
~isoDay: int,
~calendar: Webapi__Temporal__Types.calendar=?,
) => t = "Temporal.PlainDate"

module PlainDateInit = {
type t

// TODO: Review type safety. Not all of these options are valid together when passed to fromInit
@obj
external make: (
~year: int=?,
~month: int=?,
~day: int=?,
~era: string=?,
~eraYear: int=?,
~monthCode: string=?,
) => t = ""
}

@scope("Temporal.PlainDate") @val external fromString: string => t = "from"
@scope("Temporal.PlainDate") @val
external fromStringWithOptions: (string, {"overflow": [#constrain | #reject]}) => t = "from"
@scope("Temporal.PlainDate") @val external fromInit: PlainDateInit.t => t = "from"
@scope("Temporal.PlainDate") @val
external fromInitWithOptions: (PlainDateInit.t, {"overflow": [#constrain | #reject]}) => t = "from"
@scope("Temporal.PlainDate") @val external compare: (t, t) => int = "compare"
@get external year: t => int = "year"
@get external month: t => int = "month"
@get external day: t => int = "day"
@get external monthCode: t => int = "monthCode"
@get external calendar: t => Webapi__Temporal__Types.calendar = "calendar"
@get external timeZone: t => Webapi__Temporal__Types.timeZone = "timeZone"
@get external era: t => option<string> = "era"
@get external eraYear: t => option<float> = "eraYear"
@get external dayOfWeek: t => int = "dayOfWeek"
@get external dayOfYear: t => int = "dayOfYear"
@get external weekOfYear: t => int = "weekOfYear"
@get external daysInWeek: t => int = "daysInWeek"
@get external daysInMonth: t => int = "daysInMonth"
@get external daysInYear: t => int = "daysInYear"
@get external monthsInYear: t => int = "monthsInYear"
@get external inLeapYear: t => bool = "inLeapYear"
// TODO: add `with` binding
@send external withCalendar: (t, Webapi__Temporal__Types.calendar) => t = "withCalendar"
@send external add: (t, Webapi__Temporal__Types.duration) => t = "add"
@send
external addWithOptions: (
t,
Webapi__Temporal__Types.duration,
Webapi__Temporal__ArithmaticOptions.t,
) => t = "add"
@send external subtract: (t, Webapi__Temporal__Types.duration) => t = "subtract"
@send
external subtractWithOptions: (
t,
Webapi__Temporal__Types.duration,
Webapi__Temporal__ArithmaticOptions.t,
) => t = "subtract"
@send external until: (t, t) => Webapi__Temporal__Types.duration = "until"
@send
external untilWithOptions: (
t,
t,
// TODO: Need a different type as the valid units don't include time here
Webapi__Temporal__Difference.DifferenceOptions.t,
) => Webapi__Temporal__Types.duration = "until"
@send external since: (t, t) => Webapi__Temporal__Types.duration = "since"
@send
external sinceWithOptions: (
t,
t,
Webapi__Temporal__Difference.DifferenceOptions.t,
) => Webapi__Temporal__Types.duration = "since"
@send external equals: (t, t) => bool = "equals"

module ToStringOptions = {
type t

@obj
external make: (~calendarName: [#auto | #always | #never]=?) => t = ""
}

@send external toString: t => string = "toString"
@send external toStringWithOptions: (t, ToStringOptions.t) => string = "toString"
// TODO: toLocaleString, need Intl bindings

module ToZonedDateTimeOptions = {
type t
@obj
external make: (
~plainTime: Webapi__Temporal__Types.plainTime=?,
~timeZone: Webapi__Temporal__Types.timeZone,
) => t = ""
}

@send
external toZonedDateTime: (t, ToZonedDateTimeOptions.t) => Webapi__Temporal__Types.zonedDateTime =
"toZonedDateTime"
@send external toPlainDateTime: t => Webapi__Temporal__Types.plainDateTime = "toPlainDateTime"
@send
external toPlainDateTimeWithPlainTime: (
t,
Webapi__Temporal__Types.plainTime,
) => Webapi__Temporal__Types.plainDateTime = "toPlainDateTime"
@send external toPlainYearMonth: t => Webapi__Temporal__Types.plainYearMonth = "toPlainYearMonth"
@send external toPlainMonthDay: t => Webapi__Temporal__Types.plainMonthDay = "toPlainMonthDay"

type isoFields = {
isoYear: int,
isoMonth: int,
isoDay: int,
calendar: Webapi__Temporal__Types.calendar,
}
@send external getISOFields: t => isoFields = "getISOFields"
27 changes: 27 additions & 0 deletions src/Webapi/Temporal/Webapi__Temporal__PlainTime.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
type t = Webapi__Temporal__Types.plainTime

@new
external make: (
~isoHour: int=?,
~isoMinute: int=?,
~isoSecond: int=?,
~isoMillisecond: int=?,
~isoMicrosecond: int=?,
~isoNanosecond: int=?,
) => t = "Temporal.PlainTime"

module PlainTimeInit = {
type t
@obj
external make: (
~hour: int=?,
~minute: int=?,
~second: int=?,
~millisecond: int=?,
~microsecond: int=?,
~nanosecond: int=?,
~calendar: Webapi__Temporal__Types.calendar=?,
) => t = ""
}

@scope("Temporal.PlainTime") @val external fromString: string => t = "from"
1 change: 1 addition & 0 deletions src/Webapi/Temporal/Webapi__Temporal__TimeZone.res
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type t = Webapi__Temporal__Types.timeZone
10 changes: 10 additions & 0 deletions src/Webapi/Temporal/Webapi__Temporal__Types.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
type calendar
type duration
type instant
type plainDate
type plainDateTime
type plainTime
type plainYearMonth
type plainMonthDay
type timeZone
type zonedDateTime
Loading