Fixup date

This commit is contained in:
K. Lange 2018-08-21 22:31:26 +09:00
parent 605fef6b0c
commit 058e84bfd8

View File

@ -15,39 +15,45 @@
*/ */
#include <stdio.h> #include <stdio.h>
#include <time.h> #include <time.h>
#include <unistd.h>
#include <sys/time.h> #include <sys/time.h>
/* XXX Why do we have our own list of weekdays */ static void show_usage(int argc, char * argv[]) {
char * daysofweeks[] = { printf(
"Sunday", "%s - print the time and day\n"
"Monday", "\n"
"Tuesday", "usage: %s [-?] +FORMAT\n"
"Wednesday", "\n"
"Thursday", " Note: This implementation is not currently capable of\n"
"Friday", " setting the system time.\n"
"Saturday" "\n"
}; " -? \033[3mshow this help text\033[0m\n"
"\n", argv[0], argv[0]);
void print_time(time_t time) {
struct tm * date = localtime(&time);
if (!date) {
fprintf(stderr, "Failure.\n");
} else {
printf("%d-%02d-%02d %02d:%02d:%02d (%s, day %d)\n",
date->tm_year + 1900,
date->tm_mon + 1,
date->tm_mday,
date->tm_hour,
date->tm_min,
date->tm_sec,
daysofweeks[date->tm_wday],
date->tm_yday);
}
} }
int main(int argc, char * argv[]) { int main(int argc, char * argv[]) {
struct timeval tv; char * format = "%a %b %d %T %Y";
gettimeofday(&tv, NULL); struct tm * timeinfo;
print_time(tv.tv_sec); struct timeval now;
char buf[BUFSIZ] = {0};
int opt;
while ((opt = getopt(argc,argv,"?")) != -1) {
switch (opt) {
case '?':
show_usage(argc,argv);
return 1;
}
}
if (optind < argc && *argv[optind] == '+') {
format = &argv[optind][1];
}
gettimeofday(&now, NULL); //time(NULL);
timeinfo = localtime((time_t *)&now.tv_sec);
strftime(buf,BUFSIZ,format,timeinfo);
puts(buf);
return 0; return 0;
} }