.
[coreutils.git] / src / date.c
blobd6980d1e28593fcb8dccf98d6ffc1449788f464f
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 non-zero, display usage information and exit. */
54 static int show_help;
56 /* If non-zero, print the version on standard output and exit. */
57 static int show_version;
59 /* If non-zero, 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 {"set", required_argument, NULL, 's'},
68 {"uct", no_argument, NULL, 'u'},
69 {"utc", no_argument, NULL, 'u'},
70 {"universal", no_argument, NULL, 'u'},
71 {"version", no_argument, &show_version, 1},
72 {NULL, 0, NULL, 0}
75 /* Parse each line in INPUT_FILENAME as with --date and display the
76 each resulting time and date. If the file cannot be opened, tell why
77 then exit. Issue a diagnostic for any lines that cannot be parsed.
78 If any line cannot be parsed, return non-zero; otherwise return zero. */
80 static int
81 batch_convert (input_filename, format)
82 const char *input_filename;
83 const char *format;
85 int have_read_stdin;
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;
97 have_read_stdin = 1;
99 else
101 in_stream = fopen (input_filename, "r");
102 if (in_stream == NULL)
104 error (0, errno, "%s", input_filename);
106 have_read_stdin = 0;
109 line = NULL;
110 buflen = 0;
112 status = 0;
113 while (1)
115 line_length = getline (&line, &buflen, in_stream);
116 if (line_length < 0)
118 /* FIXME: detect/handle error here. */
119 break;
121 when = get_date (line, NULL);
122 if (when == -1)
124 if (line[line_length - 1] == '\n')
125 line[line_length - 1] = '\0';
126 error (0, 0, "invalid date `%s'", line);
127 status = 1;
129 else
131 show_date (format, when);
135 if (have_read_stdin && fclose (stdin) == EOF)
136 error (2, errno, "standard input");
138 if (line != NULL)
139 free (line);
141 return status;
144 void
145 main (argc, argv)
146 int argc;
147 char **argv;
149 int optc;
150 const char *datestr = NULL;
151 time_t when;
152 int set_date = 0;
153 int print_date = 0;
154 char *format;
155 char *batch_file = NULL;
156 int n_args;
157 int status;
159 program_name = argv[0];
161 while ((optc = getopt_long (argc, argv, "d:f:s:u", long_options, (int *) 0))
162 != EOF)
163 switch (optc)
165 case 0:
166 break;
167 case 'd':
168 datestr = optarg;
169 print_date = 1;
170 break;
171 case 'f':
172 batch_file = optarg;
173 break;
174 case 's':
175 datestr = optarg;
176 set_date = 1;
177 break;
178 case 'u':
179 universal_time = 1;
180 break;
181 default:
182 usage (1);
185 if (show_version)
187 printf ("date - %s\n", version_string);
188 exit (0);
191 if (show_help)
192 usage (0);
194 n_args = argc - optind;
196 if (set_date && print_date)
198 error (0, 0,
199 "the options to print and set the time may not be used together");
200 usage (1);
203 if (n_args > 1)
205 error (0, 0, "too many non-option arguments");
206 usage (1);
209 if ((set_date || print_date || batch_file != NULL)
210 && n_args == 1 && argv[optind][0] != '+')
212 error (0, 0, "\
213 when using the print, set time, or batch options, any\n\
214 non-option argument must be a format string beginning with `+'");
215 usage (1);
218 if (batch_file != NULL)
220 if (set_date || print_date)
222 error (0, 0, "\
223 neither print nor set options may be used when reading dates from a file");
224 usage (1);
226 status = batch_convert (batch_file,
227 (n_args == 1 ? argv[optind] + 1 : NULL));
229 else
231 status = 0;
233 if (!print_date && !set_date)
235 if (n_args == 1 && argv[optind][0] != '+')
237 /* Prepare to set system clock to the specified date/time
238 given in the POSIX-format. */
239 set_date = 1;
240 datestr = argv[optind];
241 when = posixtime (datestr);
242 format = NULL;
244 else
246 /* Prepare to print the current date/time. */
247 print_date = 1;
248 datestr = "undefined";
249 time (&when);
250 format = (n_args == 1 ? argv[optind] + 1 : NULL);
253 else
255 /* (print_date || set_date) */
256 when = get_date (datestr, NULL);
257 format = (n_args == 1 ? argv[optind] + 1 : NULL);
260 if (when == -1)
261 error (1, 0, "invalid date `%s'", datestr);
263 if (set_date)
265 /* Set the system clock to the specified date, then regardless of
266 the success of that operation, format and print that date. */
267 if (stime (&when) == -1)
268 error (0, errno, "cannot set date");
271 show_date (format, when);
274 if (fclose (stdout) == EOF)
275 error (2, errno, "write error");
277 exit (status);
280 /* Display the date and/or time in WHEN according to the format specified
281 in FORMAT, followed by a newline. If FORMAT is NULL, use the
282 standard output format (ctime style but with a timezone inserted). */
284 static void
285 show_date (format, when)
286 const char *format;
287 time_t when;
289 struct tm *tm;
290 char *out = NULL;
291 size_t out_length = 0;
293 tm = (universal_time ? gmtime : localtime) (&when);
295 if (format == NULL)
297 /* Print the date in the default format. Vanilla ANSI C strftime
298 doesn't support %e, but POSIX requires it. If you don't use
299 a GNU strftime, make sure yours supports %e. */
300 format = (universal_time
301 ? "%a %b %e %H:%M:%S UTC %Y"
302 : "%a %b %e %H:%M:%S %Z %Y");
304 else if (*format == '\0')
306 printf ("\n");
307 return;
312 out_length += 200;
313 out = (char *) xrealloc (out, out_length);
315 while (strftime (out, out_length, format, tm) == 0);
317 printf ("%s\n", out);
318 free (out);
321 static void
322 usage (status)
323 int status;
325 if (status != 0)
326 fprintf (stderr, "Try `%s --help' for more information.\n",
327 program_name);
328 else
330 printf ("\
331 Usage: %s [OPTION]... [+FORMAT]\n\
332 or: %s [OPTION] [MMDDhhmm[[CC]YY][.ss]]\n\
334 program_name, program_name);
335 printf ("\
336 Display the current time in the given FORMAT, or set the system date.\n\
338 -d, --date=STRING display time described by STRING, not `now'\n\
339 -f, --file=DATEFILE like --date once for each line of DATEFILE\n\
340 -s, --set=STRING set time described by STRING\n\
341 -u, --utc, --universal print or set Coordinated Universal Time\n\
342 --help display this help and exit\n\
343 --version output version information and exit\n\
345 printf ("\
347 FORMAT controls the output. The only valid option for the second form\n\
348 specifies Coordinated Universal Time. Interpreted sequences are:\n\
350 %%%% a literal %%\n\
351 %%a locale's abbreviated weekday name (Sun..Sat)\n\
352 %%A locale's full weekday name, variable length (Sunday..Saturday)\n\
353 %%b locale's abbreviated month name (Jan..Dec)\n\
354 %%B locale's full month name, variable length (January..December)\n\
355 %%c locale's date and time (Sat Nov 04 12:02:33 EST 1989)\n\
356 %%d day of month (01..31)\n\
357 %%D date (mm/dd/yy)\n\
358 %%h same as %%b\n\
359 %%H hour (00..23)\n\
360 %%I hour (01..12)\n\
361 %%j day of year (001..366)\n\
362 %%k hour ( 0..23)\n\
363 %%l hour ( 1..12)\n\
364 %%m month (01..12)\n\
365 %%M minute (00..59)\n\
366 %%n a newline\n\
367 %%p locale's AM or PM\n\
368 %%r time, 12-hour (hh:mm:ss [AP]M)\n\
369 %%s seconds since 00:00:00, Jan 1, 1970 (a GNU extension)\n\
370 %%S second (00..61)\n\
371 %%t a horizontal tab\n\
372 %%T time, 24-hour (hh:mm:ss)\n\
373 %%U week number of year with Sunday as first day of week (00..53)\n\
374 %%w day of week (0..6); 0 represents Sunday\n\
375 %%W week number of year with Monday as first day of week (00..53)\n\
376 %%x locale's date representation (mm/dd/yy)\n\
377 %%X locale's time representation (%%H:%%M:%%S)\n\
378 %%y last two digits of year (00..99)\n\
379 %%Y year (1970...)\n\
380 %%Z time zone (e.g., EDT), or nothing if no time zone is determinable\n\
382 By default, date pads numeric fields with zeroes. GNU date recognizes\n\
383 the following modifiers between `%%' and a numeric directive.\n\
385 `-' (hyphen) do not pad the field\n\
386 `_' (underscore) pad the field with spaces\n\
389 exit (status);