| 973 | * Used instead of `Date.parse` because of browser discrepancies. |
| 974 | */ |
| 975 | export function isoStringToDate(match: RegExpMatchArray): Date { |
| 976 | const date = new Date(0); |
| 977 | let tzHour = 0; |
| 978 | let tzMin = 0; |
| 979 | |
| 980 | // match[8] means that the string contains "Z" (UTC) or a timezone like "+01:00" or "+0100" |
| 981 | const dateSetter = match[8] ? date.setUTCFullYear : date.setFullYear; |
| 982 | const timeSetter = match[8] ? date.setUTCHours : date.setHours; |
| 983 | |
| 984 | // if there is a timezone defined like "+01:00" or "+0100" |
| 985 | if (match[9]) { |
| 986 | tzHour = Number(match[9] + match[10]); |
| 987 | tzMin = Number(match[9] + match[11]); |
| 988 | } |
| 989 | dateSetter.call(date, Number(match[1]), Number(match[2]) - 1, Number(match[3])); |
| 990 | const h = Number(match[4] || 0) - tzHour; |
| 991 | const m = Number(match[5] || 0) - tzMin; |
| 992 | const s = Number(match[6] || 0); |
| 993 | // The ECMAScript specification (https://www.ecma-international.org/ecma-262/5.1/#sec-15.9.1.11) |
| 994 | // defines that `DateTime` milliseconds should always be rounded down, so that `999.9ms` |
| 995 | // becomes `999ms`. |
| 996 | const ms = Math.floor(parseFloat('0.' + (match[7] || 0)) * 1000); |
| 997 | timeSetter.call(date, h, m, s, ms); |
| 998 | return date; |
| 999 | } |
| 1000 | |
| 1001 | export function isDate(value: any): value is Date { |
| 1002 | return value instanceof Date && !isNaN(value.valueOf()); |