summaryrefslogtreecommitdiffstats
path: root/bin/date/date.c
diff options
context:
space:
mode:
authorsheldonh <sheldonh@FreeBSD.org>1999-12-29 16:50:08 +0000
committersheldonh <sheldonh@FreeBSD.org>1999-12-29 16:50:08 +0000
commit506b57590bdf57e2248ebd3c429fe94984702ce2 (patch)
treea571b5552f60d707e0d287270c0057a1915bd465 /bin/date/date.c
parente30fec6f248668aa32418bf2be2337b9a0a0e14a (diff)
downloadFreeBSD-src-506b57590bdf57e2248ebd3c429fe94984702ce2.zip
FreeBSD-src-506b57590bdf57e2248ebd3c429fe94984702ce2.tar.gz
Back out previous commit and replace with a cleaner solution adapted
from the source attributed below. In particular, this removes a goto inside a switch and replaces those horrendous ATOI macros with something acceptable. More clean-ups to come. PR: bin/14151 Reported by: Christian Weisgerber <naddy@mips.rhein-neckar.de> Obtained from: NetBSD
Diffstat (limited to 'bin/date/date.c')
-rw-r--r--bin/date/date.c32
1 files changed, 21 insertions, 11 deletions
diff --git a/bin/date/date.c b/bin/date/date.c
index 4d4c9cd..fcd9069 100644
--- a/bin/date/date.c
+++ b/bin/date/date.c
@@ -60,6 +60,10 @@ static const char rcsid[] =
#include "extern.h"
#include "vary.h"
+#ifndef TM_YEAR_BASE
+#define TM_YEAR_BASE 1900
+#endif
+
time_t tval;
int retval;
@@ -174,9 +178,8 @@ main(argc, argv)
exit(retval);
}
-#define ATOI2(ar) ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
-#define ATOI4(ar) ((ar)[0] - '0') * 1000 + ((ar)[1] - '0') * 100 + \
- ((ar)[2] - '0') * 10 + ((ar)[3] - '0'); (ar) += 4;
+#define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
+
void
setthetime(fmt, p, jflag, nflag)
const char *fmt;
@@ -186,6 +189,7 @@ setthetime(fmt, p, jflag, nflag)
register struct tm *lt;
struct timeval tv;
const char *dot, *t;
+ int century;
if (fmt != NULL) {
lt = localtime(&tval);
@@ -221,18 +225,24 @@ setthetime(fmt, p, jflag, nflag)
} else
lt->tm_sec = 0;
+ century = 0;
/* if p has a ".ss" field then let's pretend it's not there */
switch (strlen(p) - ((dot != NULL) ? 3 : 0)) {
case 12: /* cc */
- lt->tm_year = -1900 + ATOI4(p);
- if (lt->tm_year < 0)
- badformat();
- goto year_done;
+ lt->tm_year = ATOI2(p) * 100 - TM_YEAR_BASE;
+ century = 1;
+ /* FALLTHROUGH */
case 10: /* yy */
- lt->tm_year = ATOI2(p);
- if (lt->tm_year < 69) /* hack for 2000 ;-} */
- lt->tm_year += 100;
-year_done: /* FALLTHROUGH */
+ if (century)
+ lt->tm_year += ATOI2(p);
+ else { /* hack for 2000 ;-} */
+ lt->tm_year = ATOI2(p);
+ if (lt->tm_year < 69)
+ lt->tm_year += 2000 - TM_YEAR_BASE;
+ else
+ lt->tm_year += 1900 - TM_YEAR_BASE;
+ }
+ /* FALLTHROUGH */
case 8: /* mm */
lt->tm_mon = ATOI2(p);
if (lt->tm_mon > 12)
OpenPOWER on IntegriCloud