From: ori@eigenstate.org
To: 9front@9front.org
Subject: date(1) cleanup
Date: Sun, 14 Jun 2020 11:25:23 -0700 [thread overview]
Message-ID: <B22DD049AD655F46E7F59B33AC50C13D@eigenstate.org> (raw)
Simplify using tmdate(2). Also add '-f' for custom formats,
since it costs almost nothing.
diff -r b07da9e4e4d2 sys/man/1/date
--- a/sys/man/1/date Sun Jun 14 09:33:32 2020 -0700
+++ b/sys/man/1/date Sun Jun 14 11:23:56 2020 -0700
@@ -4,8 +4,12 @@
.SH SYNOPSIS
.B date
[
-.I option
-] [
+.I -u
+]
+[
+.I -nitm | -f fmt
+]
+[
.I seconds
]
.br
@@ -33,6 +37,12 @@
.TP
.B -m
Report the date as an email compatible (RFC2822) time stamp.
+.TP
+.B -f
+.I fmt
+Report the date using a
+.IR tmdate (1)
+compatible format.
.PP
The conversion from Greenwich Mean Time to local time depends on the
.B $timezone
diff -r b07da9e4e4d2 sys/src/cmd/date.c
--- a/sys/src/cmd/date.c Sun Jun 14 09:33:32 2020 -0700
+++ b/sys/src/cmd/date.c Sun Jun 14 11:23:56 2020 -0700
@@ -1,101 +1,57 @@
#include <u.h>
#include <libc.h>
-static char *day[] = {
- "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
-};
-
-static char *mon[] = {
- "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
- "Aug", "Sep", "Oct", "Nov", "Dec"
-};
-
-int uflg, nflg, iflg, tflg, mflg;
-
-char*
-isodate(Tm *t)
+void
+usage(void)
{
- static char c[26]; /* leave room to append isotime */
- snprint(c, 11, "%04d-%02d-%02d",
- t->year + 1900, t->mon + 1, t->mday);
- return c;
-}
-
-char*
-isotime(Tm *t)
-{
- int tz;
- char *c, *d;
- d = isodate(t);
- c = d+10;
- snprint(c, 10, "T%02d:%02d:%02d",
- t->hour, t->min, t->sec); /* append to isodate */
- tz = t->tzoff / 60;
- if(t->tzoff) {
- /* localtime */
- if (t->tzoff > 0) {
- c[9] = '+';
- } else {
- c[9] = '-';
- tz = -tz;
- }
- snprint(c+10, 6, "%02d:%02d", tz / 60, tz % 60);
- } else {
- c[9] = 'Z';
- c[10] = 0;
- }
- return d;
-}
-
-char *
-mailtime(Tm *t)
-{
- static char c[64];
- char *sgn;
- int off;
-
- sgn = "+";
- if(t->tzoff < 0)
- sgn = "";
- off = (t->tzoff/3600)*100 + (t->tzoff/60)%60;
- snprint(c, sizeof(c), "%s, %.2d %s %.4d %.2d:%.2d:%.2d %s%.4d",
- day[t->wday], t->mday, mon[t->mon], t->year + 1900,
- t->hour, t->min, t->sec, sgn, off);
- return c;
+ fprint(2, "usage: date [-itunm] [seconds]\n");
+ exits("usage");
}
void
main(int argc, char *argv[])
{
- ulong now;
- Tm *tm;
+ char *fmt;
+ vlong now;
+ Tzone *tz;
+ Tm tm;
+
+ now = 0;
+ tz = nil;
+ fmt = "W MMM _D hh:mm:ss ZZZ YYYY";
+ tmfmtinstall();
+
ARGBEGIN{
- case 'n': nflg = 1; break;
- case 'u': uflg = 1; break;
- case 't': tflg = 1; /* implies -i */
- case 'i': iflg = 1; break;
- case 'm': mflg = 1; break;
- default: fprint(2, "usage: date [-itunm] [seconds]\n"); exits("usage");
+ case 'n': nflg = 1; break;
+ case 'u': uflg = 1; break;
+ case 't': fmt = "YYYY-MM-DDThh:mm:ssZZ"; break;
+ case 'i': fmt = "YYYY-MM-DD"; break;
+ case 'm': fmt = "W, DD MMM YYYY hh:mm:ss Z"; break;
+ case 'f': fmt = EARGF(usage()); break;
+ default: usage();
}ARGEND
- if(argc == 1)
- now = strtoul(*argv, 0, 0);
- else
+ switch(argc) {
+ case 0:
now = time(0);
+ break;
+ case 1:
+ now = strtoll(argv[0], nil, 0);
+ break;
+ default:
+ usage();
+ break;
+ }
+
+ if(!uflg && (tz = tmgetzone("local")) == nil)
+ sysfatal("timezone: %r");
+ if(tmtime(&tm, now, tz) == nil)
+ sysfatal("now: %r");
if(nflg)
- print("%ld\n", now);
- else {
- tm = uflg ? gmtime(now) : localtime(now);
- if(iflg) {
- if(tflg)
- print("%s\n", isotime(tm));
- else
- print("%s\n", isodate(tm));
- } else if(mflg)
- print("%s\n", mailtime(tm));
- else
- print("%s", asctime(tm));
- }
+ print("%lld\n", tm.abs);
+ else
+ if(print("%τ\n", tmfmt(&tm, fmt)) == -1)
+ sysfatal("%r");
exits(0);
}
next reply other threads:[~2020-06-14 18:25 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-06-14 18:25 ori [this message]
2020-06-14 21:46 ` [9front] " sirjofri
2020-06-15 6:04 ` ori
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=B22DD049AD655F46E7F59B33AC50C13D@eigenstate.org \
--to=ori@eigenstate.org \
--cc=9front@9front.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).