1 /* date - print or set the system date and time
2 Copyright (C) 89, 90, 91, 92, 93, 94, 1995 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 David MacKenzie <djm@gnu.ai.mit.edu> */
23 #include <sys/types.h>
48 static void show_date __P ((const char *format, time_t when));
49 static void usage __P ((int status));
51 /* The name this program was run with, for error messages. */
54 /* If nonzero, display usage information and exit. */
57 /* If nonzero, print the version on standard output and exit. */
58 static int show_version;
60 /* If non-zero, display time in RFC-822 format for mail or news. */
61 static int rfc_format = 0;
63 /* If nonzero, print or set Coordinated Universal Time. */
64 static int universal_time = 0;
66 static struct option const long_options[] =
68 {"date", required_argument, NULL, 'd'},
69 {"file", required_argument, NULL, 'f'},
70 {"help", no_argument, &show_help, 1},
71 {"reference", required_argument, NULL, 'r'},
72 {"rfc-822", no_argument, NULL, 'R'},
73 {"set", required_argument, NULL, 's'},
74 {"uct", no_argument, NULL, 'u'},
75 {"utc", no_argument, NULL, 'u'},
76 {"universal", no_argument, NULL, 'u'},
77 {"version", no_argument, &show_version, 1},
81 /* Parse each line in INPUT_FILENAME as with --date and display the
82 each resulting time and date. If the file cannot be opened, tell why
83 then exit. Issue a diagnostic for any lines that cannot be parsed.
84 If any line cannot be parsed, return nonzero; otherwise return zero. */
87 batch_convert (const char *input_filename, const char *format)
96 if (strcmp (input_filename, "-") == 0)
98 input_filename = _("standard input");
103 in_stream = fopen (input_filename, "r");
104 if (in_stream == NULL)
106 error (1, errno, "`%s'", input_filename);
116 line_length = getline (&line, &buflen, in_stream);
119 /* FIXME: detect/handle error here. */
122 when = get_date (line, NULL);
125 if (line[line_length - 1] == '\n')
126 line[line_length - 1] = '\0';
127 error (0, 0, _("invalid date `%s'"), line);
132 show_date (format, when);
136 if (fclose (in_stream) == EOF)
137 error (2, errno, input_filename);
146 main (int argc, char **argv)
149 const char *datestr = NULL;
150 const char *set_datestr = NULL;
154 char *batch_file = NULL;
155 char *reference = NULL;
156 struct stat refstats;
159 int option_specified_date;
161 program_name = argv[0];
163 while ((optc = getopt_long (argc, argv, "d:f:r:Rs:u", long_options, NULL))
182 set_datestr = optarg;
187 if (putenv ("TZ=UTC0") != 0)
188 error (1, 0, "memory exhausted");
199 printf ("date - %s\n", version_string);
206 n_args = argc - optind;
208 option_specified_date = ((datestr ? 1 : 0)
209 + (batch_file ? 1 : 0)
210 + (reference ? 1 : 0));
212 if (option_specified_date > 1)
215 _("the options to specify dates for printing are mutually exclusive"));
219 if (set_date && option_specified_date)
222 _("the options to print and set the time may not be used together"));
228 error (0, 0, _("too many non-option arguments"));
232 if ((set_date || option_specified_date)
233 && n_args == 1 && argv[optind][0] != '+')
236 when using an option to specify date(s), any\n\
237 non-option argument must be a format string beginning with `+'"));
242 datestr = set_datestr;
244 if (batch_file != NULL)
246 status = batch_convert (batch_file,
247 (n_args == 1 ? argv[optind] + 1 : NULL));
253 if (!option_specified_date && !set_date)
255 if (n_args == 1 && argv[optind][0] != '+')
257 /* Prepare to set system clock to the specified date/time
258 given in the POSIX-format. */
260 datestr = argv[optind];
261 when = posixtime (datestr);
266 /* Prepare to print the current date/time. */
267 datestr = _("undefined");
269 format = (n_args == 1 ? argv[optind] + 1 : NULL);
274 /* (option_specified_date || set_date) */
275 if (reference != NULL)
277 if (stat (reference, &refstats))
278 error (1, errno, "%s", reference);
279 when = refstats.st_mtime;
282 when = get_date (datestr, NULL);
283 format = (n_args == 1 ? argv[optind] + 1 : NULL);
287 error (1, 0, _("invalid date `%s'"), datestr);
291 /* Set the system clock to the specified date, then regardless of
292 the success of that operation, format and print that date. */
293 if (stime (&when) == -1)
294 error (0, errno, _("cannot set date"));
297 show_date (format, when);
300 if (fclose (stdout) == EOF)
301 error (2, errno, _("write error"));
306 /* Display the date and/or time in WHEN according to the format specified
307 in FORMAT, followed by a newline. If FORMAT is NULL, use the
308 standard output format (ctime style but with a timezone inserted). */
311 show_date (const char *format, time_t when)
315 size_t out_length = 0;
317 tm = localtime (&when);
321 /* Print the date in the default format. Vanilla ANSI C strftime
322 doesn't support %e, but POSIX requires it. If you don't use
323 a GNU strftime, make sure yours supports %e.
324 If you are not using GNU strftime, you want to change %z
325 in the RFC format to %Z; this gives, however, an invalid
326 RFC time format outside the continental United States and GMT. */
330 ? "%a, %_d %b %Y %H:%M:%S GMT"
331 : "%a, %_d %b %Y %H:%M:%S %z")
332 : "%a %b %e %H:%M:%S %Z %Y");
334 else if (*format == '\0')
343 out = (char *) xrealloc (out, out_length);
345 while (strftime (out, out_length, format, tm) == 0);
347 printf ("%s\n", out);
355 fprintf (stderr, _("Try `%s --help' for more information.\n"),
360 Usage: %s [OPTION]... [+FORMAT]\n\
361 or: %s [OPTION] [MMDDhhmm[[CC]YY][.ss]]\n\
363 program_name, program_name);
365 Display the current time in the given FORMAT, or set the system date.\n\
367 -d, --date=STRING display time described by STRING, not `now'\n\
368 -f, --file=DATEFILE like --date once for each line of DATEFILE\n\
369 -r, --reference=FILE display the last modification time of FILE\n\
370 -R, --rfc-822 output RFC-822 compliant date string\n\
371 -s, --set=STRING set time described by STRING\n\
372 -u, --utc, --universal print or set Coordinated Universal Time\n\
373 --help display this help and exit\n\
374 --version output version information and exit\n\
378 FORMAT controls the output. The only valid option for the second form\n\
379 specifies Coordinated Universal Time. Interpreted sequences are:\n\
382 %%a locale's abbreviated weekday name (Sun..Sat)\n\
383 %%A locale's full weekday name, variable length (Sunday..Saturday)\n\
384 %%b locale's abbreviated month name (Jan..Dec)\n\
385 %%B locale's full month name, variable length (January..December)\n\
386 %%c locale's date and time (Sat Nov 04 12:02:33 EST 1989)\n\
387 %%d day of month (01..31)\n\
388 %%D date (mm/dd/yy)\n\
392 %%j day of year (001..366)\n\
395 %%m month (01..12)\n\
396 %%M minute (00..59)\n\
398 %%p locale's AM or PM\n\
399 %%r time, 12-hour (hh:mm:ss [AP]M)\n\
400 %%s seconds since 00:00:00, Jan 1, 1970 (a GNU extension)\n\
401 %%S second (00..61)\n\
402 %%t a horizontal tab\n\
403 %%T time, 24-hour (hh:mm:ss)\n\
404 %%U week number of year with Sunday as first day of week (00..53)\n\
405 %%w day of week (0..6); 0 represents Sunday\n\
406 %%W week number of year with Monday as first day of week (00..53)\n\
407 %%x locale's date representation (mm/dd/yy)\n\
408 %%X locale's time representation (%%H:%%M:%%S)\n\
409 %%y last two digits of year (00..99)\n\
410 %%Y year (1970...)\n\
411 %%Z time zone (e.g., EDT), or nothing if no time zone is determinable\n\
413 By default, date pads numeric fields with zeroes. GNU date recognizes\n\
414 the following modifiers between `%%' and a numeric directive.\n\
416 `-' (hyphen) do not pad the field\n\
417 `_' (underscore) pad the field with spaces\n\