Skip to content

Commit 2accd78

Browse files
authored
Merge pull request #6461 from kenjis/fix-Time-toString
fix: make Time::__toString() database-compatible on any locale
2 parents 31ae195 + 6e1dd7f commit 2accd78

File tree

6 files changed

+65
-11
lines changed

6 files changed

+65
-11
lines changed

system/I18n/Time.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,12 +1122,11 @@ protected static function hasRelativeKeywords(string $time): bool
11221122

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

11331132
/**

tests/system/I18n/TimeTest.php

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Config\App;
1818
use DateTime;
1919
use DateTimeZone;
20+
use Generator;
2021
use IntlDateFormatter;
2122
use Locale;
2223

@@ -56,7 +57,7 @@ public function testNewTimeNow()
5657
);
5758

5859
$time = new Time('', 'America/Chicago');
59-
$this->assertSame($formatter->format($time), (string) $time);
60+
$this->assertSame($formatter->format($time), $time->toDateTimeString());
6061
}
6162

6263
public function testTimeWithTimezone()
@@ -72,7 +73,7 @@ public function testTimeWithTimezone()
7273

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

75-
$this->assertSame($formatter->format($time), (string) $time);
76+
$this->assertSame($formatter->format($time), $time->toDateTimeString());
7677
}
7778

7879
public function testTimeWithTimezoneAndLocale()
@@ -88,7 +89,7 @@ public function testTimeWithTimezoneAndLocale()
8889

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

91-
$this->assertSame($formatter->format($time), (string) $time);
92+
$this->assertSame($formatter->format($time), $time->toDateTimeString());
9293
}
9394

9495
public function testTimeWithDateTimeZone()
@@ -104,7 +105,7 @@ public function testTimeWithDateTimeZone()
104105

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

107-
$this->assertSame($formatter->format($time), (string) $time);
108+
$this->assertSame($formatter->format($time), $time->toDateTimeString());
108109
}
109110

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

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

@@ -1128,15 +1128,34 @@ public function testUnserializeTimeObject()
11281128

11291129
public function testSetTestNowWithFaLocale()
11301130
{
1131-
$currentLocale = Locale::getDefault();
11321131
Locale::setDefault('fa');
11331132

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

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

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

1140-
Locale::setDefault($currentLocale);
1140+
/**
1141+
* @dataProvider provideLocales
1142+
*/
1143+
public function testToStringDoesNotDependOnLocale(string $locale)
1144+
{
1145+
Locale::setDefault($locale);
1146+
1147+
$time = new Time('2017/03/10 12:00');
1148+
1149+
$this->assertSame('2017-03-10 12:00:00', (string) $time);
1150+
}
1151+
1152+
public function provideLocales(): Generator
1153+
{
1154+
yield from [
1155+
['en'],
1156+
['de'],
1157+
['ar'],
1158+
['fa'],
1159+
];
11411160
}
11421161
}

user_guide_src/source/changelogs/v4.2.7.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Release Date: Unreleased
1212
BREAKING
1313
********
1414

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

1717
Enhancements
1818
************
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#############################
2+
Upgrading from 4.2.6 to 4.2.7
3+
#############################
4+
5+
Please refer to the upgrade instructions corresponding to your installation method.
6+
7+
- :ref:`Composer Installation App Starter Upgrading <app-starter-upgrading>`
8+
- :ref:`Composer Installation Adding CodeIgniter4 to an Existing Project Upgrading <adding-codeigniter4-upgrading>`
9+
- :ref:`Manual Installation Upgrading <installing-manual-upgrading>`
10+
11+
.. contents::
12+
:local:
13+
:depth: 2
14+
15+
Breaking Changes
16+
****************
17+
18+
- ``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.
19+
20+
Project Files
21+
*************
22+
23+
A few files in the **project space** (root, app, public, writable) received cosmetic updates.
24+
You need not touch these files at all. There are some third-party CodeIgniter modules available
25+
to assist with merging changes to the project space: `Explore on Packagist <https://packagist.org/explore/?query=codeigniter4%20updates>`_.
26+
27+
All Changes
28+
===========
29+
30+
This is a list of all files in the **project space** that received changes;
31+
many will be simple comments or formatting that have no effect on the runtime:
32+
33+
*

user_guide_src/source/installation/upgrading.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ See also :doc:`./backward_compatibility_notes`.
1616

1717
backward_compatibility_notes
1818

19+
upgrade_427
1920
upgrade_426
2021
upgrade_425
2122
upgrade_423

user_guide_src/source/libraries/time.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ A full listing of values can be found `here <https://unicode-org.github.io/icu-d
149149

150150
.. literalinclude:: time/015.php
151151

152+
.. _time-todatetimestring:
153+
152154
toDateTimeString()
153155
==================
154156

0 commit comments

Comments
 (0)