strptime: fix am/pm converting to 24-hour system

Message ID 47c4248d8449aa6efa9ef3839ba2a9f329fcea9b.camel@espressif.com
State New
Headers
Series strptime: fix am/pm converting to 24-hour system |

Commit Message

Alexey Lapshin Feb. 20, 2024, 6:51 p.m. UTC
  Fix the issue of parsing 08:00AM, which currently gives a 20:00 representation.

---
 newlib/libc/time/strptime.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

-- 
2.34.1
  

Comments

Corinna Vinschen Feb. 21, 2024, 2:53 p.m. UTC | #1
Hi Alexey,

On Feb 20 18:51, Alexey Lapshin wrote:
> Fix the issue of parsing 08:00AM, which currently gives a 20:00 representation.
> 
> ---
>  newlib/libc/time/strptime.c | 11 ++++++-----
>  1 file changed, 6 insertions(+), 5 deletions(-)

Pushed, thank you!

Corinna
  

Patch

diff --git a/newlib/libc/time/strptime.c b/newlib/libc/time/strptime.c
index 6220ff73a..188218059 100644
--- a/newlib/libc/time/strptime.c
+++ b/newlib/libc/time/strptime.c
@@ -292,11 +292,12 @@  strptime_l (const char *buf, const char *format, struct tm *timeptr,
 		ret = match_string (&buf, _ctloc (am_pm), locale);
 		if (ret < 0)
 		    return NULL;
-		if (timeptr->tm_hour == 0) {
-		    if (ret == 1)
-			timeptr->tm_hour = 12;
-		} else
-		    timeptr->tm_hour += 12;
+		if (timeptr->tm_hour > 12)
+		    return NULL;
+		else if (timeptr->tm_hour == 12)
+		    timeptr->tm_hour = ret * 12;
+		else
+		    timeptr->tm_hour += ret * 12;
 		break;
 	    case 'q' :		/* quarter year - GNU extension */
 		ret = strtol_l (buf, &s, 10, locale);