]> git.ipfire.org Git - thirdparty/gnulib.git/commitdiff
nstrftime, fprintftime: Reduce link requirements on macOS.
authorBruno Haible <bruno@clisp.org>
Thu, 20 Nov 2025 02:04:39 +0000 (03:04 +0100)
committerBruno Haible <bruno@clisp.org>
Thu, 20 Nov 2025 02:04:39 +0000 (03:04 +0100)
Reported by Paul Eggert for GNU tar.

* lib/strftime.c: On macOS, don't include localename.h.
(my_strftime): On macOS, use strftime() instead of gl_locale_name_unsafe
to test for the three particular locales.
* modules/nstrftime (Depends-on): Add condition to localename-unsafe.
(Link): Remove @INTL_MACOSX_LIBS@.
* modules/nstrftime-limited (Depends-on): Add condition to
localename-unsafe-limited.
(Link): Remove @INTL_MACOSX_LIBS@.
* modules/nstrftime-tests (Makefile.am): Link test-nstrftime* without
@INTL_MACOSX_LIBS@.
* modules/fprintftime (Link): Remove @INTL_MACOSX_LIBS@.

ChangeLog
lib/strftime.c
modules/fprintftime
modules/nstrftime
modules/nstrftime-limited
modules/nstrftime-tests

index 1ee42a2ada3e3e877e224c40581da1881712e6ae..e84cd50da36e5cf3c25402cf15382f11fc1369db 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,19 @@
+2025-11-19  Bruno Haible  <bruno@clisp.org>
+
+       nstrftime, fprintftime: Reduce link requirements on macOS.
+       Reported by Paul Eggert for GNU tar.
+       * lib/strftime.c: On macOS, don't include localename.h.
+       (my_strftime): On macOS, use strftime() instead of gl_locale_name_unsafe
+       to test for the three particular locales.
+       * modules/nstrftime (Depends-on): Add condition to localename-unsafe.
+       (Link): Remove @INTL_MACOSX_LIBS@.
+       * modules/nstrftime-limited (Depends-on): Add condition to
+       localename-unsafe-limited.
+       (Link): Remove @INTL_MACOSX_LIBS@.
+       * modules/nstrftime-tests (Makefile.am): Link test-nstrftime* without
+       @INTL_MACOSX_LIBS@.
+       * modules/fprintftime (Link): Remove @INTL_MACOSX_LIBS@.
+
 2025-11-19  Bruno Haible  <bruno@clisp.org>
 
        wcsdup: Make POSIX compliant.
index b78901f5fd190bcc77c9cb2fe2a59002a7930bc5..291c650b97031f78439b65084481aadb0f349c7d 100644 (file)
@@ -181,7 +181,9 @@ enum pad_style
 #if SUPPORT_NON_GREG_CALENDARS_IN_STRFTIME
 /* Support for non-Gregorian calendars.  */
 # include "localcharset.h"
-# include "localename.h"
+# if !(defined __APPLE__ && defined __MACH__)
+#  include "localename.h"
+# endif
 # include "calendars.h"
 # define CAL_ARGS(x,y) x, y,
 #else
@@ -1137,9 +1139,9 @@ my_strftime (STREAM_OR_CHAR_T *s, STRFTIME_ARG (size_t maxsize)
 #if SUPPORT_NON_GREG_CALENDARS_IN_STRFTIME
   /* Recognize whether to use a non-Gregorian calendar.  */
   const struct calendar *cal = NULL;
-  struct calendar_date caldate;
   if (strcmp (locale_charset (), "UTF-8") == 0)
     {
+# if !(defined __APPLE__ && defined __MACH__)
       const char *loc = gl_locale_name_unsafe (LC_TIME, "LC_TIME");
       if (strlen (loc) >= 5 && !(loc[5] >= 'A' && loc[5] <= 'Z'))
         {
@@ -1149,15 +1151,45 @@ my_strftime (STREAM_OR_CHAR_T *s, STRFTIME_ARG (size_t maxsize)
             cal = &persian_calendar;
           else if (memcmp (loc, "am_ET", 5) == 0)
             cal = &ethiopian_calendar;
-          if (cal != NULL)
-            {
-              if (cal->from_gregorian (&caldate,
-                                       tp->tm_year + 1900,
-                                       tp->tm_mon,
-                                       tp->tm_mday) < 0)
-                cal = NULL;
-            }
         }
+# else /* defined __APPLE__ && defined __MACH__ */
+      /* Nearly equivalent code for macOS, that avoids the need to link with
+         CoreFoundation.
+         It's not entirely equivalent, because it tests only for the language
+         (Thai, Farsi, Amharic) instead of also for the territory (Thailand,
+         Iran, Ethiopia).  */
+      /* Get the translation of "Monday" in the LC_TIME locale, by calling
+         the underlying strftime function.  */
+      struct tm some_monday; /* 2024-01-01 12:00:00 */
+      memset (&some_monday, '\0', sizeof (struct tm));
+      some_monday.tm_year = 2024 - 1900;
+      some_monday.tm_mon = 1 - 1;
+      some_monday.tm_mday = 1;
+      some_monday.tm_wday = 1; /* Monday */
+      some_monday.tm_hour = 12;
+      some_monday.tm_min = 0;
+      some_monday.tm_sec = 0;
+      char weekday_buf[32];
+      if (strftime (weekday_buf, sizeof (weekday_buf), "%A", &some_monday) > 0)
+        {
+          /* Test for the Thai / Farsi / Amharic translation of "Monday".  */
+          if (streq (weekday_buf, "จันทร์") || streq (weekday_buf, "วันจันทร์"))
+            cal = &thai_calendar;
+          else if (streq (weekday_buf, "ﺩﻮﺸﻨﺒﻫ") || streq (weekday_buf, "دوشنبه"))
+            cal = &persian_calendar;
+          else if (streq (weekday_buf, "ሰኞ"))
+            cal = &ethiopian_calendar;
+        }
+# endif
+    }
+  struct calendar_date caldate;
+  if (cal != NULL)
+    {
+      if (cal->from_gregorian (&caldate,
+                               tp->tm_year + 1900,
+                               tp->tm_mon,
+                               tp->tm_mday) < 0)
+        cal = NULL;
     }
 #endif
   bool tzset_called = false;
index 121073af1f6c4f674d3b90c7df6c31029f887aac..3700a3b45e5620d75f78a6d723e294c9029005a4 100644 (file)
@@ -21,7 +21,6 @@ Include:
 "fprintftime.h"
 
 Link:
-@INTL_MACOSX_LIBS@
 
 License:
 GPL
index ee52e898253874c9f7662daf46877bbe4f0dc377..4cb87d28af707727baca12e8a8ef05c448ce73db 100644 (file)
@@ -22,7 +22,7 @@ extensions
 intprops
 libc-config
 localcharset
-localename-unsafe
+localename-unsafe [case "$host_os" in darwin*) false ;; *) true ;; esac]
 bool
 stdckdint-h
 stdint-h
@@ -38,7 +38,6 @@ Include:
 "strftime.h"
 
 Link:
-@INTL_MACOSX_LIBS@
 $(LIBTHREAD)
 
 License:
index 9f53b621a7793b1306f81a6a6b593ea5188eaa23..18fc11a51ab7d0b0b052205e2c03dd00eca4e844 100644 (file)
@@ -23,7 +23,7 @@ extensions
 intprops
 libc-config
 localcharset
-localename-unsafe-limited
+localename-unsafe-limited [case "$host_os" in darwin*) false ;; *) true ;; esac]
 bool
 stdckdint-h
 time_rz
@@ -38,7 +38,6 @@ Include:
 "strftime.h"
 
 Link:
-@INTL_MACOSX_LIBS@
 
 License:
 LGPL
index 88f4b850749193fb846e4b3c17923d4736d90ac7..80dae9e02484ed0c0a936311cb887fba44eb48c7 100644 (file)
@@ -49,16 +49,16 @@ check_PROGRAMS += \
   test-nstrftime-TH \
   test-nstrftime-IR \
   test-nstrftime-ET
-test_nstrftime_LDADD = $(LDADD) $(SETLOCALE_LIB) @INTL_MACOSX_LIBS@ $(LIBTHREAD)
-test_nstrftime_DE_LDADD = $(LDADD) $(SETLOCALE_LIB) @INTL_MACOSX_LIBS@ $(LIBTHREAD)
-test_nstrftime_TH_LDADD = $(LDADD) $(SETLOCALE_LIB) @INTL_MACOSX_LIBS@ $(LIBTHREAD)
-test_nstrftime_IR_LDADD = $(LDADD) $(SETLOCALE_LIB) @INTL_MACOSX_LIBS@ $(LIBTHREAD)
-test_nstrftime_ET_LDADD = $(LDADD) $(SETLOCALE_LIB) @INTL_MACOSX_LIBS@ $(LIBTHREAD)
+test_nstrftime_LDADD = $(LDADD) $(SETLOCALE_LIB) $(LIBTHREAD)
+test_nstrftime_DE_LDADD = $(LDADD) $(SETLOCALE_LIB) $(LIBTHREAD)
+test_nstrftime_TH_LDADD = $(LDADD) $(SETLOCALE_LIB) $(LIBTHREAD)
+test_nstrftime_IR_LDADD = $(LDADD) $(SETLOCALE_LIB) $(LIBTHREAD)
+test_nstrftime_ET_LDADD = $(LDADD) $(SETLOCALE_LIB) $(LIBTHREAD)
 
 if OS_IS_NATIVE_WINDOWS
 TESTS += test-nstrftime-w32utf8.sh
 noinst_PROGRAMS += test-nstrftime-w32utf8
-test_nstrftime_w32utf8_LDADD = $(LDADD) test-nstrftime-windows-utf8.res $(SETLOCALE_LIB) @INTL_MACOSX_LIBS@ $(LIBTHREAD)
+test_nstrftime_w32utf8_LDADD = $(LDADD) test-nstrftime-windows-utf8.res $(SETLOCALE_LIB) $(LIBTHREAD)
 test-nstrftime-windows-utf8.res : $(srcdir)/windows-utf8.rc
        $(WINDRES) -i $(srcdir)/windows-utf8.rc -o test-nstrftime-windows-utf8.res --output-format=coff
 MOSTLYCLEANFILES += test-nstrftime-windows-utf8.res