Skip to content

Commit

Permalink
Throw date exception when date is fictious
Browse files Browse the repository at this point in the history
  • Loading branch information
textagroup committed Sep 10, 2024
1 parent 11af37b commit 7f843ae
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
29 changes: 29 additions & 0 deletions core/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ public static function factory($dateString, $timezone = null)
$date = self::lastMonth();
} elseif (is_string($dateString) && preg_match('/last[ -]?year/i', urldecode($dateString))) {
$date = self::lastYear();
} else if (preg_match('/(\d{4})-(\d{2})-(\d{2})/', urldecode($dateString))) {
if (!self::isDateStringValid($dateString)) {
throw self::getFictiousDateException($dateString);
}
$date = new Date(strtotime($dateString));
} elseif (
!is_int($dateString)
&& (
Expand Down Expand Up @@ -175,6 +180,23 @@ public static function factory($dateString, $timezone = null)
return Date::factory($timestamp);
}

/**
* Returns if the date string is valid
*
* @param $dateString
* @return Boolean
* @ignore
*/
public static function isDateStringValid($dateString)
{
$ret = date_parse($dateString);
if ($ret['warning_count'] || $ret['error_count']) {
return false;
}
return true;
}


/**
* Returns Date w/ UTC timestamp of time $dateString/$timezone.
* (Only applies to special strings, like 'now','today','yesterday','yesterdaySameTime'.
Expand Down Expand Up @@ -1139,6 +1161,13 @@ public static function secondsToDays($secs)
return $secs / self::NUM_SECONDS_IN_DAY;
}

private static function getFictiousDateException($dateString)
{
$message = Piwik::translate('General_ExceptionFictiousDate');
return new Exception($message . ": $dateString");
}


private static function getInvalidDateFormatException($dateString)
{
$message = Piwik::translate('General_ExceptionInvalidDateFormat', array("YYYY-MM-DD, or 'today' or 'yesterday'", "strtotime", "http://php.net/strtotime"));
Expand Down
1 change: 1 addition & 0 deletions lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@
"ExceptionIncompatibleClientServerVersions": "Your %1$s client version is %2$s which is incompatible with server version %3$s.",
"ExceptionInvalidAggregateReportsFormat": "Aggregate reports format '%1$s' not valid. Try any of the following instead: %2$s.",
"ExceptionInvalidArchiveTimeToLive": "Today archive time to live must be a number of seconds greater than zero",
"ExceptionFictiousDate": "This date does not exist.",
"ExceptionInvalidDateBeforeFirstWebsite": "The date '%1$s' is a date before first website was online. Try date that's after %2$s (timestamp %3$s).",
"ExceptionInvalidDateFormat": "Date format must be: %1$s or any keyword supported by the %2$s function (see %3$s for more information)",
"ExceptionInvalidDateRange": "The date '%1$s' is not a correct date range. It should have the following format: %2$s.",
Expand Down
8 changes: 8 additions & 0 deletions tests/PHPUnit/Unit/DateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,14 @@ public function testIsLeapYear()
}
}

public function testIsDateStringValid()
{
$this->assertTrue(Date::isDateStringValid('2024-02-29'));
$this->assertFalse(Date::isDateStringValid('2025-02-29'));
$this->assertFalse(Date::isDateStringValid('2025-09-31'));
$this->assertTrue(Date::isDateStringValid('2025-03-31'));
}


public function getLocalizedLongStrings()
{
Expand Down

0 comments on commit 7f843ae

Please sign in to comment.