Passed
Push — trunk ( adb1f8...2b412b )
by Christian
12:16 queued 12s
created

src/Administration/Resources/app/administration/src/core/service/timezone.service.ts   A

Complexity

Total Complexity 7
Complexity/F 2.33

Size

Lines of Code 71
Function Count 3

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 39
dl 0
loc 71
rs 10
c 0
b 0
f 0
wmc 7
mnd 4
bc 4
fnc 3
bpm 1.3333
cpm 2.3333
noi 0

3 Functions

Rating   Name   Duplication   Size   Complexity  
B TimezoneService.toUTCTime 0 29 5
A TimezoneService.loadTimezones 0 10 1
A TimezoneService.getTimezoneOptions 0 20 1
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
    }
70
}
71