summaryrefslogtreecommitdiff
path: root/app/frontend/dto/TimezoneDTO.php
blob: e4078e6fb379c255d84c8ce20a80ed2e656b70a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php

class TimezoneDTO {

    public $Label;
    public $Name;
    public $Offset;
    public $OffsetHours;
    public $OffsetMinutes;
    public $Location;
    public $FirstDayOfTheWeek;

    public function __construct(string $name) {
        $tz = new DateTimeZone($name);
        $this->Name = $tz->getName();
        $this->Offset = $tz->getOffset(new DateTime());
        $this->OffsetHours = $this->Offset / 3600;
        $this->OffsetMinutes = $this->Offset % 3600 / 60;
        $this->Location = $tz->getLocation()['country_code'];
        $this->FirstDayOfTheWeek = $this->_getFirstDayOfTheWeek();
        $this->Label = sprintf('UTC%+03d:%02d %s', $this->OffsetHours, $this->OffsetMinutes, $this->Name);
    }

    private function _getFirstDayOfTheWeek() {
        $dayMapping = json_decode(
            file_get_contents(
                Prado::getPathOfNamespace('Application.dto.weekdays', '.json')
            ),
            TRUE
        );
        if ($this->Location && isset($dayMapping[$this->Location])) {
            return ucfirst($dayMapping[$this->Location]);
        }
        return ucfirst($dayMapping['001']);
    }



    public static function __compare(TimezoneDTO $tz1, TimezoneDTO $tz2) {
        $diff = $tz1->Offset - $tz2->Offset;
        return ($diff == 0) ? strcmp($tz1->Name, $tz2->Name) : $diff;
    }

}

?>