summaryrefslogtreecommitdiff
path: root/src/date.c
diff options
context:
space:
mode:
authorJim Meyering <jim@meyering.net>1994-09-17 00:17:25 +0000
committerJim Meyering <jim@meyering.net>1994-09-17 00:17:25 +0000
commitb4baf84d322bb09eb218c0b7cd77e7d155c4f02b (patch)
treec64c2af755594008fb1ac6149d41db1ac14c7b93 /src/date.c
parent1401f3e3f991ac4382d6ae1aeea6227c1cf5b24a (diff)
downloadcoreutils-b4baf84d322bb09eb218c0b7cd77e7d155c4f02b.tar.xz
.
Diffstat (limited to 'src/date.c')
-rw-r--r--src/date.c72
1 files changed, 57 insertions, 15 deletions
diff --git a/src/date.c b/src/date.c
index 4e6f99744..cb3a9c4d1 100644
--- a/src/date.c
+++ b/src/date.c
@@ -110,6 +110,9 @@ main (argc, argv)
char *datestr = NULL;
time_t when;
int set_date = 0;
+ int print_date = 0;
+ char *format;
+ int n_args;
program_name = argv[0];
@@ -121,6 +124,7 @@ main (argc, argv)
break;
case 'd':
datestr = optarg;
+ print_date = 1;
break;
case 's':
datestr = optarg;
@@ -142,30 +146,68 @@ main (argc, argv)
if (show_help)
usage (0);
- if (argc - optind > 1)
- usage (1);
+ n_args = argc - optind;
- time (&when);
+ if (set_date && print_date)
+ {
+ error (0, 0,
+ "the options to print and set the time may not be used together");
+ usage (1);
+ }
- if (datestr)
- when = get_date (datestr, NULL);
+ if (n_args > 1)
+ {
+ error (0, 0, "too many non-option arguments");
+ usage (1);
+ }
- if (argc - optind == 1 && argv[optind][0] != '+')
+ if ((set_date || print_date) && n_args == 1 && argv[optind][0] != '+')
{
- when = posixtime (argv[optind]);
- set_date = 1;
+ error (0, 0, "\
+when using the print or set time option, the sole\n\
+non-option argument must be a format string beginning with `+'");
+ usage (1);
+ }
+
+ if (!print_date && !set_date)
+ {
+ if (n_args == 1 && argv[optind][0] != '+')
+ {
+ /* Prepare to set system clock to the specified date/time given in
+ the POSIX-format. */
+ set_date = 1;
+ datestr = argv[optind];
+ when = posixtime (datestr);
+ format = NULL;
+ }
+ else
+ {
+ /* Prepare to print the current date/time. */
+ print_date = 1;
+ datestr = "undefined";
+ time (&when);
+ format = (n_args == 1 ? argv[optind] + 1 : NULL);
+ }
+ }
+ else
+ {
+ /* (print_date || set_date) */
+ when = get_date (datestr, NULL);
+ format = (n_args == 1 ? argv[optind] + 1 : NULL);
}
if (when == -1)
- error (1, 0, "invalid date");
+ error (1, 0, "invalid date `%s'", datestr);
- if (set_date && stime (&when) == -1)
- error (0, errno, "cannot set date");
+ if (set_date)
+ {
+ /* Set the system clock to the specified date, then regardless of
+ the success of that operation, format and print that date. */
+ if (stime (&when) == -1)
+ error (0, errno, "cannot set date");
+ }
- if (argc - optind == 1 && argv[optind][0] == '+')
- show_date (argv[optind] + 1, when);
- else
- show_date ((char *) NULL, when);
+ show_date (format, when);
exit (0);
}