Merge branch 'MDL-78811-Master' of https://github.com/aydevworks/moodle
[moodle.git] / lib / phpxmlrpc / Helper / Date.php
blobad4c3d51c7c022bbdf6bcfcf8c4c16fa3ebf2a14
1 <?php
3 namespace PhpXmlRpc\Helper;
5 use PhpXmlRpc\PhpXmlRpc;
7 /**
8 * Helps to convert timestamps to the xml-rpc date format.
10 * Feature creep -- add support for custom TZs
12 class Date
14 /**
15 * Given a timestamp, return the corresponding ISO8601 encoded string.
17 * Really, timezones ought to be supported but the XML-RPC spec says:
19 * "Don't assume a timezone. It should be specified by the server in its documentation what assumptions it makes
20 * about timezones."
22 * This routine always encodes to local time unless $utc is set to 1, in which case UTC output is produced and an
23 * adjustment for the local timezone's offset is made
25 * @param int|\DateTimeInterface $timet timestamp or datetime
26 * @param bool|int $utc (0 or 1)
27 * @return string
29 public static function iso8601Encode($timet, $utc = 0)
31 if (is_a($timet, 'DateTimeInterface') || is_a($timet, 'DateTime')) {
32 $timet = $timet->getTimestamp();
34 if (!$utc) {
35 $t = date('Ymd\TH:i:s', $timet);
36 } else {
37 $t = gmdate('Ymd\TH:i:s', $timet);
40 return $t;
43 /**
44 * Given an ISO8601 date string, return a timestamp in the localtime, or UTC.
46 * @param string $idate
47 * @param bool|int $utc either 0 (assume date is in local time) or 1 (assume date is in UTC)
49 * @return int (timestamp) 0 if the source string does not match the xml-rpc dateTime format
51 public static function iso8601Decode($idate, $utc = 0)
53 $t = 0;
54 if (preg_match(PhpXmlRpc::$xmlrpc_datetime_format, $idate, $regs)) {
55 if ($utc) {
56 $t = gmmktime($regs[4], $regs[5], $regs[6], $regs[2], $regs[3], $regs[1]);
57 } else {
58 $t = mktime($regs[4], $regs[5], $regs[6], $regs[2], $regs[3], $regs[1]);
62 return $t;