/** * Parses given date/time of given timezone and converts to UTC. * If no timezone is given, a UTC date is expected. * * @param string $date_ * @param \Components\Timezone $timezone_ * * @return \Components\Date */ public static function parse($date_, Timezone $timezone_ = null, $pattern_ = null) { $parsed = @date_parse($date_); if (null === $timezone_) { if (isset($parsed['zone'])) { $timezone_ = Timezone::forOffset($parsed['zone'] / 60); } } if (null === $timezone_) { $timezone_ = Timezone::utc(); } if (null === $pattern_) { $pattern_ = I18n::translate('common/datetime/pattern/parse'); } if (isset($parsed['errors_count']) && 0 < (int) $parsed['errors_count']) { $date = \DateTime::createFromFormat($pattern_, $date_, $timezone_->internal()); } else { extract($parsed); $date = new \DateTime(sprintf('%1$d-%2$d-%3$d %4$d:%5$d:%6$d', $year, $month, $day, $hour, $minute, $second), $timezone_->internal()); } if (!$date) { throw new Exception_IllegalArgument('type/date', 'Unable to parse given date. Try specifying a matching pattern.'); } $date->setTimezone(Timezone::utc()->internal()); return new static($date); }