Completed
Branch BUG-11108-ticket-reserved-coun... (144d27)
by
unknown
14:21 queued 17s
created

convertWpGmtOffsetForDateTimeZone()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 4
nop 1
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace EventEspresso\core\services\helpers\datetime;
3
4
use DateTime;
5
use DateTimeZone;
6
use DomainException;
7
use EE_Datetime_Field;
8
9
class PhpCompatGreaterFiveSixHelper extends AbstractHelper
10
{
11
    /**
12
     * PhpCompatLessFiveSixHelper constructor.
13
     *
14
     * @throws DomainException
15
     */
16 View Code Duplication
    public function __construct()
17
    {
18
        if (PHP_VERSION_ID < 50600) {
19
            throw new DomainException(
20
                sprintf(
21
                    esc_html__(
22
                        'The %1$s is only usable on php versions greater than 5.6.  You\'ll want to use %2$s instead.',
23
                        'event_espresso'
24
                    ),
25
                    __CLASS__,
26
                    'EventEspresso\core\services\helpers\datetime\PhpCompatLessFiveSixHelper'
27
                )
28
            );
29
        }
30
    }
31
32
    /**
33
     * Returns a timezone string for the provided gmt_offset.
34
     * This is a valid timezone string that can be sent into DateTimeZone
35
     *
36
     * @param float|string $gmt_offset
37
     * @return string
38
     */
39
    public function getTimezoneStringFromGmtOffset($gmt_offset = '')
40
    {
41
        $gmt_offset_or_timezone_string = $this->sanitizeInitialIncomingGmtOffsetForGettingTimezoneString($gmt_offset);
42
        return is_float($gmt_offset_or_timezone_string)
43
            ? $this->convertWpGmtOffsetForDateTimeZone($gmt_offset_or_timezone_string)
44
            : $gmt_offset_or_timezone_string;
45
    }
46
47
48
49
    /**
50
     * Returns a formatted offset for use as an argument for constructing DateTimeZone
51
     * @param float $gmt_offset This should be a float representing the gmt_offset.
52
     * @return string
53
     */
54
    protected function convertWpGmtOffsetForDateTimeZone($gmt_offset)
55
    {
56
        $gmt_offset = (float) $gmt_offset;
57
        $is_negative = $gmt_offset < 0;
58
        $gmt_offset *= 100;
59
        $gmt_offset = absint($gmt_offset);
60
        //negative and need zero padding?
61
        if (strlen($gmt_offset) < 4) {
62
            $gmt_offset = str_pad($gmt_offset, 4, '0', STR_PAD_LEFT);
63
        }
64
        $gmt_offset = $this->convertToTimeFraction($gmt_offset);
65
        //return something like -1300, -0200 or +1300, +0200
66
        return $is_negative ? '-' . $gmt_offset : '+' . $gmt_offset;
67
    }
68
69
70
    /**
71
     * Converts something like `1550` to `1530` or `0275` to `0245`
72
     * Incoming offset should be a positive value, this will mutate negative values. Be aware!
73
     * @param int $offset
74
     * @return mixed
75
     */
76
    protected function convertToTimeFraction($offset)
77
    {
78
        $first_part = substr($offset, 0, 2);
79
        $second_part = substr($offset, 2, 2);
80
        $second_part = str_replace(array('25', '50', '75'), array('15', '30', '45'), $second_part);
81
        return $first_part . $second_part;
82
    }
83
84
85
    /**
86
     * Get Timezone offset for given timezone object
87
     *
88
     * @param DateTimeZone $date_time_zone
89
     * @param null|int     $time
90
     * @return int
91
     */
92
    public function getTimezoneOffset(DateTimezone $date_time_zone, $time = NULL)
93
    {
94
        $time = is_int($time) || $time === null ? $time : (int) strtotime($time);
95
        $time = preg_match(EE_Datetime_Field::unix_timestamp_regex, $time) ? $time : time();
96
        return $date_time_zone->getOffset(new DateTime('@' . $time));
97
    }
98
}
99