This commit is contained in:
Connor Lane Smith
2011-05-24 01:13:34 +01:00
parent 687e5411ee
commit 9714d7b1d3
8 changed files with 103 additions and 90 deletions

25
date.c
View File

@@ -1,29 +1,30 @@
/* See LICENSE file for copyright and license details. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "util.h"
int
main(int argc, char *argv[])
{
char buf[BUFSIZ];
char buf[BUFSIZ], c;
char *fmt = "%c";
int i;
struct tm *now = NULL;
time_t t;
t = time(NULL);
for(i = 1; i < argc; i++)
if(!strncmp("+", argv[i], 1))
fmt = &argv[i][1];
else if(!strcmp("-d", argv[i]) && i+1 < argc)
t = strtol(argv[++i], NULL, 0);
else
eprintf("usage: %s [-d time] [+format]\n", argv[0]);
now = localtime(&t);
if(!now)
while((c = getopt(argc, argv, "d:")) != -1)
switch(c) {
case 'd':
t = strtol(optarg, NULL, 0);
break;
default:
exit(EXIT_FAILURE);
}
if(optind < argc && argv[optind][0] == '+')
fmt = &argv[optind][1];
if(!(now = localtime(&t)))
eprintf("localtime failed\n");
strftime(buf, sizeof buf, fmt, now);