This patch adds a test that the 'date' command uses the 12-hour clock or
24-hour clock depending on the current locale.

Also, I noticed that 'date' uses nl_langinfo (_DATE_FMT). It appears that
_DATE_FMT is a glibc extension. Is there any reason that the
standardized D_T_FMT isn't used instead?

It looks better in my timezone (America/Los_Angeles) at least:

    $ export LC_ALL=en_US.UTF-8
    $ ./date-D_T_FMT
    Sat 11 Oct 2025 10:30:17 PM PDT
    $ ./date-_DATE_FMT
    Sat Oct 11 10:30:08 PM PDT 2025

With _DATE_FMT the year feels misplaced to me.

Collin

[1] https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/langinfo.h.html

>From dda09bfe9cfe367ce767b0ead89c1b7c2875c870 Mon Sep 17 00:00:00 2001
Message-ID: <dda09bfe9cfe367ce767b0ead89c1b7c2875c870.1760247204.git.collin.fu...@gmail.com>
From: Collin Funk <[email protected]>
Date: Sat, 11 Oct 2025 21:55:31 -0700
Subject: [PATCH] tests: date: check that the hour format of the current locale
 is used

* tests/date/date-locale-hour.sh: New file.
* tests/local.mk (all_tests): Add the new test.
---
 tests/date/date-locale-hour.sh | 45 ++++++++++++++++++++++++++++++++++
 tests/local.mk                 |  1 +
 2 files changed, 46 insertions(+)
 create mode 100755 tests/date/date-locale-hour.sh

diff --git a/tests/date/date-locale-hour.sh b/tests/date/date-locale-hour.sh
new file mode 100755
index 000000000..b7506b34b
--- /dev/null
+++ b/tests/date/date-locale-hour.sh
@@ -0,0 +1,45 @@
+#!/bin/sh
+# Check that 'date' uses the 12-hour or 24-hour clock depending on the
+# current locale.
+
+# Copyright (C) 2025 Free Software Foundation, Inc.
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
+print_ver_ date
+
+test "$LOCALE_FR_UTF8" != none || skip_ "French UTF-8 locale not available"
+
+# Check that the French locale uses the 24-hour clock by default.
+export LC_ALL="$LOCALE_FR_UTF8"
+french_date=$(date -d 2025-10-11T13:00)
+case "$french_date" in
+  *"13:00:00"*) ;;
+  *) fail=1 ;;
+esac
+
+export LC_ALL=en_US.UTF-8
+if test "$(locale charmap 2>/dev/null)" != UTF-8; then
+  skip_ "American English UTF-8 locale not available"
+fi
+
+# Check that the American English locale uses the 12-hour clock by default.
+american_date=$(date -d 2025-10-11T13:00)
+case "$american_date" in
+  *"01:00:00 PM"*) ;;
+  *) fail=1 ;;
+esac
+
+Exit $fail
diff --git a/tests/local.mk b/tests/local.mk
index 749f4caf5..92b119283 100644
--- a/tests/local.mk
+++ b/tests/local.mk
@@ -317,6 +317,7 @@ all_tests =					\
   tests/date/date-debug.sh			\
   tests/date/date-ethiopia.sh			\
   tests/date/date-iran.sh			\
+  tests/date/date-locale-hour.sh		\
   tests/date/date-sec.sh			\
   tests/date/date-thailand.sh			\
   tests/date/date-tz.sh				\
-- 
2.51.0

Reply via email to