Skip to content

Commit

Permalink
Be more careful when parsing times with timezones.
Browse files Browse the repository at this point in the history
Consider the timezone abbreviation "IST" for Asia/Kolkata.
Unfortuately, the PHP strtotime() function, when it encounters
a timezone abbreviation, uses the first matching timezone
definition in the timezone database, which for IST is
Asia/Jerusalem!, rather than preferring the timezone that equals
the current timezone, if there is one.

And abbreviations like GMT-5 (for Etc/GMT-5) aren't even parsed.

Work around this with regular expressions.

Reported by Aakash Sharma.
  • Loading branch information
kohler committed Feb 27, 2014
1 parent 3e377be commit 913aad6
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
4 changes: 2 additions & 2 deletions log.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@
if ($_REQUEST["date"] == "")
$_REQUEST["date"] = "now";
if ($_REQUEST["date"] != "now" && isset($_REQUEST["search"]))
if (($firstDate = strtotime($_REQUEST["date"])) === false) {
$Conf->errorMsg("\"" . htmlspecialchars($_REQUEST["date"]) . "\" is not a valid date.");
if (($firstDate = $Conf->parse_time($_REQUEST["date"])) === false) {
$Conf->errorMsg("" . htmlspecialchars($_REQUEST["date"]) . " is not a valid date.");
$Eclass["date"] = " error";
}

Expand Down
2 changes: 1 addition & 1 deletion settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ function parseValue($name, $type) {
else if ($type === "date" || $type === "cdate") {
if ($v == "" || strtoupper($v) == "N/A" || $v == "0")
return -1;
else if (($v = strtotime($v)) !== false)
else if (($v = $Conf->parse_time($v)) !== false)
return $v;
else
$err = $SettingText[$name] . ": invalid date.";
Expand Down
23 changes: 23 additions & 0 deletions src/conference.php
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,29 @@ function parseableTime($value, $include_zone) {
}
return $d;
}
function parse_time($d, $reference = null) {
global $Now, $Opt;
if ($reference === null)
$reference = $Now;
if (!isset($Opt["dateFormatTimezoneRemover"])
&& function_exists("timezone_abbreviations_list")) {
$mytz = date_default_timezone_get();
$x = array();
foreach (timezone_abbreviations_list() as $tzname => $tzinfo) {
foreach ($tzinfo as $tz)
if ($tz["timezone_id"] == $mytz)
$x[] = preg_quote($tzname);
}
if (count($x) == 0)
$x[] = preg_quote(date("T", $reference));
$Opt["dateFormatTimezoneRemover"] =
"/(?:\\s|\\A)(?:" . join("|", $x) . ")(?:\\s|\\z)/i";
}
if (@$Opt["dateFormatTimezoneRemover"])
$d = preg_replace($Opt["dateFormatTimezoneRemover"], " ", $d);
return strtotime($d, $reference);
}

function _printableTime($value, $long, $useradjust, $preadjust = null) {
global $Opt;
if ($value <= 0)
Expand Down
2 changes: 1 addition & 1 deletion src/paperstatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ function normalize($pj, $old_pj) {
if (is_numeric($pj->$k))
$pj->$k = (int) $pj->$k;
else if (is_string($pj->$k))
$pj->$k = strtotime($pj->$k, $Now);
$pj->$k = $Conf->parse_time($pj->$k, $Now);
else
$pj->$k = false;
if ($pj->$k === false || $pj->$k < 0)
Expand Down

0 comments on commit 913aad6

Please sign in to comment.