Passed
Push — master ( 790382...9b34f8 )
by Mark
09:58
created

TimeParts   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 34
dl 0
loc 102
ccs 33
cts 33
cp 1
rs 10
c 1
b 0
f 0
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A second() 0 18 3
A hour() 0 18 3
A minute() 0 18 3
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;
4
5
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
6
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
7
use PhpOffice\PhpSpreadsheet\Shared\Date as SharedDateHelper;
8
9
class TimeParts
10
{
11
    /**
12
     * HOUROFDAY.
13
     *
14
     * Returns the hour of a time value.
15
     * The hour is given as an integer, ranging from 0 (12:00 A.M.) to 23 (11:00 P.M.).
16
     *
17
     * Excel Function:
18
     *        HOUR(timeValue)
19
     *
20
     * @param mixed $timeValue Excel date serial value (float), PHP date timestamp (integer),
21
     *                                    PHP DateTime object, or a standard time string
22
     *
23
     * @return int|string Hour
24
     */
25 18
    public static function hour($timeValue)
26
    {
27
        try {
28 18
            $timeValue = Functions::flattenSingleValue($timeValue);
29 18
            Helpers::nullFalseTrueToNumber($timeValue);
30 18
            if (!is_numeric($timeValue)) {
31 5
                $timeValue = Helpers::getTimeValue($timeValue);
32
            }
33 18
            Helpers::validateNotNegative($timeValue);
34 2
        } catch (Exception $e) {
35 2
            return $e->getMessage();
36
        }
37
38
        // Execute function
39 16
        $timeValue = fmod($timeValue, 1);
40 16
        $timeValue = SharedDateHelper::excelToDateTimeObject($timeValue);
41
42 16
        return (int) $timeValue->format('H');
43
    }
44
45
    /**
46
     * MINUTE.
47
     *
48
     * Returns the minutes of a time value.
49
     * The minute is given as an integer, ranging from 0 to 59.
50
     *
51
     * Excel Function:
52
     *        MINUTE(timeValue)
53
     *
54
     * @param mixed $timeValue Excel date serial value (float), PHP date timestamp (integer),
55
     *                                    PHP DateTime object, or a standard time string
56
     *
57
     * @return int|string Minute
58
     */
59 18
    public static function minute($timeValue)
60
    {
61
        try {
62 18
            $timeValue = Functions::flattenSingleValue($timeValue);
63 18
            Helpers::nullFalseTrueToNumber($timeValue);
64 18
            if (!is_numeric($timeValue)) {
65 5
                $timeValue = Helpers::getTimeValue($timeValue);
66
            }
67 18
            Helpers::validateNotNegative($timeValue);
68 2
        } catch (Exception $e) {
69 2
            return $e->getMessage();
70
        }
71
72
        // Execute function
73 16
        $timeValue = fmod($timeValue, 1);
74 16
        $timeValue = SharedDateHelper::excelToDateTimeObject($timeValue);
75
76 16
        return (int) $timeValue->format('i');
77
    }
78
79
    /**
80
     * SECOND.
81
     *
82
     * Returns the seconds of a time value.
83
     * The minute is given as an integer, ranging from 0 to 59.
84
     *
85
     * Excel Function:
86
     *        SECOND(timeValue)
87
     *
88
     * @param mixed $timeValue Excel date serial value (float), PHP date timestamp (integer),
89
     *                                    PHP DateTime object, or a standard time string
90
     *
91
     * @return int|string Second
92
     */
93 18
    public static function second($timeValue)
94
    {
95
        try {
96 18
            $timeValue = Functions::flattenSingleValue($timeValue);
97 18
            Helpers::nullFalseTrueToNumber($timeValue);
98 18
            if (!is_numeric($timeValue)) {
99 5
                $timeValue = Helpers::getTimeValue($timeValue);
100
            }
101 18
            Helpers::validateNotNegative($timeValue);
102 2
        } catch (Exception $e) {
103 2
            return $e->getMessage();
104
        }
105
106
        // Execute function
107 16
        $timeValue = fmod($timeValue, 1);
108 16
        $timeValue = SharedDateHelper::excelToDateTimeObject($timeValue);
109
110 16
        return (int) $timeValue->format('s');
111
    }
112
}
113