DateHandler   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
c 0
b 0
f 0
dl 0
loc 91
rs 10
wmc 8

7 Methods

Rating   Name   Duplication   Size   Complexity  
A prettyHeader() 0 6 1
A fromSlot() 0 5 1
A prettyDateSlot() 0 3 1
A toSlot() 0 4 1
A __construct() 0 3 1
A prettyDateEpoch() 0 3 1
A getDST() 0 6 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Module\statistics;
6
7
/**
8
 * @package SimpleSAMLphp
9
 */
10
class DateHandler
11
{
12
    /** @var int */
13
    protected int $offset;
14
15
16
    /**
17
     * Constructor
18
     *
19
     * @param int $offset Date offset
20
     */
21
    public function __construct(int $offset)
22
    {
23
        $this->offset = $offset;
24
    }
25
26
27
    /**
28
     * @param int $timestamp
29
     * @return int
30
     */
31
    protected function getDST(int $timestamp): int
32
    {
33
        if (idate('I', $timestamp)) {
34
            return 3600;
35
        }
36
        return 0;
37
    }
38
39
40
    /**
41
     * @param int $epoch
42
     * @param int $slotsize
43
     * @return int
44
     */
45
    public function toSlot(int $epoch, int $slotsize): int
46
    {
47
        $dst = $this->getDST($epoch);
48
        return intval(floor(($epoch + $this->offset + $dst) / $slotsize));
49
    }
50
51
52
    /**
53
     * @param int $slot
54
     * @param int $slotsize
55
     * @return int
56
     */
57
    public function fromSlot(int $slot, int $slotsize): int
58
    {
59
        $temp = $slot * $slotsize - $this->offset;
60
        $dst = $this->getDST($temp);
61
        return $slot * $slotsize - $this->offset - $dst;
62
    }
63
64
65
    /**
66
     * @param int $epoch
67
     * @param string $dateformat
68
     * @return string
69
     */
70
    public function prettyDateEpoch(int $epoch, string $dateformat): string
71
    {
72
        return date($dateformat, $epoch);
73
    }
74
75
76
    /**
77
     * @param int $slot
78
     * @param int $slotsize
79
     * @param string $dateformat
80
     * @return string
81
     */
82
    public function prettyDateSlot(int $slot, int $slotsize, string $dateformat): string
83
    {
84
        return $this->prettyDateEpoch($this->fromSlot($slot, $slotsize), $dateformat);
85
    }
86
87
88
    /**
89
     * @param int $from
90
     * @param int $to
91
     * @param int $slotsize
92
     * @param string $dateformat
93
     * @return string
94
     */
95
    public function prettyHeader(int $from, int $to, int $slotsize, string $dateformat): string
96
    {
97
        $text = $this->prettyDateSlot($from, $slotsize, $dateformat);
98
        $text .= ' to ';
99
        $text .= $this->prettyDateSlot($to, $slotsize, $dateformat);
100
        return $text;
101
    }
102
}
103