cakephp: I18n/Time::timeAgoInWords() produces wrong results in April

This is a (multiple allowed):

  • bug

  • enhancement

  • feature-discussion (RFC)

  • CakePHP Version: 3.5.x

  • Platform and Target: Win, OSX, Linux

What you did

Adde this case for testing the issue I experienced in our app.

    public function testWrongTimeCase()
    {
        Time::setTestNow('2018-04-12 14:00:00');
        $date = new Time('2018-04-11 13:00:00');
        $result = $date->timeAgoInWords();
        $this->assertEquals('1 day ago', $result);
        Time::setTestNow(null);
    }

What happened

1) Cake\Test\TestCase\I18n\TimeTest::testWrongTimeCase
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'1 day ago'
+'5 days'

What you expected to happen

That itโ€™s one day ago. ๐Ÿ˜„

Additional information

This is a problem because the date in entities is returned as a I18n/Time object:

        'start_date' => object(Cake\I18n\Time) {

                'time' => '2012-12-19T14:19:41+00:00',
                'timezone' => 'UTC',
                'fixedNowTime' => false

        },

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 15 (15 by maintainers)

Commits related to this issue

Most upvoted comments

Probably we could. But I am not sure if people expect to get different times when they call FrozenTime::now() and Time::now(). Also, RelativeTimeFormatter can be used for non-Chronos classes such as DateTime, DateTimeImmutable or Carbon. In such cases, we would have to decide which class (Time or FrozenTime) we should use. Though we can decide it, but that feels not intuitive to me.