public inbox for cygwin-cvs@sourceware.org
help / color / mirror / Atom feed
From: Corinna Vinschen <corinna@sourceware.org>
To: cygwin-cvs@sourceware.org
Subject: [newlib-cygwin/main] Cygwin: strptime: fix am/pm handling
Date: Wed, 21 Feb 2024 18:54:35 +0000 (GMT)	[thread overview]
Message-ID: <20240221185435.8082B385840B@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=585855eef8639e6066bb7c12753deed33a6018b4

commit 585855eef8639e6066bb7c12753deed33a6018b4
Author:     Corinna Vinschen <corinna@vinschen.de>
AuthorDate: Wed Feb 21 19:54:20 2024 +0100
Commit:     Corinna Vinschen <corinna@vinschen.de>
CommitDate: Wed Feb 21 19:54:20 2024 +0100

    Cygwin: strptime: fix am/pm handling
    
    The %p format specifier is handled immediately.  It requires
    that tm_hour is already set.  This falls flat in case the am/pm
    marker preceeds the time specification.  Locales with am/pm
    marker preceeding time spec by default exist (e. g. ko_KR).
    
    Also, the code expects that tm_hour might be set to an invalid
    value because the %p specifier is used in conjunction with %H.
    But this usage is invalid in itself and now catched as error
    condition after commit 343a2a558153 ("Cygwin: strptime: make
    sure to fail on invalid input digits").
    
    Change the %H/%I/%p handling according to GLibC, i. e.
    
    - fix tm_hour for pm only if the time value has been specified
      as 12 hour time %I, and
    
    - perform the fixup only after the entire input has been scanned.
      This decouples the fixup from the %p position relativ to %I.
    
    Signed-off-by: Corinna Vinschen <corinna@vinschen.de>

Diff:
---
 winsup/cygwin/libc/strptime.cc | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/winsup/cygwin/libc/strptime.cc b/winsup/cygwin/libc/strptime.cc
index 353b8008a1c9..dc6771fe5e23 100644
--- a/winsup/cygwin/libc/strptime.cc
+++ b/winsup/cygwin/libc/strptime.cc
@@ -335,6 +335,8 @@ __strptime(const char *buf, const char *fmt, struct tm *tm,
 	const char *new_fmt;
 	uint ulim;
 	int ymd = 0;
+	bool got_I = false;
+	bool got_pm = false;
 
 	bp = (const unsigned char *)buf;
 	const struct lc_time_T *_CurrentTimeLocale = __get_time_locale (locale);
@@ -537,6 +539,7 @@ literal:
 		case 'H':
 			LEGAL_ALT(ALT_O);
 			bp = conv_num(bp, &tm->tm_hour, 0, 23, ALT_DIGITS);
+			got_I = false;
 			continue;
 
 		case 'l':	/* The hour (12-hour clock representation). */
@@ -547,6 +550,7 @@ literal:
 			bp = conv_num(bp, &tm->tm_hour, 1, 12, ALT_DIGITS);
 			if (tm->tm_hour == 12)
 				tm->tm_hour = 0;
+			got_I = true;
 			continue;
 
 		case 'j':	/* The day of year. */
@@ -573,9 +577,7 @@ literal:
 		case 'p':	/* The locale's equivalent of AM/PM. */
 			bp = find_string(bp, &i, _ctloc(am_pm), NULL, 2,
 					 locale);
-			if (tm->tm_hour > 11)
-				return NULL;
-			tm->tm_hour += i * 12;
+			got_pm = (i == 1);
 			LEGAL_ALT(0);
 			continue;
 
@@ -751,8 +753,12 @@ literal:
 		default:	/* Unknown/unsupported conversion. */
 			return NULL;
 		}
+
 	}
 
+	if (got_I && got_pm)
+	  tm->tm_hour += 12;
+
 	if (bp && (era || got_eoff))
 	  {
 	    /* Default to current era. */

                 reply	other threads:[~2024-02-21 18:54 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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

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

  git send-email \
    --in-reply-to=20240221185435.8082B385840B@sourceware.org \
    --to=corinna@sourceware.org \
    --cc=cygwin-cvs@sourceware.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).