| Total Complexity | 7 |
| Total Lines | 67 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | import { getTimeZones } from '@vvo/tzdb'; |
||
| 2 | |||
| 3 | /** |
||
| 4 | * @deprecated tag:v6.6.0 - Will be private |
||
| 5 | */ |
||
| 6 | export default class TimezoneService { |
||
| 7 | /** |
||
| 8 | * @package system-settings |
||
| 9 | * |
||
| 10 | * Returns an array of all timezones in the world |
||
| 11 | * @returns {Promise<string[]>} |
||
| 12 | */ |
||
| 13 | async loadTimezones() { |
||
| 14 | const timezonesImport = await import('@vvo/tzdb/time-zones-names.json'); |
||
| 15 | |||
| 16 | return timezonesImport.default; |
||
| 17 | } |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @package system-settings |
||
| 21 | * |
||
| 22 | * Returns an array of time zones objects |
||
| 23 | * @returns {object[]} |
||
| 24 | */ |
||
| 25 | getTimezoneOptions() { |
||
| 26 | const timezones = getTimeZones(); |
||
| 27 | const items = timezones.map(({ currentTimeOffsetInMinutes, name }) => ({ |
||
| 28 | label: `${this.toUTCTime(currentTimeOffsetInMinutes)} ${name}`, |
||
| 29 | value: name, |
||
| 30 | })); |
||
| 31 | |||
| 32 | return [ |
||
| 33 | { |
||
| 34 | label: 'UTC', |
||
| 35 | value: 'UTC', |
||
| 36 | }, |
||
| 37 | ...items, |
||
| 38 | ]; |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @package system-settings |
||
| 43 | * @param number |
||
| 44 | * Returns a string containing UTC, hours, and minutes |
||
| 45 | * @returns {string} |
||
| 46 | */ |
||
| 47 | toUTCTime(number: number): string { |
||
| 48 | if (number === 0) { |
||
| 49 | return '(UTC)'; |
||
| 50 | } |
||
| 51 | |||
| 52 | let hours: number|string = Math.floor(number / 60); |
||
| 53 | let minutes = `${Math.abs(number % 60)}`; |
||
| 54 | |||
| 55 | if (hours > 0) { |
||
| 56 | hours = `+${hours}`; |
||
| 57 | } |
||
| 58 | |||
| 59 | hours = `${hours}`; |
||
| 60 | if (hours.length < 3) { |
||
| 61 | hours = hours.split('').join('0'); |
||
| 62 | } |
||
| 63 | |||
| 64 | if (minutes.length < 2) { |
||
| 65 | minutes = `0${minutes}`; |
||
| 66 | } |
||
| 67 | |||
| 68 | return `(UTC ${hours}:${minutes})`; |
||
| 69 | } |
||
| 71 |