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>
47 static void show_date ();
50 /* The name this program was run with, for error messages. */
53 /* If non-zero, display usage information and exit. */
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},
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. */
81 batch_convert (input_filename
, format
)
82 const char *input_filename
;
93 if (strcmp (input_filename
, "-") == 0)
95 input_filename
= "standard input";
101 in_stream
= fopen (input_filename
, "r");
102 if (in_stream
== NULL
)
104 error (0, errno
, "%s", input_filename
);
115 line_length
= getline (&line
, &buflen
, in_stream
);
118 /* FIXME: detect/handle error here. */
121 when
= get_date (line
, NULL
);
124 if (line
[line_length
- 1] == '\n')
125 line
[line_length
- 1] = '\0';
126 error (0, 0, "invalid date `%s'", line
);
131 show_date (format
, when
);
135 if (have_read_stdin
&& fclose (stdin
) == EOF
)
136 error (2, errno
, "standard input");
150 const char *datestr
= NULL
;
155 char *batch_file
= NULL
;
159 program_name
= argv
[0];
161 while ((optc
= getopt_long (argc
, argv
, "d:f:s:u", long_options
, (int *) 0))
187 printf ("date - %s\n", version_string
);
194 n_args
= argc
- optind
;
196 if (set_date
&& print_date
)
199 "the options to print and set the time may not be used together");
205 error (0, 0, "too many non-option arguments");
209 if ((set_date
|| print_date
|| batch_file
!= NULL
)
210 && n_args
== 1 && argv
[optind
][0] != '+')
213 when using the print, set time, or batch options, any\n\
214 non-option argument must be a format string beginning with `+'");
218 if (batch_file
!= NULL
)
220 if (set_date
|| print_date
)
223 neither print nor set options may be used when reading dates from a file");
226 status
= batch_convert (batch_file
,
227 (n_args
== 1 ? argv
[optind
] + 1 : NULL
));
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. */
240 datestr
= argv
[optind
];
241 when
= posixtime (datestr
);
246 /* Prepare to print the current date/time. */
248 datestr
= "undefined";
250 format
= (n_args
== 1 ? argv
[optind
] + 1 : NULL
);
255 /* (print_date || set_date) */
256 when
= get_date (datestr
, NULL
);
257 format
= (n_args
== 1 ? argv
[optind
] + 1 : NULL
);
261 error (1, 0, "invalid date `%s'", datestr
);
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");
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). */
285 show_date (format
, when
)
291 size_t out_length
= 0;
293 tm
= (universal_time
? gmtime
: localtime
) (&when
);
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')
313 out
= (char *) xrealloc (out
, out_length
);
315 while (strftime (out
, out_length
, format
, tm
) == 0);
317 printf ("%s\n", out
);
326 fprintf (stderr
, "Try `%s --help' for more information.\n",
331 Usage: %s [OPTION]... [+FORMAT]\n\
332 or: %s [OPTION] [MMDDhhmm[[CC]YY][.ss]]\n\
334 program_name
, program_name
);
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\
347 FORMAT controls the output. The only valid option for the second form\n\
348 specifies Coordinated Universal Time. Interpreted sequences are:\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\
361 %%j day of year (001..366)\n\
364 %%m month (01..12)\n\
365 %%M minute (00..59)\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\