Skip to content

Commit

Permalink
MDL-81074 core: align core_date::strftime results for numeric and string
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaboesch authored and andrewnicols committed Mar 25, 2024
1 parent 1c42dea commit aa29193
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/classes/date.php
Expand Up @@ -717,10 +717,10 @@ public static function strftime(string $format, $timestamp = null, ?string $loca
// Windows format.
$locale = $locale ?: get_string('locale', 'langconfig');

// The following code is taken from https://github.com/alphp/strftime without modifications.
// The following code is taken from https://github.com/alphp/strftime.
// phpcs:disable
if (!($timestamp instanceof DateTimeInterface)) {
$timestamp = is_int($timestamp) ? '@' . $timestamp : (string) $timestamp;
$timestamp = is_numeric($timestamp) ? '@' . $timestamp : (string) $timestamp;

try {
$timestamp = new DateTime($timestamp);
Expand Down
61 changes: 61 additions & 0 deletions lib/tests/date_test.php
Expand Up @@ -614,4 +614,65 @@ public function test_get_user_timezone_object() {
$this->assertSame($zone, $tz->getName());
}
}

/**
* Data provider for the values for test_core_strftime().
*
* @return array
*/
public static function get_strftime_provider(): array {
return [
'string_c' => [
"1708405742",
null,
"20 February 2024 at 6:09 pm",
],
'numeric_c' => [
1708405742,
null,
"20 February 2024 at 6:09 pm",
],
'string_strftimedatetime' => [
"1708405742",
"strftimedatetime",
"20 February 2024, 06:09 PM",
],
'numeric_strftimedatetime' => [
1708405742,
"strftimedatetime",
"20 February 2024, 06:09 PM",
],
'string_strftimedatetimeshortaccurate' => [
"1708405742",
"strftimedatetimeshortaccurate",
"20/02/24, 18:09:02",
],
'numeric_strftimedatetimeshortaccurate' => [
1708405742,
"strftimedatetimeshortaccurate",
"20/02/24, 18:09:02",
],
];
}

/**
* Test \core_date::strftime function.
*
* @dataProvider get_strftime_provider
* @param mixed $input Input passed to strftime
* @param string|null $format The date format to pass to strftime, falls back to '%c' if null
* @param string $expected The output generated by strftime
*
* @covers \core_date::strftime
*/
public function test_core_strftime(mixed $input, string|null $format, string $expected): void {
$this->resetAfterTest();
$this->setTimezone('Pacific/Auckland', 'Pacific/Auckland');
if (!$format) {
$format = "%c";
} else {
$format = get_string($format, 'langconfig');
}
$this->assertSame($expected, core_date::strftime($format, $input));
}
}

0 comments on commit aa29193

Please sign in to comment.