Skip to content

fix: make Time::__toString() database-compatible on any locale #6461

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions system/I18n/Time.php
Original file line number Diff line number Diff line change
Expand Up @@ -1122,12 +1122,11 @@ protected static function hasRelativeKeywords(string $time): bool

/**
* Outputs a short format version of the datetime.
*
* @throws Exception
* The output is NOT localized intentionally.
*/
public function __toString(): string
{
return IntlDateFormatter::formatObject($this->toDateTime(), $this->toStringFormat, $this->locale);
return $this->format('Y-m-d H:i:s');
}

/**
Expand Down
33 changes: 26 additions & 7 deletions tests/system/I18n/TimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Config\App;
use DateTime;
use DateTimeZone;
use Generator;
use IntlDateFormatter;
use Locale;

Expand Down Expand Up @@ -56,7 +57,7 @@ public function testNewTimeNow()
);

$time = new Time('', 'America/Chicago');
$this->assertSame($formatter->format($time), (string) $time);
$this->assertSame($formatter->format($time), $time->toDateTimeString());
}

public function testTimeWithTimezone()
Expand All @@ -72,7 +73,7 @@ public function testTimeWithTimezone()

$time = new Time('now', 'Europe/London');

$this->assertSame($formatter->format($time), (string) $time);
$this->assertSame($formatter->format($time), $time->toDateTimeString());
}

public function testTimeWithTimezoneAndLocale()
Expand All @@ -88,7 +89,7 @@ public function testTimeWithTimezoneAndLocale()

$time = new Time('now', 'Europe/London', 'fr_FR');

$this->assertSame($formatter->format($time), (string) $time);
$this->assertSame($formatter->format($time), $time->toDateTimeString());
}

public function testTimeWithDateTimeZone()
Expand All @@ -104,7 +105,7 @@ public function testTimeWithDateTimeZone()

$time = new Time('now', new DateTimeZone('Europe/London'), 'fr_FR');

$this->assertSame($formatter->format($time), (string) $time);
$this->assertSame($formatter->format($time), $time->toDateTimeString());
}

public function testToDateTime()
Expand Down Expand Up @@ -138,7 +139,6 @@ public function testToDateTimeString()
{
$time = Time::parse('2017-01-12 00:00', 'America/Chicago');

$this->assertSame('2017-01-12 00:00:00', (string) $time);
$this->assertSame('2017-01-12 00:00:00', $time->toDateTimeString());
}

Expand Down Expand Up @@ -1128,15 +1128,34 @@ public function testUnserializeTimeObject()

public function testSetTestNowWithFaLocale()
{
$currentLocale = Locale::getDefault();
Locale::setDefault('fa');

Time::setTestNow('2017/03/10 12:00', 'Asia/Tokyo');

$now = Time::now()->format('c');

$this->assertSame('2017-03-10T12:00:00+09:00', $now);
}

Locale::setDefault($currentLocale);
/**
* @dataProvider provideLocales
*/
public function testToStringDoesNotDependOnLocale(string $locale)
{
Locale::setDefault($locale);

$time = new Time('2017/03/10 12:00');

$this->assertSame('2017-03-10 12:00:00', (string) $time);
}

public function provideLocales(): Generator
{
yield from [
['en'],
['de'],
['ar'],
['fa'],
];
}
}
2 changes: 1 addition & 1 deletion user_guide_src/source/changelogs/v4.2.7.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Release Date: Unreleased
BREAKING
********

none.
- ``Time::__toString()`` is now locale-independent. It returns database-compatible strings like '2022-09-07 12:00:00' in any locale.

Enhancements
************
Expand Down
33 changes: 33 additions & 0 deletions user_guide_src/source/installation/upgrade_427.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#############################
Upgrading from 4.2.6 to 4.2.7
#############################

Please refer to the upgrade instructions corresponding to your installation method.

- :ref:`Composer Installation App Starter Upgrading <app-starter-upgrading>`
- :ref:`Composer Installation Adding CodeIgniter4 to an Existing Project Upgrading <adding-codeigniter4-upgrading>`
- :ref:`Manual Installation Upgrading <installing-manual-upgrading>`

.. contents::
:local:
:depth: 2

Breaking Changes
****************

- ``Time::__toString()`` is now locale-independent. It returns database-compatible strings like '2022-09-07 12:00:00' in any locale. Most locales are not affected by this change. But in a few locales like `ar`, `fa`, ``Time::__toString()`` (or ``(string) $time`` or implicit casting to a string) no longer returns a localized datetime string. if you want to get a localized datetime string, use :ref:`Time::toDateTimeString() <time-todatetimestring>` instead.

Project Files
*************

A few files in the **project space** (root, app, public, writable) received cosmetic updates.
You need not touch these files at all. There are some third-party CodeIgniter modules available
to assist with merging changes to the project space: `Explore on Packagist <https://packagist.org/explore/?query=codeigniter4%20updates>`_.

All Changes
===========

This is a list of all files in the **project space** that received changes;
many will be simple comments or formatting that have no effect on the runtime:

*
1 change: 1 addition & 0 deletions user_guide_src/source/installation/upgrading.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ See also :doc:`./backward_compatibility_notes`.

backward_compatibility_notes

upgrade_427
upgrade_426
upgrade_425
upgrade_423
Expand Down
2 changes: 2 additions & 0 deletions user_guide_src/source/libraries/time.rst
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ A full listing of values can be found `here <https://unicode-org.github.io/icu-d

.. literalinclude:: time/015.php

.. _time-todatetimestring:

toDateTimeString()
==================

Expand Down