Hi Paul,

>                || STREQ (buf, "Jeu 26 oct 06:40:03 2017") /* macOS, older 
> FreeBSD */
> +              || STREQ (buf, "Jeu oct. 26 06:40:03 2017") /* MS-Windows */
>                || STREQ (buf, "26 octobre 2017 06:40:03") /* NetBSD */

This alternative (mentioning the month before the day) is not culturally
acceptable in French. I can assert this; I am a native French speaker.

> Unfortunately I had some trouble using that patch with GNU Emacs, which 
> wants to avoid bringing in a lot of Gnulib code for various reasons. The 
> trouble is that this change to nstrftime brings in the hard-locale 
> module, which brings in a bunch of other stuff.

In this case, it is easy to avoid the hard-locale module, since it directly
delegates to setlocale or _wsetlocale on native Windows:
  hard_locale
    -> setlocale_null_r
       Since SETLOCALE_NULL_ALL_MTSAFE=1 and SETLOCALE_NULL_ONE_MTSAFE=1:
         -> setlocale_null_r_unlocked
            -> MSVC:  const wchar_t *result = _wsetlocale (category, NULL);
               mingw: setlocale_null_unlocked
                        -> setlocale (category, NULL);

> If not, perhaps on MS-Windows nstrftime can test whether 
> it's a hard locale by invoking the underlying strftime with a %B for 
> tm_mon==4 and see whether that generates "May". Or something like that.

That would make a distinction between C and English locales on one side,
and other locales on the other side. I think that here it is preferrable
to make a distinction between C locale on one side and other locales on
the other side.

Done through this patch.

Note that your ChangeLog pre-filling helper was disturbed by the line
with libc_hidden_def.


diff --git a/ChangeLog b/ChangeLog
index 8e808fd6a8..d04105378d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2024-06-05  Bruno Haible  <br...@clisp.org>
+
+       nstrftime: Make %c work on native Windows again.
+       * lib/strftime.c: Include <locale.h> always. Include <wchar.h> on native
+       Windows.
+       (__strftime_internal): On native Windows, inspect the locale name like
+       hard_locale would do.
+       * modules/nstrftime (Link): Remove section.
+       * tests/test-nstrftime.h (locales_test): Disallow alternative that is
+       not culturally acceptable for French.
+
 2024-06-04  Paul Eggert  <egg...@cs.ucla.edu>
 
        endian: port better to Android NDK r26
@@ -28,7 +39,7 @@
        * lib/strftime.c: Include locale.h only if (USE_C_LOCALE &&
        HAVE_STRFTIME_L) || ((defined __NetBSD__ || defined __sun) &&
        REQUIRE_GNUISH_STRFTIME_AM_PM). Do not include hard-locale.h.
-       (libc_hidden_def): On native MS-Windows, use the C locale %c
+       (__strftime_internal): On native MS-Windows, use the C locale %c
        format regardless of language.  I hope that’s good enough.
        If not, let’s try to think of a way of solving the problem
        that doesn’t require using hard_locale.
diff --git a/lib/strftime.c b/lib/strftime.c
index 5f1e76833f..19a2d73ebd 100644
--- a/lib/strftime.c
+++ b/lib/strftime.c
@@ -92,13 +92,11 @@ extern char *tzname[];
 #include <stdlib.h>
 #include <string.h>
 
-#if USE_C_LOCALE && HAVE_STRFTIME_L
-# include <locale.h>
-#endif
-
+#include <locale.h>
 #if (defined __NetBSD__ || defined __sun) && REQUIRE_GNUISH_STRFTIME_AM_PM
-# include <locale.h>
 # include "localename.h"
+#elif defined _WIN32 && !defined __CYGWIN__
+# include <wchar.h>
 #endif
 
 #include "attribute.h"
@@ -1293,7 +1291,21 @@ __strftime_internal (STREAM_OR_CHAR_T *s, STRFTIME_ARG 
(size_t maxsize)
           subfmt = L_("%a %b %e %H:%M:%S %Y");
 #elif defined _WIN32 && !defined __CYGWIN__
           /* On native Windows, "%c" is "%d/%m/%Y %H:%M:%S" by default.  */
-          subfmt = L_("%a %b %e %H:%M:%S %Y");
+          bool is_c_locale;
+          /* This code is equivalent to is_c_locale = !hard_locale (LC_TIME). 
*/
+# if defined _MSC_VER
+          const wchar_t *locale = _wsetlocale (LC_TIME, NULL);
+          is_c_locale =
+            (wstrcmp (locale, L"C") == 0 || wstrcmp (locale, L"POSIX") == 0);
+# else
+          const char *locale = setlocale (LC_TIME, NULL);
+          is_c_locale =
+            (strcmp (locale, "C") == 0 || strcmp (locale, "POSIX") == 0);
+# endif
+          if (is_c_locale)
+            subfmt = L_("%a %b %e %H:%M:%S %Y");
+          else
+            subfmt = L_("%a %e %b %Y %H:%M:%S");
 #else
           goto underlying_strftime;
 #endif
diff --git a/modules/nstrftime b/modules/nstrftime
index e2f852a16b..69b9d84605 100644
--- a/modules/nstrftime
+++ b/modules/nstrftime
@@ -29,9 +29,6 @@ lib_SOURCES += nstrftime.c
 Include:
 "strftime.h"
 
-Link:
-$(HARD_LOCALE_LIB)
-
 License:
 LGPL
 
diff --git a/tests/test-nstrftime.h b/tests/test-nstrftime.h
index 80edc7aaec..b822f3acd1 100644
--- a/tests/test-nstrftime.h
+++ b/tests/test-nstrftime.h
@@ -482,7 +482,6 @@ locales_test (language_t language)
       ASSERT (STREQ (buf, "jeu. 26 oct. 2017 06:40:03") /* glibc, Cygwin */
               || STREQ (buf, "jeu. 26 oct. 06:40:03 2017") /* FreeBSD */
               || STREQ (buf, "Jeu 26 oct 06:40:03 2017") /* macOS, older 
FreeBSD */
-              || STREQ (buf, "Jeu oct. 26 06:40:03 2017") /* MS-Windows */
               || STREQ (buf, "26 octobre 2017 06:40:03") /* NetBSD */
               || STREQ (buf, "26 octobre 2017 à 06:40:03") /* Solaris (UTF-8) 
*/
               || STREQ (buf, "26 octobre 2017 \340 06:40:03") /* Solaris 
(ISO-8859-1) */);




Reply via email to