Skip to content

Commit 15486dd

Browse files
Merge pull request #62598 from nextcloud/fix/sharing/date-error-messages
fix(Sharing): Improve date error messages
2 parents 54d8458 + eaa2d89 commit 15486dd

4 files changed

Lines changed: 16 additions & 27 deletions

File tree

lib/private/L10N/L10N.php

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -95,28 +95,6 @@ public function n(string $text_singular, string $text_plural, int $count, array
9595
return (string)new L10NString($this, $text_plural, $parameters, $count);
9696
}
9797

98-
/**
99-
* Localization
100-
* @param string $type Type of localization
101-
* @param \DateTime|int|string|null $data parameters for this localization
102-
* @param array $options
103-
* @return string|int|false
104-
*
105-
* Returns the localized data.
106-
*
107-
* Implemented types:
108-
* - date
109-
* - Creates a date
110-
* - params: timestamp (int/string)
111-
* - datetime
112-
* - Creates date and time
113-
* - params: timestamp (int/string)
114-
* - time
115-
* - Creates a time
116-
* - params: timestamp (int/string)
117-
* - firstday: Returns the first day of the week (0 sunday - 6 saturday)
118-
* - jsdate: Returns the short JS date format
119-
*/
12098
#[\Override]
12199
public function l(string $type, $data = null, array $options = []) {
122100
if ($this->locale === null) {
@@ -135,7 +113,7 @@ public function l(string $type, $data = null, array $options = []) {
135113
}
136114

137115
$value = new \DateTime();
138-
if ($data instanceof \DateTime) {
116+
if ($data instanceof \DateTime || $data instanceof \DateTimeImmutable) {
139117
$value = $data;
140118
} elseif (\is_string($data) && !is_numeric($data)) {
141119
$data = strtotime($data);

lib/public/IL10N.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function n(string $text_singular, string $text_plural, int $count, array
5050
/**
5151
* Localization
5252
* @param string $type Type of localization
53-
* @param \DateTime|int|string|null $data parameters for this localization
53+
* @param \DateTime|\DateTimeImmutable|int|string|null $data parameters for this localization
5454
* @param array $options currently supports following options:
5555
* - 'width': handed into \Punic\Calendar::formatDate as second parameter
5656
* @return string|int|false

lib/public/Sharing/Property/ADateSharePropertyType.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,17 @@ public function validateValue(IFactory $l10nFactory, Share $share, string $value
4646
}
4747

4848
if ($date === false) {
49-
return $l10nFactory->get(Application::APP_ID)->t('Invalid ISO date: %s', [$value]);
49+
return $l10nFactory->get(Application::APP_ID)->t('Invalid ISO8601 date: %s', [$value]);
5050
}
5151

5252
if (($minDate = $this->getMinDate($share)) instanceof DateTimeImmutable && $date->diff($minDate)->invert === 0) {
53-
return $l10nFactory->get(Application::APP_ID)->t('Date needs to be after %1$s: %2$s', [$minDate->format(DateTimeInterface::ATOM), $value]);
53+
$l10n = $l10nFactory->get(Application::APP_ID);
54+
return $l10n->t('Date needs to be after %s', [$l10n->l('datetime', $minDate)]);
5455
}
5556

5657
if (($maxDate = $this->getMaxDate($share)) instanceof DateTimeImmutable && $date->diff($maxDate)->invert === 1) {
57-
return $l10nFactory->get(Application::APP_ID)->t('Date needs to be before %1$s: %2$s', [$maxDate->format(DateTimeInterface::ATOM), $value]);
58+
$l10n = $l10nFactory->get(Application::APP_ID);
59+
return $l10n->t('Date needs to be before %s', [$l10n->l('datetime', $maxDate)]);
5860
}
5961

6062
return true;

tests/lib/L10N/L10nTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace Test\L10N;
1010

1111
use DateTime;
12+
use DateTimeImmutable;
1213
use OC\L10N\Factory;
1314
use OC\L10N\L10N;
1415
use OCP\App\IAppManager;
@@ -146,6 +147,14 @@ public static function localizationData(): array {
146147
["11:31:30\xE2\x80\xAFPM GMT+0", 'en', 'en_US', 'time', new DateTime('@1234567890')],
147148
['23:31:30 GMT+0', 'de', 'de_DE', 'time', new DateTime('@1234567890')],
148149

150+
// DateTimeImmutable object
151+
["February 13, 2009, 11:31:30\xE2\x80\xAFPM GMT+0", 'en', 'en_US', 'datetime', new DateTimeImmutable('@1234567890')],
152+
['13. Februar 2009, 23:31:30 GMT+0', 'de', 'de_DE', 'datetime', new DateTimeImmutable('@1234567890')],
153+
['February 13, 2009', 'en', 'en_US', 'date', new DateTimeImmutable('@1234567890')],
154+
['13. Februar 2009', 'de', 'de_DE', 'date', new DateTimeImmutable('@1234567890')],
155+
["11:31:30\xE2\x80\xAFPM GMT+0", 'en', 'en_US', 'time', new DateTimeImmutable('@1234567890')],
156+
['23:31:30 GMT+0', 'de', 'de_DE', 'time', new DateTimeImmutable('@1234567890')],
157+
149158
// en_GB
150159
['13 February 2009, 23:31:30 GMT+0', 'en_GB', 'en_GB', 'datetime', new DateTime('@1234567890')],
151160
['13 February 2009', 'en_GB', 'en_GB', 'date', new DateTime('@1234567890')],

0 commit comments

Comments
 (0)