[PATCH] add '-' glibc extension to strftime(3)
Xin LI
delphij at frontfree.net
Sat Oct 16 10:23:15 PDT 2004
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
===================================================================
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 ru 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
===================================================================
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
+#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 format,
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 = __get_current_time_locale();
for ( ; *format; ++format) {
if (*format == '%') {
Ealternative = 0;
Oalternative = 0;
+ Palternative = PAD_DEFAULT;
label:
switch (*++format) {
case '\0':
@@ -188,21 +196,27 @@
Oalternative++;
goto label;
case 'e':
- pt = _conv(t->tm_mday, "%2d", pt, ptlim);
+ pt = _conv(t->tm_mday, (Palternative == PAD_LESS) ?
+ "%d" : "%2d",
+ pt, ptlim);
continue;
case 'F':
pt = _fmt("%Y-%m-%d", t, pt, ptlim, warnp);
continue;
case 'H':
- pt = _conv(t->tm_hour, "%02d", pt, ptlim);
+ pt = _conv(t->tm_hour, (Palternative == PAD_LESS) ?
+ "%d" : "%02d",
+ pt, ptlim);
continue;
case 'I':
pt = _conv((t->tm_hour % 12) ?
- (t->tm_hour % 12) : 12,
- "%02d", pt, ptlim);
+ (t->tm_hour % 12) : 12, (Palternative == PAD_LESS) ?
+ "%d" : "%02d",
+ pt, ptlim);
continue;
case 'j':
- pt = _conv(t->tm_yday + 1, "%03d", pt, ptlim);
+ pt = _conv(t->tm_yday + 1, (Palternative == PAD_LESS) ?
+ "%d" : "%03d", pt, ptlim);
continue;
case 'k':
/*
@@ -215,7 +229,9 @@
** "%l" have been swapped.
** (ado, 1993-05-24)
*/
- pt = _conv(t->tm_hour, "%2d", pt, ptlim);
+ pt = _conv(t->tm_hour, (Palternative == PAD_LESS) ?
+ "%d": "%2d",
+ pt, ptlim);
continue;
#ifdef KITCHEN_SINK
case 'K':
@@ -236,14 +252,19 @@
** (ado, 1993-05-24)
*/
pt = _conv((t->tm_hour % 12) ?
- (t->tm_hour % 12) : 12,
- "%2d", pt, ptlim);
+ (t->tm_hour % 12) : 12, (Palternative == PAD_LESS) ?
+ "%d" : "%2d",
+ pt, ptlim);
continue;
case 'M':
- pt = _conv(t->tm_min, "%02d", pt, ptlim);
+ pt = _conv(t->tm_min, (Palternative == PAD_LESS) ?
+ "%d" : "%02d",
+ pt, ptlim);
continue;
case 'm':
- pt = _conv(t->tm_mon + 1, "%02d", pt, ptlim);
+ pt = _conv(t->tm_mon + 1, (Palternative == PAD_LESS) ?
+ "%d" : "%02d",
+ pt, ptlim);
continue;
case 'n':
pt = _add("\n", pt, ptlim);
@@ -262,7 +283,9 @@
warnp);
continue;
case 'S':
- pt = _conv(t->tm_sec, "%02d", pt, ptlim);
+ pt = _conv(t->tm_sec, (Palternative == PAD_LESS) ?
+ "%d" : "%02d",
+ pt, ptlim);
continue;
case 's':
{
@@ -289,8 +312,8 @@
continue;
case 'U':
pt = _conv((t->tm_yday + DAYSPERWEEK -
- t->tm_wday) / DAYSPERWEEK,
- "%02d", pt, ptlim);
+ t->tm_wday) / DAYSPERWEEK, (Palternative == PAD_LESS) ?
+ "%d" : "%02d", pt, ptlim);
continue;
case 'u':
/*
@@ -423,11 +446,13 @@
continue;
case 'y':
*warnp = IN_ALL;
- pt = _conv((t->tm_year + TM_YEAR_BASE) % 100,
- "%02d", pt, ptlim);
+ pt = _conv((t->tm_year + TM_YEAR_BASE) % 100, (Palternative == PAD_LESS) ?
+ "%d" : "%02d",
+ pt, ptlim);
continue;
case 'Y':
- pt = _conv(t->tm_year + TM_YEAR_BASE, "%04d",
+ pt = _conv(t->tm_year + TM_YEAR_BASE, (Palternative == PAD_LESS) ?
+ "%d" : "%04d",
pt, ptlim);
continue;
case 'Z':
@@ -501,6 +526,23 @@
pt = _fmt(tptr->date_fmt, t, pt, ptlim,
warnp);
continue;
+ case '-':
+ if (Palternative != PAD_DEFAULT)
+ break;
+ Palternative = PAD_LESS;
+ goto label;
+#if 0 /* XXX NOT IMPLEMENTED YET */
+ case '_':
+ if (Palternative != PAD_DEFAULT)
+ break;
+ Palternative = PAD_SPACE;
+ goto label;
+ case '0':
+ if (Palternative != PAD_DEFAULT)
+ break;
+ Palternative = PAD_ZERO;
+ goto label;
+#endif
case '%':
/*
** X311J/88-090 (4.12.3.5): if conversion char is
--
Xin LI <delphij frontfree net> http://www.delphij.net/
See complete headers for GPG key and other information.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 187 bytes
Desc: not available
Url : http://lists.freebsd.org/pipermail/freebsd-hackers/attachments/20041017/5aeea879/attachment.bin
More information about the freebsd-hackers
mailing list