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

TimeValue::fromString()   B

Complexity

Conditions 9
Paths 8

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 9

Importance

Changes 0
Metric Value
cc 9
eloc 18
nc 8
nop 1
dl 0
loc 28
ccs 18
cts 18
cp 1
crap 9
rs 8.0555
c 0
b 0
f 0
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;
4
5
use Datetime;
6
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
7
use PhpOffice\PhpSpreadsheet\Shared\Date as SharedDateHelper;
8
9
class TimeValue
10
{
11
    /**
12
     * TIMEVALUE.
13
     *
14
     * Returns a value that represents a particular time.
15
     * Use TIMEVALUE to convert a time represented by a text string to an Excel or PHP date/time stamp
16
     * value.
17
     *
18
     * NOTE: When used in a Cell Formula, MS Excel changes the cell format so that it matches the time
19
     * format of your regional settings. PhpSpreadsheet does not change cell formatting in this way.
20
     *
21
     * Excel Function:
22
     *        TIMEVALUE(timeValue)
23
     *
24
     * @param string $timeValue A text string that represents a time in any one of the Microsoft
25
     *                                    Excel time formats; for example, "6:45 PM" and "18:45" text strings
26
     *                                    within quotation marks that represent time.
27
     *                                    Date information in time_text is ignored.
28
     *
29
     * @return mixed Excel date/time serial value, PHP date/time serial value or PHP date/time object,
30
     *                        depending on the value of the ReturnDateType flag
31
     */
32 39
    public static function fromString($timeValue)
33
    {
34 39
        $timeValue = trim(Functions::flattenSingleValue($timeValue), '"');
35 39
        $timeValue = str_replace(['/', '.'], '-', $timeValue);
36
37 39
        $arraySplit = preg_split('/[\/:\-\s]/', $timeValue) ?: [];
38 39
        if ((count($arraySplit) == 2 || count($arraySplit) == 3) && $arraySplit[0] > 24) {
39 1
            $arraySplit[0] = ($arraySplit[0] % 24);
40 1
            $timeValue = implode(':', $arraySplit);
41
        }
42
43 39
        $PHPDateArray = date_parse($timeValue);
44 39
        $retValue = Functions::VALUE();
45 39
        if (($PHPDateArray !== false) && ($PHPDateArray['error_count'] == 0)) {
46
            // OpenOffice-specific code removed - it works just like Excel
47 31
            $excelDateValue = SharedDateHelper::formattedPHPToExcel(1900, 1, 1, $PHPDateArray['hour'], $PHPDateArray['minute'], $PHPDateArray['second']) - 1;
48
49 31
            $retType = Functions::getReturnDateType();
50 31
            if ($retType === Functions::RETURNDATE_EXCEL) {
51 29
                $retValue = (float) $excelDateValue;
52 2
            } elseif ($retType === Functions::RETURNDATE_UNIX_TIMESTAMP) {
53 1
                $retValue = (int) $phpDateValue = SharedDateHelper::excelToTimestamp($excelDateValue + 25569) - 3600;
0 ignored issues
show
Unused Code introduced by
The assignment to $phpDateValue is dead and can be removed.
Loading history...
54
            } else {
55 1
                $retValue = new DateTime('1900-01-01 ' . $PHPDateArray['hour'] . ':' . $PHPDateArray['minute'] . ':' . $PHPDateArray['second']);
56
            }
57
        }
58
59 39
        return $retValue;
60
    }
61
}
62