Skip to content

Commit 4f2e82b

Browse files
authored
Add locale and formatting options to localeDateString, localeString and localTimeString functions (#30)
* Add locale and formatting options to localeDateString, localeString and localTimeString functions * Add doc strings for date to locale string functions * Update changelog; Add return values of code examples
1 parent 9517829 commit 4f2e82b

File tree

3 files changed

+337
-0
lines changed

3 files changed

+337
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- Add `Array.at` binding for returning an array item by its index. https://github.com/rescript-association/rescript-core/pull/48
1212
- Fixed type signatures of `Array.fromArrayLikeWithMap` and `Array.fromIteratorWithMap`. https://github.com/rescript-association/rescript-core/pull/50
1313
- Remove internal async/await helpers that do not need to be exposed in `Core`.
14+
- Add locale and formatting options to `localeDateString`, `localeString` and `localTimeString` functions https://github.com/rescript-association/rescript-core/pull/30
1415

1516
### Documentation
1617

src/Core__Date.res

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,20 @@ type t = Js.Date.t
22

33
type time = float
44

5+
type localeOptions = {
6+
dateStyle?: [#full | #long | #medium | #short],
7+
timeStyle?: [#full | #long | #medium | #short],
8+
weekday?: [#long | #short | #narrow],
9+
era?: [#long | #short | #narrow],
10+
year?: [#numeric | #"2-digit"],
11+
month?: [#numeric | #"2-digit" | #long | #short | #narrow],
12+
day?: [#numeric | #"2-digit"],
13+
hour?: [#numeric | #"2-digit"],
14+
minute?: [#numeric | #"2-digit"],
15+
second?: [#numeric | #"2-digit"],
16+
timeZoneName?: [#long | #short],
17+
}
18+
519
@send external valueOf: t => time = "valueOf"
620

721
@new external make: unit => t = "Date"
@@ -146,8 +160,19 @@ external setUTCMinutesSMs: (t, ~minutes: int, ~seconds: int, ~milliseconds: int)
146160
@send external toTimeString: t => string = "toTimeString"
147161

148162
@send external toLocaleDateString: t => string = "toLocaleDateString"
163+
@send external toLocaleDateStringWithLocale: (t, string) => string = "toLocaleDateString"
164+
@send
165+
external toLocaleDateStringWithLocaleAndOptions: (t, string, localeOptions) => string =
166+
"toLocaleDateString"
149167
@send external toLocaleString: t => string = "toLocaleString"
168+
@send external toLocaleStringWithLocale: (t, string) => string = "toLocaleString"
169+
@send
170+
external toLocaleStringWithLocaleAndOptions: (t, string, localeOptions) => string = "toLocaleString"
150171
@send external toLocaleTimeString: t => string = "toLocaleTimeString"
172+
@send external toLocaleTimeStringWithLocale: (t, string) => string = "toLocaleTimeString"
173+
@send
174+
external toLocaleTimeStringWithLocaleAndOptions: (t, string, localeOptions) => string =
175+
"toLocaleTimeString"
151176

152177
@send external toISOString: t => string = "toISOString"
153178
@send external toUTCString: t => string = "toUTCString"

src/Core__Date.resi

Lines changed: 311 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,311 @@
1+
/***
2+
Functions for interacting with JavaScript Dates.
3+
*/
4+
5+
type t = Js.Date.t
6+
type time = float
7+
8+
/**
9+
A type representing date time format options.
10+
11+
Note: There are some properties missing:
12+
- fractionalSecondDigits
13+
- dayPeriod
14+
- calendar
15+
- numberingSystem
16+
- localeMatcher
17+
- timeZone
18+
- hour12
19+
- hourCycle
20+
- formatMatcher
21+
22+
See full spec at https://tc39.es/ecma402/#datetimeformat-objects
23+
*/
24+
type localeOptions = {
25+
dateStyle?: [#full | #long | #medium | #short],
26+
timeStyle?: [#full | #long | #medium | #short],
27+
weekday?: [#long | #narrow | #short],
28+
era?: [#long | #narrow | #short],
29+
year?: [#"2-digit" | #numeric],
30+
month?: [#"2-digit" | #long | #narrow | #numeric | #short],
31+
day?: [#"2-digit" | #numeric],
32+
hour?: [#"2-digit" | #numeric],
33+
minute?: [#"2-digit" | #numeric],
34+
second?: [#"2-digit" | #numeric],
35+
timeZoneName?: [#long | #short],
36+
}
37+
38+
@send external valueOf: t => time = "valueOf"
39+
@new external make: unit => t = "Date"
40+
@new external fromString: string => t = "Date"
41+
@new external fromTime: time => t = "Date"
42+
@new external makeWithYM: (~year: int, ~month: int) => t = "Date"
43+
@new external makeWithYMD: (~year: int, ~month: int, ~date: int) => t = "Date"
44+
@new external makeWithYMDH: (~year: int, ~month: int, ~date: int, ~hours: int) => t = "Date"
45+
@new
46+
external makeWithYMDHM: (~year: int, ~month: int, ~date: int, ~hours: int, ~minutes: int) => t =
47+
"Date"
48+
@new
49+
external makeWithYMDHMS: (
50+
~year: int,
51+
~month: int,
52+
~date: int,
53+
~hours: int,
54+
~minutes: int,
55+
~seconds: int,
56+
) => t = "Date"
57+
@new
58+
external makeWithYMDHMSM: (
59+
~year: int,
60+
~month: int,
61+
~date: int,
62+
~hours: int,
63+
~minutes: int,
64+
~seconds: int,
65+
~milliseconds: int,
66+
) => t = "Date"
67+
module UTC: {
68+
@val external makeWithYM: (~year: int, ~month: int) => time = "Date.UTC"
69+
@val external makeWithYMD: (~year: int, ~month: int, ~date: int) => time = "Date.UTC"
70+
@val
71+
external makeWithYMDH: (~year: int, ~month: int, ~date: int, ~hours: int) => time = "Date.UTC"
72+
@val
73+
external makeWithYMDHM: (
74+
~year: int,
75+
~month: int,
76+
~date: int,
77+
~hours: int,
78+
~minutes: int,
79+
) => time = "Date.UTC"
80+
@val
81+
external makeWithYMDHMS: (
82+
~year: int,
83+
~month: int,
84+
~date: int,
85+
~hours: int,
86+
~minutes: int,
87+
~seconds: int,
88+
) => time = "Date.UTC"
89+
@val
90+
external makeWithYMDHMSM: (
91+
~year: int,
92+
~month: int,
93+
~date: int,
94+
~hours: int,
95+
~minutes: int,
96+
~seconds: int,
97+
~milliseconds: int,
98+
) => time = "Date.UTC"
99+
}
100+
@val external now: unit => time = "Date.now"
101+
@send external getTime: t => time = "getTime"
102+
@send external getTimezoneOffset: t => int = "getTimezoneOffset"
103+
@send external getFullYear: t => int = "getFullYear"
104+
@send external getMonth: t => int = "getMonth"
105+
@send external getDate: t => int = "getDate"
106+
@send external getHours: t => int = "getHours"
107+
@send external getMinutes: t => int = "getMinutes"
108+
@send external getSeconds: t => int = "getSeconds"
109+
@send external getMilliseconds: t => int = "getMilliseconds"
110+
@send external getDay: t => int = "getDay"
111+
@send external setFullYear: (t, int) => unit = "setFullYear"
112+
@send external setFullYearM: (t, ~year: int, ~month: int) => unit = "setFullYear"
113+
@send external setFullYearMD: (t, ~year: int, ~month: int, ~date: int) => unit = "setFullYear"
114+
@send external setMonth: (t, int) => unit = "setMonth"
115+
@send external setDate: (t, int) => unit = "setDate"
116+
@send external setHours: (t, int) => unit = "setHours"
117+
@send external setHoursM: (t, ~hours: int, ~minutes: int) => unit = "setHours"
118+
@send external setHoursMS: (t, ~hours: int, ~minutes: int, ~seconds: int) => unit = "setHours"
119+
@send
120+
external setHoursMSMs: (t, ~hours: int, ~minutes: int, ~seconds: int, ~milliseconds: int) => unit =
121+
"setHours"
122+
@send external setMinutes: (t, int) => unit = "setMinutes"
123+
@send external setMinutesS: (t, ~minutes: int, ~seconds: int) => unit = "setMinutes"
124+
@send
125+
external setMinutesSMs: (t, ~minutes: int, ~seconds: int, ~milliseconds: int) => unit = "setMinutes"
126+
@send external setSeconds: (t, int) => unit = "setSeconds"
127+
@send external setSecondsMs: (t, ~seconds: int, ~milliseconds: int) => unit = "setSeconds"
128+
@send external setMilliseconds: (t, int) => unit = "setMilliseconds"
129+
@send external setDay: (t, int) => unit = "setDay"
130+
@send external getUTCFullYear: t => int = "getUTCFullYear"
131+
@send external getUTCMonth: t => int = "getUTCMonth"
132+
@send external getUTCDate: t => int = "getUTCDate"
133+
@send external getUTCHours: t => int = "getUTCHours"
134+
@send external getUTCMinutes: t => int = "getUTCMinutes"
135+
@send external getUTCSeconds: t => int = "getUTCSeconds"
136+
@send external getUTCMilliseconds: t => int = "getUTCMilliseconds"
137+
@send external getUTCDay: t => int = "getUTCDay"
138+
@send external setUTCFullYear: (t, int) => unit = "setUTCFullYear"
139+
@send external setUTCFullYearM: (t, ~year: int, ~month: int) => unit = "setUTCFullYear"
140+
@send
141+
external setUTCFullYearMD: (t, ~year: int, ~month: int, ~date: int) => unit = "setUTCFullYear"
142+
@send external setUTCMonth: (t, int) => unit = "setUTCMonth"
143+
@send external setUTCDate: (t, int) => unit = "setUTCDate"
144+
@send external setUTCHours: (t, int) => unit = "setUTCHours"
145+
@send external setUTCHoursM: (t, ~hours: int, ~minutes: int) => unit = "setUTCHours"
146+
@send
147+
external setUTCHoursMS: (t, ~hours: int, ~minutes: int, ~seconds: int) => unit = "setUTCHours"
148+
@send
149+
external setUTCHoursMSMs: (
150+
t,
151+
~hours: int,
152+
~minutes: int,
153+
~seconds: int,
154+
~milliseconds: int,
155+
) => unit = "setUTCHours"
156+
@send external setUTCMinutes: (t, int) => unit = "setUTCMinutes"
157+
@send external setUTCMinutesS: (t, ~minutes: int, ~seconds: int) => unit = "setUTCMinutes"
158+
@send
159+
external setUTCMinutesSMs: (t, ~minutes: int, ~seconds: int, ~milliseconds: int) => unit =
160+
"setUTCMinutes"
161+
@send external setUTCSeconds: (t, int) => unit = "setUTCSeconds"
162+
@send external setUTCSecondsMs: (t, ~seconds: int, ~milliseconds: int) => unit = "setUTCSeconds"
163+
@send external setUTCMilliseconds: (t, int) => unit = "setUTCMilliseconds"
164+
@send external setUTCDay: (t, int) => unit = "setUTCDay"
165+
@send external toDateString: t => string = "toDateString"
166+
@send external toString: t => string = "toString"
167+
@send external toTimeString: t => string = "toTimeString"
168+
169+
/**
170+
`toLocaleDateString(date)`
171+
172+
Converts a JavaScript date to a localized date string. It will use the current locale.
173+
174+
## Examples
175+
```rescript
176+
Date.make()->Date.toLocaleDateString->Console.log
177+
// 2/19/2023
178+
```
179+
*/
180+
@send
181+
external toLocaleDateString: t => string = "toLocaleDateString"
182+
183+
/**
184+
`toLocaleDateStringWithLocale(date, locale)`
185+
186+
Converts a JavaScript date to a localized date string. It will use the specified locale.
187+
188+
## Examples
189+
```rescript
190+
Date.make()->Date.toLocaleDateStringWithLocale("en-US")->Console.log
191+
// 2/19/2023
192+
```
193+
*/
194+
@send
195+
external toLocaleDateStringWithLocale: (t, string) => string = "toLocaleDateString"
196+
197+
/**
198+
`toLocaleDateStringWithLocaleAndOptions(date, locale, options)`
199+
200+
Converts a JavaScript date to a localized date string. It will use the specified locale and formatting options.
201+
202+
## Examples
203+
```rescript
204+
Date.make()->Date.toLocaleDateStringWithLocaleAndOptions("en-US", { dateStyle: #long })->Console.log
205+
// February 19, 2023
206+
207+
Date.make()->Date.toLocaleDateStringWithLocaleAndOptions("de", { hour: #"2-digit", minute: #"2-digit" })->Console.log
208+
// 19.2.2023, 15:40
209+
210+
Date.make()->Date.toLocaleDateStringWithLocaleAndOptions("de", { year: #numeric })->Console.log
211+
// 2023
212+
```
213+
*/
214+
@send
215+
external toLocaleDateStringWithLocaleAndOptions: (t, string, localeOptions) => string =
216+
"toLocaleDateString"
217+
218+
/**
219+
`toLocaleString(date)`
220+
221+
Converts a JavaScript date to a localized date-time string. It will use the current locale.
222+
223+
## Examples
224+
```rescript
225+
Date.make()->Date.toLocaleString->Console.log
226+
// 2/19/2023, 3:40:00 PM
227+
```
228+
*/
229+
@send
230+
external toLocaleString: t => string = "toLocaleString"
231+
232+
/**
233+
`toLocaleStringWithLocale(date, locale)`
234+
235+
Converts a JavaScript date to a localized date-time string. It will use the specified locale.
236+
237+
## Examples
238+
```rescript
239+
Date.make()->Date.toLocaleStringWithLocale("en-US")->Console.log
240+
// 2/19/2023, 3:40:00 PM
241+
```
242+
*/
243+
@send
244+
external toLocaleStringWithLocale: (t, string) => string = "toLocaleString"
245+
246+
/**
247+
`toLocaleStringWithLocaleAndOptions(date, locale, options)`
248+
249+
Converts a JavaScript date to a localized date-time string. It will use the specified locale and formatting options.
250+
251+
## Examples
252+
```rescript
253+
Date.make()->Date.toLocaleStringWithLocaleAndOptions("en", { dateStyle: #short, timeStyle: #short })->Console.log
254+
// 2/19/23, 3:40 PM
255+
256+
Date.make()->Date.toLocaleStringWithLocaleAndOptions("en", { era: #long, year: #numeric, month: #"2-digit", day: #"2-digit", hour: #numeric, timeZoneName: #short })->Console.log
257+
// 02/19/2023 Anno Domini, 3 PM GMT+1
258+
```
259+
*/
260+
@send
261+
external toLocaleStringWithLocaleAndOptions: (t, string, localeOptions) => string = "toLocaleString"
262+
263+
/**
264+
`toLocaleTimeString(date)`
265+
266+
Converts a JavaScript date to a localized time string. It will use the current locale.
267+
268+
## Examples
269+
```rescript
270+
Date.make()->Date.toLocaleTimeString->Console.log
271+
// 3:40:00 PM
272+
```
273+
*/
274+
@send
275+
external toLocaleTimeString: t => string = "toLocaleTimeString"
276+
277+
/**
278+
`toLocaleTimeStringWithLocale(date, locale)`
279+
280+
Converts a JavaScript date to a localized time string. It will use the specified locale.
281+
282+
## Examples
283+
```rescript
284+
Date.make()->Date.toLocaleTimeStringWithLocale("en-US")->Console.log
285+
// 3:40:00 PM
286+
```
287+
*/
288+
@send
289+
external toLocaleTimeStringWithLocale: (t, string) => string = "toLocaleTimeString"
290+
291+
/**
292+
`toLocaleTimeStringWithLocaleAndOptions(date, locale, options)`
293+
294+
Converts a JavaScript date to a localized time string. It will use the specified locale and formatting options.
295+
296+
## Examples
297+
```rescript
298+
Date.make()->Date.toLocaleTimeStringWithLocaleAndOptions("en-US", { timeStyle: #long })->Console.log
299+
// 3:40:00 PM GMT+1
300+
301+
Date.make()->Date.toLocaleTimeStringWithLocaleAndOptions("de", { hour: #"2-digit", minute: #"2-digit" })->Console.log
302+
// 15:40
303+
```
304+
*/
305+
@send
306+
external toLocaleTimeStringWithLocaleAndOptions: (t, string, localeOptions) => string =
307+
"toLocaleTimeString"
308+
309+
@send external toISOString: t => string = "toISOString"
310+
@send external toUTCString: t => string = "toUTCString"
311+
@return(nullable) @send external toJSON: t => option<string> = "toJSON"

0 commit comments

Comments
 (0)