Cygwin strptime() is missing "%s" which strftime() has

Brian Inglis Brian.Inglis@SystematicSw.ab.ca
Mon Jul 24 04:10:00 GMT 2017


On 2017-07-23 20:09, Lavrentiev, Anton (NIH/NLM/NCBI) [C] wrote:
>> But that's just scanning a decimal integer to time_t.
> 
> It's not a question of whether I can or can't convert a string into an integer, rather it's a question about portability of code that uses %s for both functions and expects it to work unchanged in the Cygwin environment.  Also, strptime() was designed to be a reversal to strftime() (from the man-pages: the  strptime() function is the converse function to strftime(3)) so both are supposed to "understand" the same basic set of formats.  Because of Cygwin's strptime() missing "%s", the following also does not work even from command line:
> 
> $ date +"%s" | strptime "%s"

Attached diff for proposed strptime %s and %F support.
Let me know if you would prefer a different approach before I submit a git
format-patch.

-- 
Take care. Thanks, Brian Inglis, Calgary, Alberta, Canada
-------------- next part --------------
diff --git a/newlib/libc/time/strptime.c b/newlib/libc/time/strptime.c
index c0861eb87..b352bcbab 100644
--- a/newlib/libc/time/strptime.c
+++ b/newlib/libc/time/strptime.c
@@ -38,6 +38,7 @@
 #include <strings.h>
 #include <ctype.h>
 #include <stdlib.h>
+#include <inttypes.h>
 #include "../locale/setlocale.h"
 
 #define _ctloc(x) (_CurrentTimeLocale->x)
@@ -230,6 +231,13 @@ strptime_l (const char *buf, const char *format, struct tm *timeptr,
 		buf = s;
 		ymd |= SET_MDAY;
 		break;
+	    case 'F' :		/* %Y-%m-%d */
+		s = strptime_l (buf, "%Y-%m-%d", timeptr, locale);
+		if (s == NULL)
+		    return NULL;
+		buf = s;
+		ymd |= SET_YMD;
+		break;
 	    case 'H' :
 	    case 'k' :
 		ret = strtol_l (buf, &s, 10, locale);
@@ -300,6 +308,21 @@ strptime_l (const char *buf, const char *format, struct tm *timeptr,
 		    return NULL;
 		buf = s;
 		break;
+	    case 's' : {
+		    intmax_t sec;
+		    time_t t;
+
+		    sec = strtoimax (buf, &s, 10);
+		    t = (time_t)sec;
+		    if (s == buf
+			|| (intmax_t)t != sec
+			|| localtime_r (&t, timeptr) != timeptr)
+			return NULL;
+		    ;
+		    buf = s;
+		    ymd |= SET_YDAY | SET_WDAY | SET_YMD;
+		    break;
+		}
 	    case 'S' :
 		ret = strtol_l (buf, &s, 10, locale);
 		if (s == buf)


More information about the Cygwin-patches mailing list