Add RFC2822 (email style) formatted dates to to date(1).

This commit is contained in:
Ori Bernstein 2019-09-06 08:25:21 -07:00
parent 0cb4115b82
commit 8cbe3772c4
2 changed files with 35 additions and 3 deletions

View file

@ -30,6 +30,9 @@ Report the date as ISO-8601 without time and timezone suffix.
.TP
.B -t
Report the date as ISO-8601 with time and timezone suffix.
.TP
.B -m
Report the date as an email compatible (RFC2822) time stamp.
.PP
The conversion from Greenwich Mean Time to local time depends on the
.B $timezone

View file

@ -1,7 +1,16 @@
#include <u.h>
#include <libc.h>
int uflg, nflg, iflg, tflg;
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)
@ -38,6 +47,23 @@ isotime(Tm *t)
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;
}
void
main(int argc, char *argv[])
{
@ -48,7 +74,8 @@ main(int argc, char *argv[])
case 'u': uflg = 1; break;
case 't': tflg = 1; /* implies -i */
case 'i': iflg = 1; break;
default: fprint(2, "usage: date [-itun] [seconds]\n"); exits("usage");
case 'm': mflg = 1; break;
default: fprint(2, "usage: date [-itunm] [seconds]\n"); exits("usage");
}ARGEND
if(argc == 1)
@ -65,7 +92,9 @@ main(int argc, char *argv[])
print("%s\n", isotime(tm));
else
print("%s\n", isodate(tm));
} else
} else if(mflg)
print("%s\n", mailtime(tm));
else
print("%s", asctime(tm));
}
exits(0);