24 private static $dayNames = [
34 private static $monthsByName = [
49 private static $dayNamesLong = [
80 public static function parse( $dateString ) {
81 $parser =
new self( $dateString );
82 if ( $parser->execute() ) {
83 return $parser->getUnixTime();
96 public static function format( $unixTime ) {
97 return gmdate(
'D, d M Y H:i:s \G\M\T', $unixTime );
105 private function __construct(
$input ) {
114 private function execute() {
117 $this->consumeFixdate();
120 }
catch ( HeaderParserError $e ) {
124 $this->consumeRfc850Date();
127 }
catch ( HeaderParserError $e ) {
131 $this->consumeAsctimeDate();
134 }
catch ( HeaderParserError $e ) {
144 private function consumeFixdate() {
145 $this->consumeDayName();
147 $this->consumeDate1();
149 $this->consumeTimeOfDay();
158 private function consumeDayName() {
159 $next3 = substr( $this->input, $this->pos, 3 );
160 if ( isset( self::$dayNames[$next3] ) ) {
161 $this->dayName = $next3;
164 $this->
error(
'expected day-name' );
173 private function consumeDate1() {
176 $this->consumeMonth();
178 $this->consumeYear();
186 private function consumeDay() {
195 private function consumeMonth() {
196 $next3 = substr( $this->input, $this->pos, 3 );
197 if ( isset( self::$monthsByName[$next3] ) ) {
198 $this->month = self::$monthsByName[$next3];
201 $this->
error(
'expected month' );
210 private function consumeYear() {
218 private function consumeTimeOfDay() {
231 private function consumeRfc850Date() {
232 $this->consumeDayNameLong();
234 $this->consumeDate2();
236 $this->consumeTimeOfDay();
245 private function consumeDate2() {
248 $this->consumeMonth();
252 $currentYear = (int)gmdate(
'Y' );
253 $startOfCentury = (int)round( $currentYear, -2 );
254 $this->year = $startOfCentury + intval( $year );
255 $pivot = $currentYear + 50;
256 if ( $this->year > $pivot ) {
266 private function consumeDayNameLong() {
267 foreach ( self::$dayNamesLong as $dayName ) {
268 if ( substr_compare( $this->input, $dayName, $this->pos, strlen( $dayName ) ) === 0 ) {
269 $this->dayName = substr( $dayName, 0, 3 );
270 $this->pos += strlen( $dayName );
274 $this->
error(
'expected day-name-l' );
282 private function consumeAsctimeDate() {
283 $this->consumeDayName();
285 $this->consumeDate3();
287 $this->consumeTimeOfDay();
289 $this->consumeYear();
297 private function consumeDate3() {
298 $this->consumeMonth();
300 if ( ( $this->input[$this->pos] ??
'' ) ===
' ' ) {
314 private function getUnixTime() {
315 return gmmktime( (
int)$this->hour, (
int)$this->minute, (
int)$this->second,
316 $this->month, (
int)$this->day, $this->year );