bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
From: Bruno Haible <bruno@clisp.org>
To: bug-gnulib@gnu.org
Subject: Re: nstrftime %c bug w.r.t. time zone
Date: Thu, 08 Feb 2024 12:50:27 +0100	[thread overview]
Message-ID: <2383643.yKrmzQ4Hd0@nimes> (raw)
In-Reply-To: <3217691.vfdyTQepKt@nimes>

I wrote:

> time.in.h says:
> 
>   (timezone_t) NULL stands for UTC.
> 
> However, nstrftime, on NetBSD 7 and Solaris 11, prints my local time zone
> ("CET"), not "UTC" or "GMT", in this test program:

This patch provides a workaround, and thus removes the test-nstrftime-2.sh
failure at test-nstrftime.h:421.


2024-02-08  Bruno Haible  <bruno@clisp.org>

	nstrtime, c-nstrftime: Fix %c directive's result on NetBSD, Solaris.
	* lib/strftime.c (__strftime_internal): On NetBSD and Solaris, remove
	the last word of the %c directive's result if it looks like a time zone.

diff --git a/lib/strftime.c b/lib/strftime.c
index f6714524bd..b70494cf78 100644
--- a/lib/strftime.c
+++ b/lib/strftime.c
@@ -967,7 +967,48 @@ __strftime_internal (STREAM_OR_CHAR_T *s, STRFTIME_ARG (size_t maxsize)
             len = strftime (ubuf, sizeof ubuf, ufmt, tp);
 # endif
             if (len != 0)
-              cpy (len - 1, ubuf + 1);
+              {
+# if defined __NetBSD__ || defined __sun /* NetBSD, Solaris */
+                if (format_char == L_('c'))
+                  {
+                    /* The output of the strftime %c directive consists of the
+                       date, the time, and the time zone.  But the time zone is
+                       wrong, since neither TZ nor ZONE was passed as argument.
+                       Therefore, remove the the last space-delimited word.
+                       In order not to accidentally remove a date or a year
+                       (that contains no letter) or an AM/PM indicator (that has
+                       length 2), remove that last word only if it contains a
+                       letter and has length >= 3.  */
+                    char *space;
+                    for (space = ubuf + len - 1; *space != ' '; space--)
+                      ;
+                    if (space > ubuf)
+                      {
+                        /* Found a space.  */
+                        if (strlen (space + 1) >= 3)
+                          {
+                            /* The last word has length >= 3.  */
+                            bool found_letter = false;
+                            const char *p;
+                            for (p = space + 1; *p != '\0'; p++)
+                              if ((*p >= 'A' && *p <= 'Z')
+                                  || (*p >= 'a' && *p <= 'z'))
+                                {
+                                  found_letter = true;
+                                  break;
+                                }
+                            if (found_letter)
+                              {
+                                /* The last word contains a letter.  */
+                                *space = '\0';
+                                len = space - ubuf;
+                              }
+                          }
+                      }
+                  }
+# endif
+                cpy (len - 1, ubuf + 1);
+              }
           }
           break;
 #endif





  reply	other threads:[~2024-02-08 11:51 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-08  0:04 nstrftime %c bug w.r.t. time zone Bruno Haible
2024-02-08 11:50 ` Bruno Haible [this message]
2024-02-08 13:55   ` Bruno Haible
2024-02-09  7:36   ` Paul Eggert
2024-02-09 10:10     ` Bruno Haible

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://lists.gnu.org/mailman/listinfo/bug-gnulib

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2383643.yKrmzQ4Hd0@nimes \
    --to=bruno@clisp.org \
    --cc=bug-gnulib@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).