mailing list of musl libc
 help / color / mirror / code / Atom feed
* [PATCH] strptime: add basic support for '%s' (seconds since epoch)
@ 2018-07-15  3:14 Will Dietz
  2018-07-16  0:11 ` Rich Felker
  0 siblings, 1 reply; 3+ messages in thread
From: Will Dietz @ 2018-07-15  3:14 UTC (permalink / raw)
  To: musl

[-- Attachment #1: Type: text/plain, Size: 364 bytes --]

Attached.

Background/context:

* http://www.openwall.com/lists/musl/2018/01/18/4
* http://austingroupbugs.net/view.php?id=169#c283

Seems to work on basic usage, but has not yet been thoroughly tested/vetted.
Sharing in case useful / good starting point regardless ;).

Also available on github:
https://github.com/dtzWill/musl/tree/feature/strptime-s-fmt

~Will

[-- Attachment #2: strptime-s.patch --]
[-- Type: text/x-patch, Size: 1134 bytes --]

From 8cc60ad0d982d2ef04c062372e1a459e984da22d Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Wed, 11 Jul 2018 13:08:22 -0500
Subject: [PATCH] strptime: add basic support for '%s' (seconds since epoch)

---
 src/time/strptime.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/src/time/strptime.c b/src/time/strptime.c
index c54a0d8c..bec00368 100644
--- a/src/time/strptime.c
+++ b/src/time/strptime.c
@@ -5,6 +5,9 @@
 #include <stddef.h>
 #include <string.h>
 #include <strings.h>
+#include "time_impl.h"
+
+struct tm *__localtime_r(const time_t *restrict, struct tm *restrict);
 
 char *strptime(const char *restrict s, const char *restrict f, struct tm *restrict tm)
 {
@@ -119,6 +122,15 @@ char *strptime(const char *restrict s, const char *restrict f, struct tm *restri
 			min = 0;
 			range = 61;
 			goto numeric_range;
+		case 's':
+			if (!isdigit(*s)) return 0;
+			else {
+				char *new_s;
+				time_t t = strtoull(s, &new_s, 10);
+				s = new_s;
+				if (!__localtime_r(&t, tm)) return 0;
+			}
+			break;
 		case 'T':
 			s = strptime(s, "%H:%M:%S", tm);
 			if (!s) return 0;
-- 
2.18.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2018-07-16 19:41 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-07-15  3:14 [PATCH] strptime: add basic support for '%s' (seconds since epoch) Will Dietz
2018-07-16  0:11 ` Rich Felker
2018-07-16 19:41   ` Will Dietz

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

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 NNTP newsgroup(s).