date: Just set TZ to handle -u flag

This simplifies things a tiny bit, and is also necessary for setting the
date since mktime operates based on the value of TZ.
This commit is contained in:
Michael Forney 2017-09-03 11:39:12 -07:00
parent 772a40188b
commit 1b41610a82

11
date.c
View File

@ -15,9 +15,8 @@ int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
struct tm *now; struct tm *now;
struct tm *(*tztime)(const time_t *) = localtime;
time_t t; time_t t;
char buf[BUFSIZ], *fmt = "%c", *tz = "local"; char buf[BUFSIZ], *fmt = "%c";
t = time(NULL); t = time(NULL);
@ -26,8 +25,8 @@ main(int argc, char *argv[])
t = estrtonum(EARGF(usage()), 0, LLONG_MAX); t = estrtonum(EARGF(usage()), 0, LLONG_MAX);
break; break;
case 'u': case 'u':
tztime = gmtime; if (setenv("TZ", "UTC0", 1) < 0)
tz = "gm"; eprintf("setenv:");
break; break;
default: default:
usage(); usage();
@ -39,8 +38,8 @@ main(int argc, char *argv[])
else else
fmt = &argv[0][1]; fmt = &argv[0][1];
} }
if (!(now = tztime(&t))) if (!(now = localtime(&t)))
eprintf("%stime failed\n", tz); eprintf("localtime:");
strftime(buf, sizeof(buf), fmt, now); strftime(buf, sizeof(buf), fmt, now);
puts(buf); puts(buf);