(dateStr: string)
| 168 | } |
| 169 | |
| 170 | export function decodeDatetime(dateStr: string): number | Date { |
| 171 | /** |
| 172 | * Postgres uses ISO 8601 style date output by default: |
| 173 | * 1997-12-17 07:37:16-08 |
| 174 | */ |
| 175 | |
| 176 | const matches = DATETIME_RE.exec(dateStr); |
| 177 | |
| 178 | if (!matches) { |
| 179 | return decodeDate(dateStr); |
| 180 | } |
| 181 | |
| 182 | const isBC = BC_RE.test(dateStr); |
| 183 | |
| 184 | const year = parseInt(matches[1], 10) * (isBC ? -1 : 1); |
| 185 | // remember JS dates are 0-based |
| 186 | const month = parseInt(matches[2], 10) - 1; |
| 187 | const day = parseInt(matches[3], 10); |
| 188 | const hour = parseInt(matches[4], 10); |
| 189 | const minute = parseInt(matches[5], 10); |
| 190 | const second = parseInt(matches[6], 10); |
| 191 | // ms are written as .007 |
| 192 | const msMatch = matches[7]; |
| 193 | const ms = msMatch ? 1000 * parseFloat(msMatch) : 0; |
| 194 | |
| 195 | let date: Date; |
| 196 | |
| 197 | const offset = decodeTimezoneOffset(dateStr); |
| 198 | if (offset === null) { |
| 199 | date = new Date(year, month, day, hour, minute, second, ms); |
| 200 | } else { |
| 201 | // This returns miliseconds from 1 January, 1970, 00:00:00, |
| 202 | // adding decoded timezone offset will construct proper date object. |
| 203 | const utc = Date.UTC(year, month, day, hour, minute, second, ms); |
| 204 | date = new Date(utc + offset); |
| 205 | } |
| 206 | |
| 207 | // use `setUTCFullYear` because if date is from first |
| 208 | // century `Date`'s compatibility for millenium bug |
| 209 | // would set it as 19XX |
| 210 | date.setUTCFullYear(year); |
| 211 | return date; |
| 212 | } |
| 213 | |
| 214 | export function decodeDatetimeArray(value: string) { |
| 215 | return parseArray(value, decodeDatetime); |
no test coverage detected