.
[coreutils.git] / src / date.c
blob378b302627f716b1ca9be5dfbc28facf92492e4f
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)
7 any later version.
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> */
20 #include <config.h>
21 #include <stdio.h>
22 #include <getopt.h>
23 #include <sys/types.h>
25 #include "version.h"
26 #include "system.h"
27 #include "getline.h"
28 #include "error.h"
30 #ifdef TM_IN_SYS_TIME
31 #include <sys/time.h>
32 #else
33 #include <time.h>
34 #endif
36 #ifndef STDC_HEADERS
37 size_t strftime ();
38 time_t time ();
39 #endif
41 int stime ();
43 char *xrealloc ();
44 time_t get_date ();
45 time_t posixtime ();
47 static void show_date ();
48 static void usage ();
50 /* The name this program was run with, for error messages. */
51 char *program_name;
53 /* If nonzero, display usage information and exit. */
54 static int show_help;
56 /* If nonzero, print the version on standard output and exit. */
57 static int show_version;
59 /* If nonzero, print or set Coordinated Universal Time. */
60 static int universal_time = 0;
62 static struct option const long_options[] =
64 {"date", required_argument, NULL, 'd'},
65 {"file", required_argument, NULL, 'f'},
66 {"help", no_argument, &show_help, 1},
67 {"reference", required_argument, NULL, 'r'},
68 {"set", required_argument, NULL, 's'},
69 {"uct", no_argument, NULL, 'u'},
70 {"utc", no_argument, NULL, 'u'},
71 {"universal", no_argument, NULL, 'u'},
72 {"version", no_argument, &show_version, 1},
73 {NULL, 0, NULL, 0}
76 /* Parse each line in INPUT_FILENAME as with --date and display the
77 each resulting time and date. If the file cannot be opened, tell why
78 then exit. Issue a diagnostic for any lines that cannot be parsed.
79 If any line cannot be parsed, return nonzero; otherwise return zero. */
81 static int
82 batch_convert (input_filename, format)
83 const char *input_filename;
84 const char *format;
86 int status;
87 FILE *in_stream;
88 char *line;
89 int line_length;
90 int buflen;
91 time_t when;
93 if (strcmp (input_filename, "-") == 0)
95 input_filename = _("standard input");
96 in_stream = stdin;
98 else
100 in_stream = fopen (input_filename, "r");
101 if (in_stream == NULL)
103 error (0, errno, "%s", input_filename);
107 line = NULL;
108 buflen = 0;
110 status = 0;
111 while (1)
113 line_length = getline (&line, &buflen, in_stream);
114 if (line_length < 0)
116 /* FIXME: detect/handle error here. */
117 break;
119 when = get_date (line, NULL);
120 if (when == -1)
122 if (line[line_length - 1] == '\n')
123 line[line_length - 1] = '\0';
124 error (0, 0, _("invalid date `%s'"), line);
125 status = 1;
127 else
129 show_date (format, when);
133 if (fclose (in_stream) == EOF)
134 error (2, errno, input_filename);
136 if (line != NULL)
137 free (line);
139 return status;
142 void
143 main (argc, argv)
144 int argc;
145 char **argv;
147 int optc;
148 const char *datestr = NULL;
149 const char *set_datestr = NULL;
150 time_t when;
151 int set_date = 0;
152 char *format;
153 char *batch_file = NULL;
154 char *reference = NULL;
155 struct stat refstats;
156 int n_args;
157 int status;
158 int option_specified_date;
160 program_name = argv[0];
162 while (optc = getopt_long (argc, argv, "d:f:r:s:u", long_options, (int *) 0),
163 optc != EOF)
164 switch (optc)
166 case 0:
167 break;
168 case 'd':
169 datestr = optarg;
170 break;
171 case 'f':
172 batch_file = optarg;
173 break;
174 case 'r':
175 reference = optarg;
176 break;
177 case 's':
178 set_datestr = optarg;
179 set_date = 1;
180 break;
181 case 'u':
182 universal_time = 1;
183 break;
184 default:
185 usage (1);
188 if (show_version)
190 printf ("date - %s\n", version_string);
191 exit (0);
194 if (show_help)
195 usage (0);
197 n_args = argc - optind;
199 option_specified_date = ((datestr ? 1 : 0)
200 + (batch_file ? 1 : 0)
201 + (reference ? 1 : 0));
203 if (option_specified_date > 1)
205 error (0, 0,
206 _("the options to specify dates for printing are mutually exclusive"));
207 usage (1);
210 if (set_date && option_specified_date)
212 error (0, 0,
213 _("the options to print and set the time may not be used together"));
214 usage (1);
217 if (n_args > 1)
219 error (0, 0, _("too many non-option arguments"));
220 usage (1);
223 if ((set_date || option_specified_date)
224 && n_args == 1 && argv[optind][0] != '+')
226 error (0, 0, _("\
227 when using an option to specify date(s), any\n\
228 non-option argument must be a format string beginning with `+'"));
229 usage (1);
232 if (set_date)
233 datestr = set_datestr;
235 if (batch_file != NULL)
237 status = batch_convert (batch_file,
238 (n_args == 1 ? argv[optind] + 1 : NULL));
240 else
242 status = 0;
244 if (!option_specified_date && !set_date)
246 if (n_args == 1 && argv[optind][0] != '+')
248 /* Prepare to set system clock to the specified date/time
249 given in the POSIX-format. */
250 set_date = 1;
251 datestr = argv[optind];
252 when = posixtime (datestr);
253 format = NULL;
255 else
257 /* Prepare to print the current date/time. */
258 datestr = _("undefined");
259 time (&when);
260 format = (n_args == 1 ? argv[optind] + 1 : NULL);
263 else
265 /* (option_specified_date || set_date) */
266 if (reference != NULL)
268 if (stat (reference, &refstats))
269 error (1, errno, "%s", reference);
270 when = refstats.st_mtime;
272 else
273 when = get_date (datestr, NULL);
274 format = (n_args == 1 ? argv[optind] + 1 : NULL);
277 if (when == -1)
278 error (1, 0, _("invalid date `%s'"), datestr);
280 if (set_date)
282 /* Set the system clock to the specified date, then regardless of
283 the success of that operation, format and print that date. */
284 if (stime (&when) == -1)
285 error (0, errno, _("cannot set date"));
288 show_date (format, when);
291 if (fclose (stdout) == EOF)
292 error (2, errno, _("write error"));
294 exit (status);
297 /* Display the date and/or time in WHEN according to the format specified
298 in FORMAT, followed by a newline. If FORMAT is NULL, use the
299 standard output format (ctime style but with a timezone inserted). */
301 static void
302 show_date (format, when)
303 const char *format;
304 time_t when;
306 struct tm *tm;
307 char *out = NULL;
308 size_t out_length = 0;
310 tm = (universal_time ? gmtime : localtime) (&when);
312 if (format == NULL)
314 /* Print the date in the default format. Vanilla ANSI C strftime
315 doesn't support %e, but POSIX requires it. If you don't use
316 a GNU strftime, make sure yours supports %e. */
317 format = (universal_time
318 ? "%a %b %e %H:%M:%S UTC %Y"
319 : "%a %b %e %H:%M:%S %Z %Y");
321 else if (*format == '\0')
323 printf ("\n");
324 return;
329 out_length += 200;
330 out = (char *) xrealloc (out, out_length);
332 while (strftime (out, out_length, format, tm) == 0);
334 printf ("%s\n", out);
335 free (out);
338 static void
339 usage (status)
340 int status;
342 if (status != 0)
343 fprintf (stderr, _("Try `%s --help' for more information.\n"),
344 program_name);
345 else
347 printf (_("\
348 Usage: %s [OPTION]... [+FORMAT]\n\
349 or: %s [OPTION] [MMDDhhmm[[CC]YY][.ss]]\n\
351 program_name, program_name);
352 printf (_("\
353 Display the current time in the given FORMAT, or set the system date.\n\
355 -d, --date=STRING display time described by STRING, not `now'\n\
356 -f, --file=DATEFILE like --date once for each line of DATEFILE\n\
357 -r, --reference=FILE display the last modification time of FILE\n\
358 -s, --set=STRING set time described by STRING\n\
359 -u, --utc, --universal print or set Coordinated Universal Time\n\
360 --help display this help and exit\n\
361 --version output version information and exit\n\
362 "));
363 printf (_("\
365 FORMAT controls the output. The only valid option for the second form\n\
366 specifies Coordinated Universal Time. Interpreted sequences are:\n\
368 %%%% a literal %%\n\
369 %%a locale's abbreviated weekday name (Sun..Sat)\n\
370 %%A locale's full weekday name, variable length (Sunday..Saturday)\n\
371 %%b locale's abbreviated month name (Jan..Dec)\n\
372 %%B locale's full month name, variable length (January..December)\n\
373 %%c locale's date and time (Sat Nov 04 12:02:33 EST 1989)\n\
374 %%d day of month (01..31)\n\
375 %%D date (mm/dd/yy)\n\
376 %%h same as %%b\n\
377 %%H hour (00..23)\n\
378 %%I hour (01..12)\n\
379 %%j day of year (001..366)\n\
380 %%k hour ( 0..23)\n\
381 %%l hour ( 1..12)\n\
382 %%m month (01..12)\n\
383 %%M minute (00..59)\n\
384 %%n a newline\n\
385 %%p locale's AM or PM\n\
386 %%r time, 12-hour (hh:mm:ss [AP]M)\n\
387 %%s seconds since 00:00:00, Jan 1, 1970 (a GNU extension)\n\
388 %%S second (00..61)\n\
389 %%t a horizontal tab\n\
390 %%T time, 24-hour (hh:mm:ss)\n\
391 %%U week number of year with Sunday as first day of week (00..53)\n\
392 %%w day of week (0..6); 0 represents Sunday\n\
393 %%W week number of year with Monday as first day of week (00..53)\n\
394 %%x locale's date representation (mm/dd/yy)\n\
395 %%X locale's time representation (%%H:%%M:%%S)\n\
396 %%y last two digits of year (00..99)\n\
397 %%Y year (1970...)\n\
398 %%Z time zone (e.g., EDT), or nothing if no time zone is determinable\n\
400 By default, date pads numeric fields with zeroes. GNU date recognizes\n\
401 the following modifiers between `%%' and a numeric directive.\n\
403 `-' (hyphen) do not pad the field\n\
404 `_' (underscore) pad the field with spaces\n\
405 "));
407 exit (status);