* Decode numerical timezone offset from provided date string. * * Matched these kinds: * - `Z (UTC)` * - `-05` * - `+06:30` * - `+06:30:10` * * Returns offset in miliseconds.
(dateStr: string)
| 384 | * Returns offset in miliseconds. |
| 385 | */ |
| 386 | function decodeTimezoneOffset(dateStr: string): null | number { |
| 387 | // get rid of date part as TIMEZONE_RE would match '-MM` part |
| 388 | const timeStr = dateStr.split(" ")[1]; |
| 389 | const matches = TIMEZONE_RE.exec(timeStr); |
| 390 | |
| 391 | if (!matches) { |
| 392 | return null; |
| 393 | } |
| 394 | |
| 395 | const type = matches[1]; |
| 396 | |
| 397 | if (type === "Z") { |
| 398 | // Zulu timezone === UTC === 0 |
| 399 | return 0; |
| 400 | } |
| 401 | |
| 402 | // in JS timezone offsets are reversed, ie. timezones |
| 403 | // that are "positive" (+01:00) are represented as negative |
| 404 | // offsets and vice-versa |
| 405 | const sign = type === "-" ? 1 : -1; |
| 406 | |
| 407 | const hours = parseInt(matches[2], 10); |
| 408 | const minutes = parseInt(matches[3] || "0", 10); |
| 409 | const seconds = parseInt(matches[4] || "0", 10); |
| 410 | |
| 411 | const offset = hours * 3600 + minutes * 60 + seconds; |
| 412 | |
| 413 | return sign * offset * 1000; |
| 414 | } |
| 415 | |
| 416 | export function decodeTid(value: string): TID { |
| 417 | const [x, y] = value.substring(1, value.length - 1).split(","); |