Passed
Push — master ( 1a96d2...6722ee )
by Gaetano
04:55
created

Date   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 85.71%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 16
c 2
b 0
f 0
dl 0
loc 55
ccs 12
cts 14
cp 0.8571
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A iso8601Encode() 0 15 3
A iso8601Decode() 0 12 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 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 21
    public static function iso8601Encode($timet, $utc = 0)
24
    {
25 21
        if (!$utc) {
26 21
            $t = strftime("%Y%m%dT%H:%M:%S", $timet);
27
        } else {
28 1
            if (function_exists('gmstrftime')) {
29
                // gmstrftime doesn't exist in some versions
30
                // of PHP
31 1
                $t = gmstrftime("%Y%m%dT%H:%M:%S", $timet);
32
            } else {
33
                $t = strftime("%Y%m%dT%H:%M:%S", $timet - date('Z'));
34
            }
35
        }
36
37 21
        return $t;
38
    }
39
40
    /**
41
     * Given an ISO8601 date string, return a timet in the localtime, or UTC.
42
     *
43
     * @param string $idate
44
     * @param int $utc either 0 or 1
45
     *
46
     * @return int (datetime)
47
     */
48 1
    public static function iso8601Decode($idate, $utc = 0)
49
    {
50 1
        $t = 0;
51 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)) {
52 1
            if ($utc) {
53
                $t = gmmktime($regs[4], $regs[5], $regs[6], $regs[2], $regs[3], $regs[1]);
54
            } else {
55 1
                $t = mktime($regs[4], $regs[5], $regs[6], $regs[2], $regs[3], $regs[1]);
56
            }
57
        }
58
59 1
        return $t;
60
    }
61
}
62