|
| 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 | + ``` |
| 178 | +*/ |
| 179 | +@send |
| 180 | +external toLocaleDateString: t => string = "toLocaleDateString" |
| 181 | + |
| 182 | +/** |
| 183 | + `toLocaleDateStringWithLocale(date, locale)` |
| 184 | + |
| 185 | + Converts a JavaScript date to a localized date string. It will use the specified locale. |
| 186 | + |
| 187 | + ## Examples |
| 188 | + ```rescript |
| 189 | + Date.make()->Date.toLocaleDateStringWithLocale("en-US")->Console.log |
| 190 | + ``` |
| 191 | +*/ |
| 192 | +@send |
| 193 | +external toLocaleDateStringWithLocale: (t, string) => string = "toLocaleDateString" |
| 194 | + |
| 195 | +/** |
| 196 | + `toLocaleDateStringWithLocaleAndOptions(date, locale, options)` |
| 197 | + |
| 198 | + Converts a JavaScript date to a localized date string. It will use the specified locale and formatting options. |
| 199 | + |
| 200 | + ## Examples |
| 201 | + ```rescript |
| 202 | + Date.make()->Date.toLocaleDateStringWithLocaleAndOptions("en-US", { dateStyle: #long })->Console.log |
| 203 | + Date.make()->Date.toLocaleDateStringWithLocaleAndOptions("de", { hour: #"2-digit", minute: #"2-digit" })->Console.log |
| 204 | + Date.make()->Date.toLocaleDateStringWithLocaleAndOptions("de", { year: #numeric })->Console.log |
| 205 | + ``` |
| 206 | +*/ |
| 207 | +@send |
| 208 | +external toLocaleDateStringWithLocaleAndOptions: (t, string, localeOptions) => string = |
| 209 | + "toLocaleDateString" |
| 210 | + |
| 211 | +/** |
| 212 | + `toLocaleString(date)` |
| 213 | + |
| 214 | + Converts a JavaScript date to a localized date-time string. It will use the current locale. |
| 215 | + |
| 216 | + ## Examples |
| 217 | + ```rescript |
| 218 | + Date.make()->Date.toLocaleString->Console.log |
| 219 | + ``` |
| 220 | +*/ |
| 221 | +@send |
| 222 | +external toLocaleString: t => string = "toLocaleString" |
| 223 | + |
| 224 | +/** |
| 225 | + `toLocaleStringWithLocale(date, locale)` |
| 226 | + |
| 227 | + Converts a JavaScript date to a localized date-time string. It will use the specified locale. |
| 228 | + |
| 229 | + ## Examples |
| 230 | + ```rescript |
| 231 | + Date.make()->Date.toLocaleStringWithLocale("en-US")->Console.log |
| 232 | + ``` |
| 233 | +*/ |
| 234 | +@send |
| 235 | +external toLocaleStringWithLocale: (t, string) => string = "toLocaleString" |
| 236 | + |
| 237 | +/** |
| 238 | + `toLocaleStringWithLocaleAndOptions(date, locale, options)` |
| 239 | + |
| 240 | + Converts a JavaScript date to a localized date-time string. It will use the specified locale and formatting options. |
| 241 | + |
| 242 | + ## Examples |
| 243 | + ```rescript |
| 244 | + Date.make()->Date.toLocaleStringWithLocaleAndOptions("en", { dateStyle: #short, timeStyle: #short })->Console.log |
| 245 | + Date.make()->Date.toLocaleStringWithLocaleAndOptions("en", { era: #long, year: #numeric, month: #"2-digit", day: #"2-digit", hour: #numeric, timeZoneName: #short })->Console.log |
| 246 | + ``` |
| 247 | +*/ |
| 248 | +@send |
| 249 | +external toLocaleStringWithLocaleAndOptions: (t, string, localeOptions) => string = "toLocaleString" |
| 250 | + |
| 251 | +/** |
| 252 | + `toLocaleTimeString(date)` |
| 253 | + |
| 254 | + Converts a JavaScript date to a localized time string. It will use the current locale. |
| 255 | + |
| 256 | + ## Examples |
| 257 | + ```rescript |
| 258 | + Date.make()->Date.toLocaleTimeString->Console.log |
| 259 | + ``` |
| 260 | +*/ |
| 261 | +@send |
| 262 | +external toLocaleTimeString: t => string = "toLocaleTimeString" |
| 263 | + |
| 264 | +/** |
| 265 | + `toLocaleTimeStringWithLocale(date, locale)` |
| 266 | + |
| 267 | + Converts a JavaScript date to a localized time string. It will use the specified locale. |
| 268 | + |
| 269 | + ## Examples |
| 270 | + ```rescript |
| 271 | + Date.make()->Date.toLocaleTimeStringWithLocale("en-US")->Console.log |
| 272 | + ``` |
| 273 | +*/ |
| 274 | +@send |
| 275 | +external toLocaleTimeStringWithLocale: (t, string) => string = "toLocaleTimeString" |
| 276 | + |
| 277 | +/** |
| 278 | + `toLocaleTimeStringWithLocaleAndOptions(date, locale, options)` |
| 279 | + |
| 280 | + Converts a JavaScript date to a localized time string. It will use the specified locale and formatting options. |
| 281 | + |
| 282 | + ## Examples |
| 283 | + ```rescript |
| 284 | + Date.make()->Date.toLocaleTimeStringWithLocaleAndOptions("en-US", { timeStyle: #long })->Console.log |
| 285 | + Date.make()->Date.toLocaleTimeStringWithLocaleAndOptions("de", { hour: #"2-digit", minute: #"2-digit" })->Console.log |
| 286 | + ``` |
| 287 | +*/ |
| 288 | +@send |
| 289 | +external toLocaleTimeStringWithLocaleAndOptions: (t, string, localeOptions) => string = |
| 290 | + "toLocaleTimeString" |
| 291 | + |
| 292 | +@send external toISOString: t => string = "toISOString" |
| 293 | +@send external toUTCString: t => string = "toUTCString" |
| 294 | +@return(nullable) @send external toJSON: t => option<string> = "toJSON" |
0 commit comments