Skip to content

Commit

Permalink
Merge pull request #15 from flack/issue14
Browse files Browse the repository at this point in the history
Use php's datetime for parsing instead of homebrewed regexes
  • Loading branch information
flack committed Apr 17, 2023
2 parents eb35800 + 23e7e2f commit d07a9f5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 62 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
}
],
"require": {
"php": ">=5.0"
"php": ">=5.2"
},
"autoload": {
"classmap": ["lib"],
Expand Down
68 changes: 7 additions & 61 deletions lib/Element/FeedDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class FeedDate

/**
* Creates a new instance of FeedDate representing a given date.
* Accepts RFC 822, ISO 8601 date formats as well as unix time stamps.
* Accepts RFC 822, ISO 8601 date formats (or anything that PHP's DateTime
* can parse, really) as well as unix time stamps.
*
* @param mixed $dateString optional the date this FeedDate will represent. If not specified, the current date and
* time is used.
Expand All @@ -23,68 +24,13 @@ public function __construct($dateString = "")

if (is_integer($dateString)) {
$this->unix = $dateString;

return;
}
$tzOffset = 0;
if (preg_match(
"~(?:(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun),\\s+)?(\\d{1,2})\\s+([a-zA-Z]{3})\\s+(\\d{4})\\s+(\\d{2}):(\\d{2}):(\\d{2})\\s+(.*)~",
$dateString,
$matches
)) {
$months = Array(
"Jan" => 1,
"Feb" => 2,
"Mar" => 3,
"Apr" => 4,
"May" => 5,
"Jun" => 6,
"Jul" => 7,
"Aug" => 8,
"Sep" => 9,
"Oct" => 10,
"Nov" => 11,
"Dec" => 12,
);
$this->unix = mktime($matches[4], $matches[5], $matches[6], $months[$matches[2]], $matches[1], $matches[3]);
if (substr($matches[7], 0, 1) == '+' OR substr($matches[7], 0, 1) == '-') {
$tzOffset = (((int)substr($matches[7], 0, 3) * 60) + (int)substr($matches[7], -2)) * 60;
} else {
if (strlen($matches[7]) == 1) {
$oneHour = 3600;
$ord = ord($matches[7]);
if ($ord < ord("M")) {
$tzOffset = (ord("A") - $ord - 1) * $oneHour;
} elseif ($ord >= ord("M") AND $matches[7] != "Z") {
$tzOffset = ($ord - ord("M")) * $oneHour;
} elseif ($matches[7] == "Z") {
$tzOffset = 0;
}
}
switch ($matches[7]) {
case "UT":
case "GMT":
$tzOffset = 0;
}
}
$this->unix += $tzOffset;

return;
}
if (preg_match("~(\\d{4})-(\\d{2})-(\\d{2})T(\\d{2}):(\\d{2}):(\\d{2})(.*)~", $dateString, $matches)) {
$this->unix = mktime($matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1]);
if (substr($matches[7], 0, 1) == '+' OR substr($matches[7], 0, 1) == '-') {
$tzOffset = (((int)substr($matches[7], 0, 3) * 60) + (int)substr($matches[7], -2)) * 60;
} else {
if ($matches[7] == "Z") {
$tzOffset = 0;
}
} else {
try {
$this->unix = (int) (new Datetime($dateString))->format('U');
} catch (Exception $e) {
$this->unix = 0;
}
$this->unix += $tzOffset;

return;
}
$this->unix = 0;
}

/**
Expand Down

0 comments on commit d07a9f5

Please sign in to comment.