Passed
Push — master ( 248e0b...37ab4f )
by Gaetano
09:47
created

Date::iso8601Decode()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 0
Metric Value
cc 3
eloc 7
c 0
b 0
f 0
nc 3
nop 2
dl 0
loc 12
ccs 6
cts 7
cp 0.8571
crap 3.0261
rs 10
1
<?php
2
3
namespace PhpXmlRpc\Helper;
4
5
class Date
6
{
7
    /**
8
     * Given a timestamp, return the corresponding ISO8601 encoded string.
9
     *
10
     * Really, timezones ought to be supported but the XML-RPC spec says:
11
     *
12
     * "Don't assume a timezone. It should be specified by the server in its documentation what assumptions it makes
13
     *  about timezones."
14
     *
15
     * These routines always assume localtime unless $utc is set to 1, in which case UTC is assumed and an adjustment
16
     * for locale is made when encoding
17
     *
18
     * @param int $timet (timestamp)
19
     * @param int $utc (0 or 1)
20
     *
21
     * @return string
22
     */
23 23
    public static function iso8601Encode($timet, $utc = 0)
24
    {
25 23
        if (!$utc) {
26 23
            $t = date('Ymd\TH:i:s', $timet);
27
        } else {
28 1
            $t = gmdate('Ymd\TH:i:s', $timet);
29
        }
30
31 23
        return $t;
32
    }
33
34
    /**
35
     * Given an ISO8601 date string, return a timet in the localtime, or UTC.
36
     *
37
     * @param string $idate
38
     * @param int $utc either 0 or 1
39
     *
40
     * @return int (datetime)
41
     */
42 1
    public static function iso8601Decode($idate, $utc = 0)
43
    {
44 1
        $t = 0;
45 1
        if (preg_match('/([0-9]{4})([0-1][0-9])([0-3][0-9])T([0-2][0-9]):([0-5][0-9]):([0-5][0-9])/', $idate, $regs)) {
46 1
            if ($utc) {
47
                $t = gmmktime($regs[4], $regs[5], $regs[6], $regs[2], $regs[3], $regs[1]);
48
            } else {
49 1
                $t = mktime($regs[4], $regs[5], $regs[6], $regs[2], $regs[3], $regs[1]);
50
            }
51
        }
52
53 1
        return $t;
54
    }
55
}
56