Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-84413

timestamp_to_date_string throws exception on false input

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not a bug
    • Icon: Minor Minor
    • None
    • 4.5
    • Calendar
    • None
    • MOODLE_405_STABLE

      The error is

      Failed to parse time string (@) at position 0 (@): Unexpected character
      

      The relevant lines are

      calendar/type/gregorian/classes/structure.php
       
              if (is_string($time) && !is_numeric($time)) {
                  debugging(
                      "Invalid time passed to timestamp_to_date_string: '\{$time}'",
                      DEBUG_DEVELOPER,
                  );
                  $time = 0;
              }
       
              if ($time === null || $time === '') {
                  $time = 0;
              }
       
              $time = new \DateTime("@\{$time}", new \DateTimeZone(date_default_timezone_get()));
      

      which were introduced in commit cdbc789fd8182f4392a3ed8e73169bfc9176375f

      This error can be reproduced by adding an entry to the data provider for test_timestamp_to_date_string and adjusting the datatype of $timestamp to not require int

      diff --git a/calendar/type/gregorian/tests/structure_test.php b/calendar/type/gregorian/tests/structure_test.php
      index dc917ad8a1e..f4cd627b110 100644
      --- a/calendar/type/gregorian/tests/structure_test.php
      +++ b/calendar/type/gregorian/tests/structure_test.php
      @@ -46,7 +46,7 @@ final class structure_test extends \advanced_testcase {
            */
           public function test_timestamp_to_date_string(
               string $locale,
      -        int $timestamp,
      +        $timestamp,
               string $format,
               string $timezone,
               bool $fixday,
      @@ -188,6 +188,15 @@ final class structure_test extends \advanced_testcase {
                       true,
                       " 2:16:43",
                   ],
      +            'Time with false input' => [
      +                'en',
      +                false,
      +                '%Y-%m-%d %H:%M:%S',
      +                'UTC',
      +                false,
      +                false,
      +                '1970-01-01 00:00:00',
      +            ],
               ];
           }
       }
      

      Shown below:

      root@9f322656fff8:/var/www/html# php vendor/bin/phpunit calendar/type/gregorian/tests/structure_test.php
      Moodle 4.5.1+ (Build: 20250131), 89e6f737ccc2a426d28fe8a73ec7c3d3feeaf604
      Php: 8.3.16, pgsql: 14.7 (Debian 14.7-1.pgdg110+1), OS: Linux 5.15.0-131-generic x86_64
      PHPUnit 9.6.18 by Sebastian Bergmann and contributors.
       
      ............E                                                     13 / 13 (100%)
       
      Time: 00:01.543, Memory: 64.50 MB
       
      There was 1 error:
       
      1) calendartype_gregorian\structure_test::test_timestamp_to_date_string with data set "Time with false input" ('en', false, '%Y-%m-%d %H:%M:%S', 'UTC', false, false, '1970-01-01 00:00:00')
      DateMalformedStringException: Failed to parse time string (@) at position 0 (@): Unexpected character
       
      /var/www/html/calendar/type/gregorian/classes/structure.php:338
      /var/www/html/calendar/type/gregorian/tests/structure_test.php:64
      /var/www/html/lib/phpunit/classes/advanced_testcase.php:76
       
      ERRORS!
      Tests: 13, Assertions: 12, Errors: 1.
      

      Suggested fix:

      diff --git a/calendar/type/gregorian/classes/structure.php b/calendar/type/gregorian/classes/structure.php
      index 7680a874afd..d38cea13af5 100644
      --- a/calendar/type/gregorian/classes/structure.php
      +++ b/calendar/type/gregorian/classes/structure.php
      @@ -331,7 +331,7 @@ class structure extends type_base {
                   $time = 0;
               }
       
      -        if ($time === null || $time === '') {
      +        if (empty($time)) {
                   $time = 0;
               }
      

      Resulting in a passed test:

      root@9f322656fff8:/var/www/html# php vendor/bin/phpunit calendar/type/gregorian/tests/structure_test.php
      Moodle 4.5.1+ (Build: 20250131), 89e6f737ccc2a426d28fe8a73ec7c3d3feeaf604
      Php: 8.3.16, pgsql: 14.7 (Debian 14.7-1.pgdg110+1), OS: Linux 5.15.0-131-generic x86_64
      PHPUnit 9.6.18 by Sebastian Bergmann and contributors.
       
      .............                                                     13 / 13 (100%)
       
      Time: 00:01.512, Memory: 64.50 MB
       
      OK (13 tests, 13 assertions)
      

            Unassigned Unassigned
            sashaanastasi1 SashaAnastasi
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved:

                Error rendering 'clockify-timesheets-time-tracking-reports:timer-sidebar'. Please contact your Jira administrators.