-
Notifications
You must be signed in to change notification settings - Fork 30
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
Add locale and formatting options to localeDateString, localeString and localTimeString functions #30
Merged
zth
merged 6 commits into
rescript-lang:main
from
dkirchhof:date-format-functions-with-options
Feb 19, 2023
Merged
Add locale and formatting options to localeDateString, localeString and localTimeString functions #30
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
943bc54
Add locale and formatting options to localeDateString, localeString a…
dkirchhof a3c5f9e
Add doc strings for date to locale string functions
dkirchhof 6876ab7
Merge branch 'main' into date-format-functions-with-options
dkirchhof 1ac4d86
Merge branch 'main' into date-format-functions-with-options
dkirchhof f594ec5
Update changelog; Add return values of code examples
dkirchhof 20f5535
Merge branch 'main' into date-format-functions-with-options
dkirchhof 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 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 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,294 @@ | ||
/*** | ||
Functions for interacting with JavaScript Dates. | ||
*/ | ||
|
||
type t = Js.Date.t | ||
type time = float | ||
|
||
/** | ||
A type representing date time format options. | ||
|
||
Note: There are some properties missing: | ||
- fractionalSecondDigits | ||
- dayPeriod | ||
- calendar | ||
- numberingSystem | ||
- localeMatcher | ||
- timeZone | ||
- hour12 | ||
- hourCycle | ||
- formatMatcher | ||
|
||
See full spec at https://tc39.es/ecma402/#datetimeformat-objects | ||
*/ | ||
type localeOptions = { | ||
dateStyle?: [#full | #long | #medium | #short], | ||
timeStyle?: [#full | #long | #medium | #short], | ||
weekday?: [#long | #narrow | #short], | ||
era?: [#long | #narrow | #short], | ||
year?: [#"2-digit" | #numeric], | ||
month?: [#"2-digit" | #long | #narrow | #numeric | #short], | ||
day?: [#"2-digit" | #numeric], | ||
hour?: [#"2-digit" | #numeric], | ||
minute?: [#"2-digit" | #numeric], | ||
second?: [#"2-digit" | #numeric], | ||
timeZoneName?: [#long | #short], | ||
} | ||
|
||
@send external valueOf: t => time = "valueOf" | ||
@new external make: unit => t = "Date" | ||
@new external fromString: string => t = "Date" | ||
@new external fromTime: time => t = "Date" | ||
@new external makeWithYM: (~year: int, ~month: int) => t = "Date" | ||
@new external makeWithYMD: (~year: int, ~month: int, ~date: int) => t = "Date" | ||
@new external makeWithYMDH: (~year: int, ~month: int, ~date: int, ~hours: int) => t = "Date" | ||
@new | ||
external makeWithYMDHM: (~year: int, ~month: int, ~date: int, ~hours: int, ~minutes: int) => t = | ||
"Date" | ||
@new | ||
external makeWithYMDHMS: ( | ||
~year: int, | ||
~month: int, | ||
~date: int, | ||
~hours: int, | ||
~minutes: int, | ||
~seconds: int, | ||
) => t = "Date" | ||
@new | ||
external makeWithYMDHMSM: ( | ||
~year: int, | ||
~month: int, | ||
~date: int, | ||
~hours: int, | ||
~minutes: int, | ||
~seconds: int, | ||
~milliseconds: int, | ||
) => t = "Date" | ||
module UTC: { | ||
@val external makeWithYM: (~year: int, ~month: int) => time = "Date.UTC" | ||
@val external makeWithYMD: (~year: int, ~month: int, ~date: int) => time = "Date.UTC" | ||
@val | ||
external makeWithYMDH: (~year: int, ~month: int, ~date: int, ~hours: int) => time = "Date.UTC" | ||
@val | ||
external makeWithYMDHM: ( | ||
~year: int, | ||
~month: int, | ||
~date: int, | ||
~hours: int, | ||
~minutes: int, | ||
) => time = "Date.UTC" | ||
@val | ||
external makeWithYMDHMS: ( | ||
~year: int, | ||
~month: int, | ||
~date: int, | ||
~hours: int, | ||
~minutes: int, | ||
~seconds: int, | ||
) => time = "Date.UTC" | ||
@val | ||
external makeWithYMDHMSM: ( | ||
~year: int, | ||
~month: int, | ||
~date: int, | ||
~hours: int, | ||
~minutes: int, | ||
~seconds: int, | ||
~milliseconds: int, | ||
) => time = "Date.UTC" | ||
} | ||
@val external now: unit => time = "Date.now" | ||
@send external getTime: t => time = "getTime" | ||
@send external getTimezoneOffset: t => int = "getTimezoneOffset" | ||
@send external getFullYear: t => int = "getFullYear" | ||
@send external getMonth: t => int = "getMonth" | ||
@send external getDate: t => int = "getDate" | ||
@send external getHours: t => int = "getHours" | ||
@send external getMinutes: t => int = "getMinutes" | ||
@send external getSeconds: t => int = "getSeconds" | ||
@send external getMilliseconds: t => int = "getMilliseconds" | ||
@send external getDay: t => int = "getDay" | ||
@send external setFullYear: (t, int) => unit = "setFullYear" | ||
@send external setFullYearM: (t, ~year: int, ~month: int) => unit = "setFullYear" | ||
@send external setFullYearMD: (t, ~year: int, ~month: int, ~date: int) => unit = "setFullYear" | ||
@send external setMonth: (t, int) => unit = "setMonth" | ||
@send external setDate: (t, int) => unit = "setDate" | ||
@send external setHours: (t, int) => unit = "setHours" | ||
@send external setHoursM: (t, ~hours: int, ~minutes: int) => unit = "setHours" | ||
@send external setHoursMS: (t, ~hours: int, ~minutes: int, ~seconds: int) => unit = "setHours" | ||
@send | ||
external setHoursMSMs: (t, ~hours: int, ~minutes: int, ~seconds: int, ~milliseconds: int) => unit = | ||
"setHours" | ||
@send external setMinutes: (t, int) => unit = "setMinutes" | ||
@send external setMinutesS: (t, ~minutes: int, ~seconds: int) => unit = "setMinutes" | ||
@send | ||
external setMinutesSMs: (t, ~minutes: int, ~seconds: int, ~milliseconds: int) => unit = "setMinutes" | ||
@send external setSeconds: (t, int) => unit = "setSeconds" | ||
@send external setSecondsMs: (t, ~seconds: int, ~milliseconds: int) => unit = "setSeconds" | ||
@send external setMilliseconds: (t, int) => unit = "setMilliseconds" | ||
@send external setDay: (t, int) => unit = "setDay" | ||
@send external getUTCFullYear: t => int = "getUTCFullYear" | ||
@send external getUTCMonth: t => int = "getUTCMonth" | ||
@send external getUTCDate: t => int = "getUTCDate" | ||
@send external getUTCHours: t => int = "getUTCHours" | ||
@send external getUTCMinutes: t => int = "getUTCMinutes" | ||
@send external getUTCSeconds: t => int = "getUTCSeconds" | ||
@send external getUTCMilliseconds: t => int = "getUTCMilliseconds" | ||
@send external getUTCDay: t => int = "getUTCDay" | ||
@send external setUTCFullYear: (t, int) => unit = "setUTCFullYear" | ||
@send external setUTCFullYearM: (t, ~year: int, ~month: int) => unit = "setUTCFullYear" | ||
@send | ||
external setUTCFullYearMD: (t, ~year: int, ~month: int, ~date: int) => unit = "setUTCFullYear" | ||
@send external setUTCMonth: (t, int) => unit = "setUTCMonth" | ||
@send external setUTCDate: (t, int) => unit = "setUTCDate" | ||
@send external setUTCHours: (t, int) => unit = "setUTCHours" | ||
@send external setUTCHoursM: (t, ~hours: int, ~minutes: int) => unit = "setUTCHours" | ||
@send | ||
external setUTCHoursMS: (t, ~hours: int, ~minutes: int, ~seconds: int) => unit = "setUTCHours" | ||
@send | ||
external setUTCHoursMSMs: ( | ||
t, | ||
~hours: int, | ||
~minutes: int, | ||
~seconds: int, | ||
~milliseconds: int, | ||
) => unit = "setUTCHours" | ||
@send external setUTCMinutes: (t, int) => unit = "setUTCMinutes" | ||
@send external setUTCMinutesS: (t, ~minutes: int, ~seconds: int) => unit = "setUTCMinutes" | ||
@send | ||
external setUTCMinutesSMs: (t, ~minutes: int, ~seconds: int, ~milliseconds: int) => unit = | ||
"setUTCMinutes" | ||
@send external setUTCSeconds: (t, int) => unit = "setUTCSeconds" | ||
@send external setUTCSecondsMs: (t, ~seconds: int, ~milliseconds: int) => unit = "setUTCSeconds" | ||
@send external setUTCMilliseconds: (t, int) => unit = "setUTCMilliseconds" | ||
@send external setUTCDay: (t, int) => unit = "setUTCDay" | ||
@send external toDateString: t => string = "toDateString" | ||
@send external toString: t => string = "toString" | ||
@send external toTimeString: t => string = "toTimeString" | ||
|
||
/** | ||
`toLocaleDateString(date)` | ||
|
||
Converts a JavaScript date to a localized date string. It will use the current locale. | ||
|
||
## Examples | ||
```rescript | ||
Date.make()->Date.toLocaleDateString->Console.log | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you also add a comment with what the output will be when you log to the console here? Or rather, what the output is when you do it now as you're adding the docstrings. This goes for all the examples. |
||
``` | ||
*/ | ||
@send | ||
external toLocaleDateString: t => string = "toLocaleDateString" | ||
|
||
/** | ||
`toLocaleDateStringWithLocale(date, locale)` | ||
|
||
Converts a JavaScript date to a localized date string. It will use the specified locale. | ||
|
||
## Examples | ||
```rescript | ||
Date.make()->Date.toLocaleDateStringWithLocale("en-US")->Console.log | ||
``` | ||
*/ | ||
@send | ||
external toLocaleDateStringWithLocale: (t, string) => string = "toLocaleDateString" | ||
|
||
/** | ||
`toLocaleDateStringWithLocaleAndOptions(date, locale, options)` | ||
|
||
Converts a JavaScript date to a localized date string. It will use the specified locale and formatting options. | ||
|
||
## Examples | ||
```rescript | ||
Date.make()->Date.toLocaleDateStringWithLocaleAndOptions("en-US", { dateStyle: #long })->Console.log | ||
Date.make()->Date.toLocaleDateStringWithLocaleAndOptions("de", { hour: #"2-digit", minute: #"2-digit" })->Console.log | ||
Date.make()->Date.toLocaleDateStringWithLocaleAndOptions("de", { year: #numeric })->Console.log | ||
``` | ||
*/ | ||
@send | ||
external toLocaleDateStringWithLocaleAndOptions: (t, string, localeOptions) => string = | ||
"toLocaleDateString" | ||
|
||
/** | ||
`toLocaleString(date)` | ||
|
||
Converts a JavaScript date to a localized date-time string. It will use the current locale. | ||
|
||
## Examples | ||
```rescript | ||
Date.make()->Date.toLocaleString->Console.log | ||
``` | ||
*/ | ||
@send | ||
external toLocaleString: t => string = "toLocaleString" | ||
|
||
/** | ||
`toLocaleStringWithLocale(date, locale)` | ||
|
||
Converts a JavaScript date to a localized date-time string. It will use the specified locale. | ||
|
||
## Examples | ||
```rescript | ||
Date.make()->Date.toLocaleStringWithLocale("en-US")->Console.log | ||
``` | ||
*/ | ||
@send | ||
external toLocaleStringWithLocale: (t, string) => string = "toLocaleString" | ||
|
||
/** | ||
`toLocaleStringWithLocaleAndOptions(date, locale, options)` | ||
|
||
Converts a JavaScript date to a localized date-time string. It will use the specified locale and formatting options. | ||
|
||
## Examples | ||
```rescript | ||
Date.make()->Date.toLocaleStringWithLocaleAndOptions("en", { dateStyle: #short, timeStyle: #short })->Console.log | ||
Date.make()->Date.toLocaleStringWithLocaleAndOptions("en", { era: #long, year: #numeric, month: #"2-digit", day: #"2-digit", hour: #numeric, timeZoneName: #short })->Console.log | ||
``` | ||
*/ | ||
@send | ||
external toLocaleStringWithLocaleAndOptions: (t, string, localeOptions) => string = "toLocaleString" | ||
|
||
/** | ||
`toLocaleTimeString(date)` | ||
|
||
Converts a JavaScript date to a localized time string. It will use the current locale. | ||
|
||
## Examples | ||
```rescript | ||
Date.make()->Date.toLocaleTimeString->Console.log | ||
``` | ||
*/ | ||
@send | ||
external toLocaleTimeString: t => string = "toLocaleTimeString" | ||
|
||
/** | ||
`toLocaleTimeStringWithLocale(date, locale)` | ||
|
||
Converts a JavaScript date to a localized time string. It will use the specified locale. | ||
|
||
## Examples | ||
```rescript | ||
Date.make()->Date.toLocaleTimeStringWithLocale("en-US")->Console.log | ||
``` | ||
*/ | ||
@send | ||
external toLocaleTimeStringWithLocale: (t, string) => string = "toLocaleTimeString" | ||
|
||
/** | ||
`toLocaleTimeStringWithLocaleAndOptions(date, locale, options)` | ||
|
||
Converts a JavaScript date to a localized time string. It will use the specified locale and formatting options. | ||
|
||
## Examples | ||
```rescript | ||
Date.make()->Date.toLocaleTimeStringWithLocaleAndOptions("en-US", { timeStyle: #long })->Console.log | ||
Date.make()->Date.toLocaleTimeStringWithLocaleAndOptions("de", { hour: #"2-digit", minute: #"2-digit" })->Console.log | ||
``` | ||
*/ | ||
@send | ||
external toLocaleTimeStringWithLocaleAndOptions: (t, string, localeOptions) => string = | ||
"toLocaleTimeString" | ||
|
||
@send external toISOString: t => string = "toISOString" | ||
@send external toUTCString: t => string = "toUTCString" | ||
@return(nullable) @send external toJSON: t => option<string> = "toJSON" |
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.
You can remove the indentation here.