115 lines
2.0 KiB
C
115 lines
2.0 KiB
C
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#include <stdlib.h>
|
|
#include <errno.h>
|
|
#include <time.h>
|
|
#include <sys/stat.h>
|
|
#include "get_stat.h"
|
|
|
|
const char *fmts[] = {
|
|
"%R",
|
|
"%T",
|
|
"%m.%d-%R",
|
|
"%m.%d-%T",
|
|
"%Y.%m.%d-%R",
|
|
"%Y.%m.%d-%T",
|
|
"%Y-%m-%d %R %z",
|
|
"%Y-%m-%d %T %z",
|
|
"%T %d-%m-%Y",
|
|
"%H %d-%m-%Y",
|
|
"%Y-%m-%d %H",
|
|
"%Y-%m-%d",
|
|
"%d-%m-%Y"
|
|
};
|
|
|
|
time_t parse_date(char *str) {
|
|
time_t local = time(NULL);
|
|
struct tm *tm = localtime(&local);
|
|
|
|
for (size_t i = 0; i < sizeof(fmts) / sizeof(char *); i++) {
|
|
char *res = strptime(str, fmts[i], tm);
|
|
if (res && *res == '\0')
|
|
break;
|
|
}
|
|
|
|
time_t rt = mktime(tm);
|
|
if (rt == -1) {
|
|
fprintf(stderr, "date: %s\n", strerror(errno));
|
|
exit(1);
|
|
}
|
|
|
|
return rt;
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
time_t t = time(NULL);
|
|
char *fmt = "%a %b %e %H:%M:%S %Z %Y";
|
|
char *r_flag = 0;
|
|
|
|
/* For -s flag */
|
|
struct timespec ts;
|
|
|
|
int opt;
|
|
while ((opt = getopt(argc, argv, "s:d:r:u")) != -1) {
|
|
switch (opt) {
|
|
case 'r':
|
|
r_flag = optarg;
|
|
break;
|
|
|
|
case 's':
|
|
ts.tv_sec = parse_date(optarg);
|
|
if (clock_settime(CLOCK_REALTIME, &ts) < 0) {
|
|
fprintf(stderr, "date: %s\n", strerror(errno));
|
|
return 1;
|
|
}
|
|
|
|
return 0;
|
|
|
|
case 'd':
|
|
t = parse_date(optarg);
|
|
break;
|
|
|
|
case 'u':
|
|
if (setenv("TZ", "UTC0", 1) < 0) {
|
|
fprintf(stderr, "date: %s\n", strerror(errno));
|
|
return 1;
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
printf("date [+\"fmt\"]\n\t[-s DATE Set new date]\n\t[-d DATE Print new date]\n\t[-u Work in UTC]\n\t[-r FILE Display last modification time of FILE]\n");
|
|
printf("\nFormats:\n");
|
|
for (size_t i = 0; i < sizeof(fmts) / sizeof(char *); i++)
|
|
printf("\t%s\n", fmts[i]);
|
|
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
argv += optind;
|
|
argc -= optind;
|
|
|
|
if (argc != 0)
|
|
if (argv[0][0] == '+')
|
|
fmt = argv[0] + 1;
|
|
|
|
struct stat sb;
|
|
if (r_flag) {
|
|
if (mu_get_stat("date", r_flag, &sb))
|
|
return 1;
|
|
|
|
t = sb.st_mtime;
|
|
}
|
|
|
|
struct tm *tm = localtime(&t);
|
|
|
|
char buf[256];
|
|
strftime(buf, sizeof(buf), fmt, tm);
|
|
|
|
puts(buf);
|
|
return 0;
|
|
}
|
|
|