shithub: npe

Download patch

ref: f423ec55a7931a11e80de98ac249cb0be4aee077
parent: 73101d7c240331253c4b80b04520dc118dd8e679
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Mon Jul 10 10:58:45 EDT 2023

libnpe: strftime (%Y %m %d %H %M and %S)

--- a/libnpe/_main.c
+++ b/libnpe/_main.c
@@ -18,6 +18,7 @@
 	exit = npe_threadexit;
 
 	argv0 = *argv; /* for sdl to set at least some kind of label */
+	tmfmtinstall();
 
 	threadexitsall(npe_main_renamed(argc, argv) == 0 ? nil : "error");
 }
--- a/libnpe/strftime.c
+++ b/libnpe/strftime.c
@@ -3,9 +3,49 @@
 size_t
 strftime(char *s, size_t max, const char *format, const struct tm *tm)
 {
-	/* FIXME */
-	USED(max, format, tm);
-	s[0] = 0;
+	char *s₀, *e, *f, c;
+	int i;
+	Tm t;
 
-	return 0;
+	memset(&t, 0, sizeof(t));
+	t.sec = tm->tm_sec;
+	t.min = tm->tm_min;
+	t.hour = tm->tm_hour;
+	t.mday = tm->tm_mday;
+	t.mon = tm->tm_mon;
+	t.year = tm->tm_year;
+	t.wday = tm->tm_wday;
+	t.yday = tm->tm_yday;
+	t.tzoff = 0;
+	t.tz = tzload(tm->tm_zone);
+	/* ... = tm->tm_isdst; */ /* FIXME */
+
+	*s = 0;
+	s₀ = s;
+	e = s+max;
+	for(i = 0; format[i]; i++){
+		if((c = format[i]) == '%'){
+			c = format[i+1];
+			f = nil;
+			switch(c){
+			case 'Y': f = "YYYY"; break;
+			case 'm': f = "MM"; break;
+			case 'd': f = "DD"; break;
+			case 'H': f = "hh"; break;
+			case 'M': f = "mm"; break;
+			case 'S': f = "ss"; break;
+			/* FIXME - more formatting */
+			case '%': break;
+			default: s = seprint(s, e, "%%"); break;
+			}
+			i++;
+			if(f != nil){
+				s = seprint(s, e, "%τ", tmfmt(&t, f));
+				continue;
+			}
+		}
+		s = seprint(s, e, "%c", c);
+	}
+
+	return s-s₀;
 }