Skip to content

Commit 8678301

Browse files
feat: Intl.DateTimeFormat options
1 parent 4a0ce96 commit 8678301

File tree

5 files changed

+200
-14
lines changed

5 files changed

+200
-14
lines changed

src/intl/Core__Intl__DateTimeFormat.res

Lines changed: 127 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,140 @@
11
type t
22

3+
type dateStyle = [#full | #long | #medium | #short]
4+
type timeStyle = [#full | #long | #medium | #short]
5+
type calendar = [
6+
| #buddhist
7+
| #chinese
8+
| #coptic
9+
| #dangi
10+
| #ethioaa
11+
| #ethiopic
12+
| #gregory
13+
| #hebrew
14+
| #indian
15+
| #islamic
16+
| #"islamic-umalqura"
17+
| #"islamic-tbla"
18+
| #"islamic-civil"
19+
| #"islamic-rgsa"
20+
| #iso8601
21+
| #japanese
22+
| #persian
23+
| #roc
24+
]
25+
type dayPeriod = [#narrow | #short | #long]
26+
type weekday = [#narrow | #short | #long]
27+
type era = [#narrow | #short | #long]
28+
type year = [#numeric | #"2-digit"]
29+
type month = [#numeric | #"2-digit" | #narrow | #short | #long]
30+
type day = [#numeric | #"2-digit"]
31+
type hour = [#numeric | #"2-digit"]
32+
type minute = [#numeric | #"2-digit"]
33+
type second = [#numeric | #"2-digit"]
34+
// Firefox also supports IANA time zone names here
35+
type timeZoneName = [
36+
| #short
37+
| #long
38+
]
39+
// TODO unsupported in node?
40+
// | #shortOffset
41+
// | #shortGeneric
42+
// | #longOffset
43+
// | #longGeneric
44+
45+
type hourCycle = [#h11 | #h12 | #h23 | #h24]
46+
type formatMatcher = [#basic | @as("best fit") #bestFit]
47+
type fractionalSecondDigits = [#0 | #1 | #2 | #3]
48+
49+
type options = {
50+
dateStyle?: dateStyle, // can be used with timeStyle, but not other options
51+
timeStyle?: timeStyle, // can be used with dateStyle, but not other options
52+
calendar?: calendar,
53+
dayPeriod?: dayPeriod, // only has an effect if a 12-hour clock is used
54+
numberingSystem?: Core__Intl__Common.numberingSystem,
55+
localeMatcher?: Core__Intl__Common.localeMatcher,
56+
timeZone?: string,
57+
hour12?: bool,
58+
hourCycle?: hourCycle,
59+
formatMatcher?: formatMatcher,
60+
// date-time components
61+
weekday?: weekday,
62+
era?: era,
63+
year?: year,
64+
month?: month,
65+
day?: day,
66+
hour?: hour,
67+
minute?: minute,
68+
second?: second,
69+
fractionalSecondDigits?: fractionalSecondDigits,
70+
timeZoneName?: timeZoneName,
71+
}
72+
73+
type resolvedOptions = {
74+
dateStyle?: dateStyle,
75+
timeStyle?: timeStyle,
76+
weekday?: weekday,
77+
era?: era,
78+
year?: year,
79+
month?: month,
80+
day?: day,
81+
hour?: hour,
82+
minute?: minute,
83+
second?: second,
84+
fractionalSecondDigits?: fractionalSecondDigits,
85+
timeZoneName?: timeZoneName,
86+
calendar: calendar,
87+
hour12: bool,
88+
hourCycle: hourCycle,
89+
locale: string,
90+
numberingSystem: Core__Intl__Common.numberingSystem,
91+
timeZone: string,
92+
}
93+
94+
type supportedLocalesOptions = {localeMatcher: Core__Intl__Common.localeMatcher}
95+
96+
type dateTimeComponent = [
97+
| #day
98+
| #dayPeriod
99+
| #era
100+
| #fractionalSecond
101+
| #hour
102+
| #literal
103+
| #minute
104+
| #month
105+
| #relatedYear
106+
| #second
107+
| #timeZone
108+
| #weekday
109+
| #year
110+
| #yearName
111+
]
112+
113+
type dateTimePart = {
114+
\"type": dateTimeComponent,
115+
value: string,
116+
}
117+
118+
type dateTimeRangeSource = [#startRange | #shared | #endRange]
119+
type dateTimeRangePart = {
120+
\"type": dateTimeComponent,
121+
value: string,
122+
source: dateTimeRangeSource,
123+
}
124+
3125
@new external make: unit => t = "Intl.DateTimeFormat"
4126
@new external makeWithLocale: string => t = "Intl.DateTimeFormat"
5127
@new external makeWithLocales: array<string> => t = "Intl.DateTimeFormat"
6-
@new external makeWithLocaleAndOptions: (string, {..}) => t = "Intl.DateTimeFormat"
7-
@new external makeWithLocalesAndOptions: (array<string>, {..}) => t = "Intl.DateTimeFormat"
8-
@new external makeWithOptions: (@as(json`undefined`) _, {..}) => t = "Intl.DateTimeFormat"
128+
@new external makeWithLocaleAndOptions: (string, options) => t = "Intl.DateTimeFormat"
129+
@new external makeWithLocalesAndOptions: (array<string>, options) => t = "Intl.DateTimeFormat"
130+
@new external makeWithOptions: (@as(json`undefined`) _, options) => t = "Intl.DateTimeFormat"
9131

10132
@val external supportedLocalesOf: array<string> => t = "Intl.DateTimeFormat.supportedLocalesOf"
11133
@val
12-
external supportedLocalesOfWithOptions: (array<string>, {..}) => t =
134+
external supportedLocalesOfWithOptions: (array<string>, supportedLocalesOptions) => t =
13135
"Intl.DateTimeFormat.supportedLocalesOf"
14136

15-
@send external resolvedOptions: t => {..} = "resolvedOptions"
137+
@send external resolvedOptions: t => resolvedOptions = "resolvedOptions"
16138

17139
@send external format: (t, Core__Date.t) => string = "format"
18140
@send

test/Intl__DateTimeFormatTest.mjs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Generated by ReScript, PLEASE EDIT WITH CARE
2+
3+
4+
var formatter = new Intl.DateTimeFormat(undefined, {
5+
dateStyle: "full",
6+
timeStyle: "full"
7+
});
8+
9+
console.log(formatter.format(new Date(Date.now())));
10+
11+
console.log(formatter.formatRange(new Date(2023, 1, 1), new Date(2023, 12, 31)));
12+
13+
var formatter$1 = new Intl.DateTimeFormat(undefined, {
14+
timeZone: "UTC",
15+
hour12: false,
16+
hourCycle: "h24",
17+
weekday: "narrow",
18+
era: "narrow",
19+
year: "2-digit",
20+
month: "2-digit",
21+
day: "2-digit",
22+
hour: "2-digit",
23+
minute: "2-digit",
24+
second: "2-digit",
25+
fractionalSecondDigits: 3,
26+
timeZoneName: "short"
27+
});
28+
29+
console.log(formatter$1.format(new Date(Date.now())));
30+
31+
var resolvedOptions = new Intl.DateTimeFormat().resolvedOptions();
32+
33+
var timeZone = resolvedOptions.timeZone;
34+
35+
export {
36+
formatter$1 as formatter,
37+
resolvedOptions ,
38+
timeZone ,
39+
}
40+
/* formatter Not a pure module */

test/Intl__DateTimeFormatTest.res

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
open RescriptCore
2+
3+
let formatter = Intl.DateTimeFormat.makeWithOptions({dateStyle: #full, timeStyle: #full})
4+
5+
Console.log(formatter->Intl.DateTimeFormat.format(Date.fromTime(Date.now())))
6+
7+
Console.log(
8+
formatter->Intl.DateTimeFormat.formatRange(
9+
~startDate=Date.makeWithYMD(~year=2023, ~date=1, ~month=1),
10+
~endDate=Date.makeWithYMD(~year=2023, ~date=31, ~month=12),
11+
),
12+
)
13+
14+
let formatter = Intl.DateTimeFormat.makeWithOptions({
15+
hour12: false,
16+
hourCycle: #h24,
17+
timeZone: "UTC",
18+
weekday: #narrow,
19+
era: #narrow,
20+
year: #"2-digit",
21+
month: #"2-digit",
22+
day: #"2-digit",
23+
hour: #"2-digit",
24+
minute: #"2-digit",
25+
second: #"2-digit",
26+
fractionalSecondDigits: #3,
27+
timeZoneName: #short,
28+
})
29+
30+
Console.log(formatter->Intl.DateTimeFormat.format(Date.fromTime(Date.now())))
31+
32+
let resolvedOptions = Intl.DateTimeFormat.make()->Intl.DateTimeFormat.resolvedOptions
33+
let {Intl.DateTimeFormat.timeZone: timeZone, _} = resolvedOptions

test/TempTests.mjs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -288,10 +288,6 @@ if (globalThis.hello !== undefined) {
288288
console.log("hello");
289289
}
290290

291-
var resolvedOptions = new Intl.DateTimeFormat().resolvedOptions();
292-
293-
var timeZone = resolvedOptions.timeZone;
294-
295291
var z = 1.2 % 1.4;
296292

297293
var intFromBigInt = Core__BigInt.toInt(BigInt("10000000000"));
@@ -331,8 +327,6 @@ export {
331327
x ,
332328
array$1 as array,
333329
timeout ,
334-
resolvedOptions ,
335-
timeZone ,
336330
z ,
337331
intFromBigInt ,
338332
Bugfix ,

test/TempTests.res

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,6 @@ if globalThis["hello"] !== undefined {
180180
Console.log("hello")
181181
}
182182

183-
let resolvedOptions = Intl.DateTimeFormat.make()->Intl.DateTimeFormat.resolvedOptions
184-
let timeZone = resolvedOptions["timeZone"]
185-
186183
let z = Float.mod(1.2, 1.4)
187184

188185
let intFromBigInt = BigInt.fromString("10000000000")->BigInt.toInt

0 commit comments

Comments
 (0)