Exceptions
3 years ago
Lang
3 years ago
List
3 years ago
Traits
3 years ago
AbstractTranslator.php
3 years ago
Carbon.php
4 years ago
CarbonConverterInterface.php
4 years ago
CarbonImmutable.php
4 years ago
CarbonInterface.php
3 years ago
CarbonInterval.php
3 years ago
CarbonPeriod.php
3 years ago
CarbonTimeZone.php
3 years ago
Factory.php
4 years ago
FactoryImmutable.php
4 years ago
Language.php
4 years ago
Translator.php
4 years ago
TranslatorImmutable.php
4 years ago
TranslatorStrongTypeInterface.php
4 years ago
index.php
3 years ago
CarbonTimeZone.php
158 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Carbon; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use MailPoetVendor\Carbon\Exceptions\InvalidCastException; |
| 5 | use MailPoetVendor\Carbon\Exceptions\InvalidTimeZoneException; |
| 6 | use DateTimeInterface; |
| 7 | use DateTimeZone; |
| 8 | use Throwable; |
| 9 | class CarbonTimeZone extends DateTimeZone |
| 10 | { |
| 11 | public function __construct($timezone = null) |
| 12 | { |
| 13 | parent::__construct(static::getDateTimeZoneNameFromMixed($timezone)); |
| 14 | } |
| 15 | protected static function parseNumericTimezone($timezone) |
| 16 | { |
| 17 | if ($timezone <= -100 || $timezone >= 100) { |
| 18 | throw new InvalidTimeZoneException('Absolute timezone offset cannot be greater than 100.'); |
| 19 | } |
| 20 | return ($timezone >= 0 ? '+' : '') . \ltrim($timezone, '+') . ':00'; |
| 21 | } |
| 22 | protected static function getDateTimeZoneNameFromMixed($timezone) |
| 23 | { |
| 24 | if ($timezone === null) { |
| 25 | return \date_default_timezone_get(); |
| 26 | } |
| 27 | if (\is_string($timezone)) { |
| 28 | $timezone = \preg_replace('/^\\s*([+-]\\d+)(\\d{2})\\s*$/', '$1:$2', $timezone); |
| 29 | } |
| 30 | if (\is_numeric($timezone)) { |
| 31 | return static::parseNumericTimezone($timezone); |
| 32 | } |
| 33 | return $timezone; |
| 34 | } |
| 35 | protected static function getDateTimeZoneFromName(&$name) |
| 36 | { |
| 37 | return @\timezone_open($name = (string) static::getDateTimeZoneNameFromMixed($name)); |
| 38 | } |
| 39 | public function cast(string $className) |
| 40 | { |
| 41 | if (!\method_exists($className, 'instance')) { |
| 42 | if (\is_a($className, DateTimeZone::class, \true)) { |
| 43 | return new $className($this->getName()); |
| 44 | } |
| 45 | throw new InvalidCastException("{$className} has not the instance() method needed to cast the date."); |
| 46 | } |
| 47 | return $className::instance($this); |
| 48 | } |
| 49 | public static function instance($object = null, $objectDump = null) |
| 50 | { |
| 51 | $tz = $object; |
| 52 | if ($tz instanceof static) { |
| 53 | return $tz; |
| 54 | } |
| 55 | if ($tz === null) { |
| 56 | return new static(); |
| 57 | } |
| 58 | if (!$tz instanceof DateTimeZone) { |
| 59 | $tz = static::getDateTimeZoneFromName($object); |
| 60 | } |
| 61 | if ($tz !== \false) { |
| 62 | return new static($tz->getName()); |
| 63 | } |
| 64 | if (Carbon::isStrictModeEnabled()) { |
| 65 | throw new InvalidTimeZoneException('Unknown or bad timezone (' . ($objectDump ?: $object) . ')'); |
| 66 | } |
| 67 | return \false; |
| 68 | } |
| 69 | public function getAbbreviatedName($dst = \false) |
| 70 | { |
| 71 | $name = $this->getName(); |
| 72 | foreach ($this->listAbbreviations() as $abbreviation => $zones) { |
| 73 | foreach ($zones as $zone) { |
| 74 | if ($zone['timezone_id'] === $name && $zone['dst'] == $dst) { |
| 75 | return $abbreviation; |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | return 'unknown'; |
| 80 | } |
| 81 | public function getAbbr($dst = \false) |
| 82 | { |
| 83 | return $this->getAbbreviatedName($dst); |
| 84 | } |
| 85 | public function toOffsetName(DateTimeInterface $date = null) |
| 86 | { |
| 87 | return static::getOffsetNameFromMinuteOffset($this->getOffset($date ?: Carbon::now($this)) / 60); |
| 88 | } |
| 89 | public function toOffsetTimeZone(DateTimeInterface $date = null) |
| 90 | { |
| 91 | return new static($this->toOffsetName($date)); |
| 92 | } |
| 93 | public function toRegionName(DateTimeInterface $date = null, $isDst = 1) |
| 94 | { |
| 95 | $name = $this->getName(); |
| 96 | $firstChar = \substr($name, 0, 1); |
| 97 | if ($firstChar !== '+' && $firstChar !== '-') { |
| 98 | return $name; |
| 99 | } |
| 100 | $date = $date ?: Carbon::now($this); |
| 101 | // Integer construction no longer supported since PHP 8 |
| 102 | // @codeCoverageIgnoreStart |
| 103 | try { |
| 104 | $offset = @$this->getOffset($date) ?: 0; |
| 105 | } catch (Throwable $e) { |
| 106 | $offset = 0; |
| 107 | } |
| 108 | // @codeCoverageIgnoreEnd |
| 109 | $name = @\timezone_name_from_abbr('', $offset, $isDst); |
| 110 | if ($name) { |
| 111 | return $name; |
| 112 | } |
| 113 | foreach (\timezone_identifiers_list() as $timezone) { |
| 114 | if (Carbon::instance($date)->tz($timezone)->getOffset() === $offset) { |
| 115 | return $timezone; |
| 116 | } |
| 117 | } |
| 118 | return \false; |
| 119 | } |
| 120 | public function toRegionTimeZone(DateTimeInterface $date = null) |
| 121 | { |
| 122 | $tz = $this->toRegionName($date); |
| 123 | if ($tz !== \false) { |
| 124 | return new static($tz); |
| 125 | } |
| 126 | if (Carbon::isStrictModeEnabled()) { |
| 127 | throw new InvalidTimeZoneException('Unknown timezone for offset ' . $this->getOffset($date ?: Carbon::now($this)) . ' seconds.'); |
| 128 | } |
| 129 | return \false; |
| 130 | } |
| 131 | public function __toString() |
| 132 | { |
| 133 | return $this->getName(); |
| 134 | } |
| 135 | public function getType() : int |
| 136 | { |
| 137 | return \preg_match('/"timezone_type";i:(\\d)/', \serialize($this), $match) ? (int) $match[1] : 3; |
| 138 | } |
| 139 | public static function create($object = null) |
| 140 | { |
| 141 | return static::instance($object); |
| 142 | } |
| 143 | public static function createFromHourOffset(float $hourOffset) |
| 144 | { |
| 145 | return static::createFromMinuteOffset($hourOffset * Carbon::MINUTES_PER_HOUR); |
| 146 | } |
| 147 | public static function createFromMinuteOffset(float $minuteOffset) |
| 148 | { |
| 149 | return static::instance(static::getOffsetNameFromMinuteOffset($minuteOffset)); |
| 150 | } |
| 151 | public static function getOffsetNameFromMinuteOffset(float $minutes) : string |
| 152 | { |
| 153 | $minutes = \round($minutes); |
| 154 | $unsignedMinutes = \abs($minutes); |
| 155 | return ($minutes < 0 ? '-' : '+') . \str_pad((string) \floor($unsignedMinutes / 60), 2, '0', \STR_PAD_LEFT) . ':' . \str_pad((string) ($unsignedMinutes % 60), 2, '0', \STR_PAD_LEFT); |
| 156 | } |
| 157 | } |
| 158 |