From 0c3b57555993df6cb53e02f99cc3cbf2c25caddf Mon Sep 17 00:00:00 2001 From: Matthieu Marcillaud <marcimat@rezo.net> Date: Fri, 2 Apr 2021 20:40:37 +0200 Subject: [PATCH] =?UTF-8?q?La=20fonction=20DATE=5FFORMAT()=20en=20mysql=20?= =?UTF-8?q?dispose=20de=20diff=C3=A9rents=20formats=20tel=20que=20%Y=20%m?= =?UTF-8?q?=20%d=20pour=20formatter=20une=20date.=20En=20SQLite,=20on=20ma?= =?UTF-8?q?ppe=20cette=20fonction=20absente=20nativement=20sur=20strftime(?= =?UTF-8?q?)=20de=20PHP.=20Cependant,=20certains=20param=C3=A8tres=20ne=20?= =?UTF-8?q?correspondent=20pas=20entre=20les=20deux=20!=20On=20propose=20d?= =?UTF-8?q?e=20convertir=20=C3=A0=20la=20vol=C3=A9e=20les=20param=C3=A8tre?= =?UTF-8?q?s=20qui=20ont=20un=20=C3=A9quivalent=20sous=20un=20autre=20nom,?= =?UTF-8?q?=20Et=20de=20logguer=20si=20on=20trouve=20des=20param=C3=A8tres?= =?UTF-8?q?=20dont=20on=20sait=20qu'ils=20n'existent=20pas=20ou=20retourne?= =?UTF-8?q?nt=20une=20valeur=20totalement=20diff=C3=A9rente=20=C3=A0=20cel?= =?UTF-8?q?le=20attendue.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ecrire/req/sqlite_fonctions.php | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/ecrire/req/sqlite_fonctions.php b/ecrire/req/sqlite_fonctions.php index 375a1cf928..50413d978a 100644 --- a/ecrire/req/sqlite_fonctions.php +++ b/ecrire/req/sqlite_fonctions.php @@ -120,7 +120,6 @@ function _sqlite_init_functions(&$sqlite) { 'YEAR' => array('_sqlite_func_year', 1) ); - foreach ($fonctions as $f => $r) { _sqlite_add_function($sqlite, $f, $r); } @@ -315,8 +314,29 @@ function _sqlite_func_regexp_match($cherche, $quoi) { return $return; } + // https://code.spip.net/@_sqlite_func_strftime function _sqlite_func_strftime($date, $conv) { + // ok : %a %b %d %e %H %I %l %j %k %m %p %r %S %T %w %y %Y + // on ne sait pas en gérer certains... + static $errors = []; + static $mysql_to_php_not_ok = ['%c', '%D', '%f', '%U', '%V', '%W', '%X']; + static $mysql_to_php = [ + '%h' => '%I', + '%i' => '%M', + '%M' => '%B', + '%s' => '%S', + '%u' => '%U', + '%v' => '%V', + '%x' => '%G', + ]; + $count = 0; + str_replace($mysql_to_php_not_ok, '', $conv, $count); + if ($count > 0 && !isset($errors[$conv])) { + $errors[$conv] = true; + spip_log("DATE_FORMAT : At least one parameter can't be parsed by strftime with format '$conv'", 'sqlite.' . _LOG_INFO_IMPORTANTE); + } + $conv = str_replace(array_keys($mysql_to_php), $mysql_to_php, $conv); return strftime($conv, is_int($date) ? $date : strtotime($date)); } -- GitLab