Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 17 Oct 2004 01:23:02 +0800
From:      Xin LI <delphij@frontfree.net>
To:        freebsd-hackers@FreeBSD.org
Subject:   [PATCH] add '-' glibc extension to strftime(3)
Message-ID:  <20041016172302.GA2764@frontfree.net>

next in thread | raw e-mail | index | archive | help

--7AUc2qLy4jB3hD7Z
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

Hi, folks,

It turns out that the GNU extension '-' in their strftime(3) implementation
is somewhat popular in several applications.  The patch in the last part of
this e-mail will add a simulate implementation for it.

My question is:
	(1) Am I doing things cleanly and correctly? I have attempted to
	    keep the code style consistent with the old one and style(9)
	    but maybe I have missed something else, or did not do it
	    sufficently?
	(2) Is the way of implementing it clean enough?

Thanks for any comments!

Index: strftime.3
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /home/fcvs/src/lib/libc/stdtime/strftime.3,v
retrieving revision 1.34
diff -u -r1.34 strftime.3
--- strftime.3	2 Jul 2004 23:52:12 -0000	1.34
+++ strftime.3	16 Oct 2004 17:13:08 -0000
@@ -36,7 +36,7 @@
 .\"     @(#)strftime.3	8.1 (Berkeley) 6/4/93
 .\" $FreeBSD: src/lib/libc/stdtime/strftime.3,v 1.34 2004/07/02 23:52:12 r=
u Exp $
 .\"
-.Dd January 4, 2003
+.Dd October 17, 2004
 .Dt STRFTIME 3
 .Os
 .Sh NAME
@@ -216,6 +216,8 @@
 is replaced by national representation of the date and time
 (the format is similar to that produced by
 .Xr date 1 ) .
+.It Cm %-*
+GLIBC extensions.  Do not do padding when making output.
 .It Cm %%
 is replaced by
 .Ql % .
Index: strftime.c
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /home/fcvs/src/lib/libc/stdtime/strftime.c,v
retrieving revision 1.40
diff -u -r1.40 strftime.c
--- strftime.c	14 Jun 2004 10:31:52 -0000	1.40
+++ strftime.c	16 Oct 2004 17:14:24 -0000
@@ -59,6 +59,13 @@
 #define IN_THIS	2
 #define IN_ALL	3
=20
+#define PAD_DEFAULT	0
+#define PAD_LESS	1
+#if 0		/* XXX NOT IMPLEMENTED YET */
+#define PAD_SPACE	2
+#define PAD_ZERO	3
+#endif
+
 size_t
 strftime(char * __restrict s, size_t maxsize, const char * __restrict form=
at,
     const struct tm * __restrict t)
@@ -99,13 +106,14 @@
 const char * const	ptlim;
 int *			warnp;
 {
-	int Ealternative, Oalternative;
+	int Ealternative, Oalternative, Palternative;
 	struct lc_time_T *tptr =3D __get_current_time_locale();
=20
 	for ( ; *format; ++format) {
 		if (*format =3D=3D '%') {
 			Ealternative =3D 0;
 			Oalternative =3D 0;
+			Palternative =3D PAD_DEFAULT;
 label:
 			switch (*++format) {
 			case '\0':
@@ -188,21 +196,27 @@
 				Oalternative++;
 				goto label;
 			case 'e':
-				pt =3D _conv(t->tm_mday, "%2d", pt, ptlim);
+				pt =3D _conv(t->tm_mday, (Palternative =3D=3D PAD_LESS) ?
+					"%d" : "%2d",
+					pt, ptlim);
 				continue;
 			case 'F':
 				pt =3D _fmt("%Y-%m-%d", t, pt, ptlim, warnp);
 				continue;
 			case 'H':
-				pt =3D _conv(t->tm_hour, "%02d", pt, ptlim);
+				pt =3D _conv(t->tm_hour, (Palternative =3D=3D PAD_LESS) ?
+					"%d" : "%02d",
+					pt, ptlim);
 				continue;
 			case 'I':
 				pt =3D _conv((t->tm_hour % 12) ?
-					(t->tm_hour % 12) : 12,
-					"%02d", pt, ptlim);
+					(t->tm_hour % 12) : 12, (Palternative =3D=3D PAD_LESS) ?
+					"%d" : "%02d",
+					pt, ptlim);
 				continue;
 			case 'j':
-				pt =3D _conv(t->tm_yday + 1, "%03d", pt, ptlim);
+				pt =3D _conv(t->tm_yday + 1, (Palternative =3D=3D PAD_LESS) ?
+					"%d" : "%03d", pt, ptlim);
 				continue;
 			case 'k':
 				/*
@@ -215,7 +229,9 @@
 				** "%l" have been swapped.
 				** (ado, 1993-05-24)
 				*/
-				pt =3D _conv(t->tm_hour, "%2d", pt, ptlim);
+				pt =3D _conv(t->tm_hour, (Palternative =3D=3D PAD_LESS) ?
+					"%d": "%2d",
+					pt, ptlim);
 				continue;
 #ifdef KITCHEN_SINK
 			case 'K':
@@ -236,14 +252,19 @@
 				** (ado, 1993-05-24)
 				*/
 				pt =3D _conv((t->tm_hour % 12) ?
-					(t->tm_hour % 12) : 12,
-					"%2d", pt, ptlim);
+					(t->tm_hour % 12) : 12, (Palternative =3D=3D PAD_LESS) ?
+					"%d" : "%2d",
+					pt, ptlim);
 				continue;
 			case 'M':
-				pt =3D _conv(t->tm_min, "%02d", pt, ptlim);
+				pt =3D _conv(t->tm_min, (Palternative =3D=3D PAD_LESS) ?
+					"%d" : "%02d",
+					pt, ptlim);
 				continue;
 			case 'm':
-				pt =3D _conv(t->tm_mon + 1, "%02d", pt, ptlim);
+				pt =3D _conv(t->tm_mon + 1, (Palternative =3D=3D PAD_LESS) ?
+					"%d" : "%02d",
+					pt, ptlim);
 				continue;
 			case 'n':
 				pt =3D _add("\n", pt, ptlim);
@@ -262,7 +283,9 @@
 					warnp);
 				continue;
 			case 'S':
-				pt =3D _conv(t->tm_sec, "%02d", pt, ptlim);
+				pt =3D _conv(t->tm_sec, (Palternative =3D=3D PAD_LESS) ?
+					"%d" : "%02d",
+					pt, ptlim);
 				continue;
 			case 's':
 				{
@@ -289,8 +312,8 @@
 				continue;
 			case 'U':
 				pt =3D _conv((t->tm_yday + DAYSPERWEEK -
-					t->tm_wday) / DAYSPERWEEK,
-					"%02d", pt, ptlim);
+					t->tm_wday) / DAYSPERWEEK, (Palternative =3D=3D PAD_LESS) ?
+					"%d" : "%02d", pt, ptlim);
 				continue;
 			case 'u':
 				/*
@@ -423,11 +446,13 @@
 				continue;
 			case 'y':
 				*warnp =3D IN_ALL;
-				pt =3D _conv((t->tm_year + TM_YEAR_BASE) % 100,
-					"%02d", pt, ptlim);
+				pt =3D _conv((t->tm_year + TM_YEAR_BASE) % 100, (Palternative =3D=3D P=
AD_LESS) ?
+					"%d" : "%02d",
+					pt, ptlim);
 				continue;
 			case 'Y':
-				pt =3D _conv(t->tm_year + TM_YEAR_BASE, "%04d",
+				pt =3D _conv(t->tm_year + TM_YEAR_BASE, (Palternative =3D=3D PAD_LESS)=
 ?
+					"%d" : "%04d",
 					pt, ptlim);
 				continue;
 			case 'Z':
@@ -501,6 +526,23 @@
 				pt =3D _fmt(tptr->date_fmt, t, pt, ptlim,
 					warnp);
 				continue;
+			case '-':
+				if (Palternative !=3D PAD_DEFAULT)
+					break;
+				Palternative =3D PAD_LESS;
+				goto label;
+#if 0		/* XXX NOT IMPLEMENTED YET */
+			case '_':
+				if (Palternative !=3D PAD_DEFAULT)
+					break;
+				Palternative =3D PAD_SPACE;
+				goto label;
+			case '0':
+				if (Palternative !=3D PAD_DEFAULT)
+					break;
+				Palternative =3D PAD_ZERO;
+				goto label;
+#endif
 			case '%':
 			/*
 			** X311J/88-090 (4.12.3.5): if conversion char is

--=20
Xin LI <delphij frontfree net>	http://www.delphij.net/
See complete headers for GPG key and other information.


--7AUc2qLy4jB3hD7Z
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (FreeBSD)

iD8DBQFBcVj2/cVsHxFZiIoRAi+pAKCGPA4g2L71AyR1kHfNBOo4KyaBPgCdFu4K
QEphXtBLZogNaLH6fGL3l0g=
=nI+R
-----END PGP SIGNATURE-----

--7AUc2qLy4jB3hD7Z--


Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20041016172302.GA2764>