diff --git a/lib/classes/date.php b/lib/classes/date.php index 6c97e05f45a..5338f023492 100644 --- a/lib/classes/date.php +++ b/lib/classes/date.php @@ -717,10 +717,10 @@ class core_date { // 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); diff --git a/lib/tests/date_test.php b/lib/tests/date_test.php index 74060fbe07e..c7dc89caef4 100644 --- a/lib/tests/date_test.php +++ b/lib/tests/date_test.php @@ -614,4 +614,65 @@ class date_test extends advanced_testcase { $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)); + } }