alphp / strftime

This provides a cross-platform alternative to strftime() for when it will be removed from PHP

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

"Stand-alone" versions of "month in year" and "day of week" formats more closely mimic strftime in German locale

arkonan opened this issue · comments

Currently this library uses EEE and MMM for %a and %b. However I noticed with my own project's test suite that in the German locale that the "stand-alone" formats ccc and LLL more closely mimic what strftime does for %a and %b.

In particular EEE and MMM formats do not always truncate to 3 characters in the German locale (they sometimes generate 2 or 4 characters). They also add a period . after the truncated part which strftime does not do.

Switching to ccc and LLL has no effect on the English and Spanish locales in my test suite.

Looking at what "stand-alone" is for (https://unicode-org.github.io/icu/userguide/format_parse/datetime/#datetime-format-syntax) I think it is more correct to use the stand-alone formats in a strftime polyfill. While using EEE and MMM would potentially improve the output grammar when the format string combines multiple date parts, you'd have to look at the format string as a whole to know if you need to use the stand-alone versions or not. And at least my libc's strftime does not do this.

That seems like a useful change, thanks.

A quick check seems to indicate that in some cases with ccc and LLL the result is more consistent with strftime (see below some locales I had in mind for a quick test). For example for finnish and russian. In most cases it doesn't change anything from MMM/EEE.

So I'd say go for it :)

What would be useful would be to have a test that generates all local date strings using \strftime and this replacement library and find out which locales have different returns.

<?php

$locales = ['pl_PL', 'fr_FR', 'de_DE', 'nl_NL', 'nl_BE', 'de_CH', 'fr_CH', 'fr_BE', 'zh_CN', 'ru_RU', 'fi_FI', 'sv_SE', 'no_NO', 'ro_RO', 'cs_CZ', 'es_ES', 'pt_PT', 'pt_BR'];

foreach ($locales as $locale) {
	echo "$locale\n";
	$locale .= '.UTF-8';
	setlocale(LC_ALL, $locale);
	$i = function($f) use ($locale) {
		var_dump((new \IntlDateFormatter($locale, \IntlDateFormatter::FULL, \IntlDateFormatter::FULL, null, null, $f))->format(new DateTime('1 month ago')));
	};
	var_dump(@strftime('%a %A %b %B', strtotime('1 month ago')));
	$i("EEE EEEE MMM MMMM");
	$i("ccc cccc LLL LLLL");
	setlocale(LC_ALL, 'C'); // Restore locale to detect locales that are not installed
}
pl_PL
string(23) "pią piątek lip lipiec"
string(21) "pt. piątek lip lipca"
string(22) "pt. piątek lip lipiec"
fr_FR
string(27) "ven. vendredi juil. juillet"
string(27) "ven. vendredi juil. juillet"
string(27) "ven. vendredi juil. juillet"
de_DE
string(19) "Fr Freitag Jul Juli"
string(21) "Fr. Freitag Juli Juli"
string(19) "Fr Freitag Jul Juli"
nl_NL
string(19) "vr vrijdag jul juli"
string(20) "vr vrijdag jul. juli"
string(19) "vr vrijdag jul juli"
nl_BE
string(19) "vr vrijdag jul juli"
string(20) "vr vrijdag jul. juli"
string(19) "vr vrijdag jul juli"
de_CH
string(20) "Fre Freitag Jul Juli"
string(21) "Fr. Freitag Juli Juli"
string(19) "Fr Freitag Jul Juli"
fr_CH
string(24) "ven vendredi jui juillet"
string(27) "ven. vendredi juil. juillet"
string(27) "ven. vendredi juil. juillet"
fr_BE
string(24) "ven vendredi jui juillet"
string(27) "ven. vendredi juil. juillet"
string(27) "ven. vendredi juil. juillet"
zh_CN
string(25) "五 星期五 7月 七月"
string(28) "周五 星期五 7月 七月"
string(28) "周五 星期五 7月 七月"
ru_RU
string(35) "Пт Пятница июл Июль"
string(37) "пт пятница июля июля"
string(37) "Пт Пятница Июль Июль"
fi_FI
string(29) "pe perjantai heinä heinäkuu"
string(38) "pe perjantaina heinäkuuta heinäkuuta"
string(29) "pe perjantai heinä heinäkuu"
sv_SE
string(19) "fre fredag jul juli"
string(19) "fre fredag jul juli"
string(19) "fre fredag jul juli"
no_NO
string(19) "Fri Friday Jul July"
string(21) "fre. fredag juli juli"
string(19) "fr. fredag jul juli"
ro_RO
string(19) "Vi vineri iul iulie"
string(20) "Vi vineri iul. iulie"
string(20) "Vi vineri iul. iulie"
cs_CZ
string(25) "Pá Pátek čec červenec"
string(25) "pá pátek čvc července"
string(25) "pá pátek čvc červenec"
es_ES
string(21) "vie viernes jul julio"
string(21) "vie viernes jul julio"
string(21) "vie viernes jul julio"
pt_PT
string(19) "sex sexta jul julho"
string(25) "sex sexta-feira jul julho"
string(25) "sex sexta-feira jul julho"
pt_BR
string(19) "sex sexta jul julho"
string(25) "sex sexta-feira jul julho"
string(25) "sex sexta-feira jul julho"