Completed
Push — master ( 6be6c0...cc60cf )
by Gaetano
03:16
created

Date   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 28.57%

Importance

Changes 0
Metric Value
dl 0
loc 59
ccs 4
cts 14
cp 0.2857
rs 10
c 0
b 0
f 0
wmc 6
lcom 0
cbo 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A iso8601Encode() 0 16 3
A iso8601Decode() 0 13 3
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
11
     * but the XML-RPC spec says:
12
     *
13
     * "Don't assume a timezone. It should be specified by the server in its
14
     * documentation what assumptions it makes about timezones."
15
     *
16
     * These routines always assume localtime unless
17
     * $utc is set to 1, in which case UTC is assumed
18
     * and an adjustment for locale is made when encoding
19
     *
20
     * @param int $timet (timestamp)
21
     * @param int $utc (0 or 1)
22
     *
23
     * @return string
24
     */
25 19
    public static function iso8601Encode($timet, $utc = 0)
26
    {
27 19
        if (!$utc) {
28 19
            $t = strftime("%Y%m%dT%H:%M:%S", $timet);
29
        } else {
30
            if (function_exists('gmstrftime')) {
31
                // gmstrftime doesn't exist in some versions
32
                // of PHP
33
                $t = gmstrftime("%Y%m%dT%H:%M:%S", $timet);
34
            } else {
35
                $t = strftime("%Y%m%dT%H:%M:%S", $timet - date('Z'));
36
            }
37
        }
38
39 19
        return $t;
40
    }
41
42
    /**
43
     * Given an ISO8601 date string, return a timet in the localtime, or UTC.
44
     *
45
     * @param string $idate
46
     * @param int $utc either 0 or 1
47
     *
48
     * @return int (datetime)
49
     */
50
    public static function iso8601Decode($idate, $utc = 0)
51
    {
52
        $t = 0;
53
        if (preg_match('/([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})/', $idate, $regs)) {
54
            if ($utc) {
55
                $t = gmmktime($regs[4], $regs[5], $regs[6], $regs[2], $regs[3], $regs[1]);
56
            } else {
57
                $t = mktime($regs[4], $regs[5], $regs[6], $regs[2], $regs[3], $regs[1]);
58
            }
59
        }
60
61
        return $t;
62
    }
63
}
64