dd: synchronize output after write errors
[coreutils.git] / src / date.c
blob18ff22287e61cc2728215317f039e4894a4b6163
1 /* date - print or set the system date and time
2 Copyright (C) 1989-2022 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 3 of the License, or
7 (at your option) 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, see <https://www.gnu.org/licenses/>.
17 David MacKenzie <djm@gnu.ai.mit.edu> */
19 #include <config.h>
20 #include <stdio.h>
21 #include <getopt.h>
22 #include <sys/types.h>
23 #if HAVE_LANGINFO_CODESET
24 # include <langinfo.h>
25 #endif
27 #include "system.h"
28 #include "argmatch.h"
29 #include "die.h"
30 #include "error.h"
31 #include "parse-datetime.h"
32 #include "posixtm.h"
33 #include "quote.h"
34 #include "stat-time.h"
35 #include "fprintftime.h"
37 /* The official name of this program (e.g., no 'g' prefix). */
38 #define PROGRAM_NAME "date"
40 #define AUTHORS proper_name ("David MacKenzie")
42 static bool show_date (char const *, struct timespec, timezone_t);
44 enum Time_spec
46 /* Display only the date. */
47 TIME_SPEC_DATE,
48 /* Display date, hours, minutes, and seconds. */
49 TIME_SPEC_SECONDS,
50 /* Similar, but display nanoseconds. */
51 TIME_SPEC_NS,
53 /* Put these last, since they aren't valid for --rfc-3339. */
55 /* Display date and hour. */
56 TIME_SPEC_HOURS,
57 /* Display date, hours, and minutes. */
58 TIME_SPEC_MINUTES
61 static char const *const time_spec_string[] =
63 /* Put "hours" and "minutes" first, since they aren't valid for
64 --rfc-3339. */
65 "hours", "minutes",
66 "date", "seconds", "ns", NULL
68 static enum Time_spec const time_spec[] =
70 TIME_SPEC_HOURS, TIME_SPEC_MINUTES,
71 TIME_SPEC_DATE, TIME_SPEC_SECONDS, TIME_SPEC_NS
73 ARGMATCH_VERIFY (time_spec_string, time_spec);
75 /* A format suitable for Internet RFCs 5322, 2822, and 822. */
76 static char const rfc_email_format[] = "%a, %d %b %Y %H:%M:%S %z";
78 /* For long options that have no equivalent short option, use a
79 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
80 enum
82 DEBUG_DATE_PARSING_OPTION = CHAR_MAX + 1,
83 RESOLUTION_OPTION,
84 RFC_3339_OPTION
87 static char const short_options[] = "d:f:I::r:Rs:u";
89 static struct option const long_options[] =
91 {"date", required_argument, NULL, 'd'},
92 {"debug", no_argument, NULL, DEBUG_DATE_PARSING_OPTION},
93 {"file", required_argument, NULL, 'f'},
94 {"iso-8601", optional_argument, NULL, 'I'},
95 {"reference", required_argument, NULL, 'r'},
96 {"resolution", no_argument, NULL, RESOLUTION_OPTION},
97 {"rfc-email", no_argument, NULL, 'R'},
98 {"rfc-822", no_argument, NULL, 'R'},
99 {"rfc-2822", no_argument, NULL, 'R'},
100 {"rfc-3339", required_argument, NULL, RFC_3339_OPTION},
101 {"set", required_argument, NULL, 's'},
102 {"uct", no_argument, NULL, 'u'},
103 {"utc", no_argument, NULL, 'u'},
104 {"universal", no_argument, NULL, 'u'},
105 {GETOPT_HELP_OPTION_DECL},
106 {GETOPT_VERSION_OPTION_DECL},
107 {NULL, 0, NULL, 0}
110 /* flags for parse_datetime2 */
111 static unsigned int parse_datetime_flags;
113 #if LOCALTIME_CACHE
114 # define TZSET tzset ()
115 #else
116 # define TZSET /* empty */
117 #endif
119 #ifdef _DATE_FMT
120 # define DATE_FMT_LANGINFO() nl_langinfo (_DATE_FMT)
121 #else
122 # define DATE_FMT_LANGINFO() ""
123 #endif
125 void
126 usage (int status)
128 if (status != EXIT_SUCCESS)
129 emit_try_help ();
130 else
132 printf (_("\
133 Usage: %s [OPTION]... [+FORMAT]\n\
134 or: %s [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]\n\
136 program_name, program_name);
137 fputs (_("\
138 Display the current time in the given FORMAT, or set the system date.\n\
139 "), stdout);
141 emit_mandatory_arg_note ();
143 fputs (_("\
144 -d, --date=STRING display time described by STRING, not 'now'\n\
145 "), stdout);
146 fputs (_("\
147 --debug annotate the parsed date,\n\
148 and warn about questionable usage to stderr\n\
149 "), stdout);
150 fputs (_("\
151 -f, --file=DATEFILE like --date; once for each line of DATEFILE\n\
152 "), stdout);
153 fputs (_("\
154 -I[FMT], --iso-8601[=FMT] output date/time in ISO 8601 format.\n\
155 FMT='date' for date only (the default),\n\
156 'hours', 'minutes', 'seconds', or 'ns'\n\
157 for date and time to the indicated precision.\n\
158 Example: 2006-08-14T02:34:56-06:00\n\
159 "), stdout);
160 fputs (_("\
161 --resolution output the available resolution of timestamps\n\
162 Example: 0.000000001\n\
163 "), stdout);
164 fputs (_("\
165 -R, --rfc-email output date and time in RFC 5322 format.\n\
166 Example: Mon, 14 Aug 2006 02:34:56 -0600\n\
167 "), stdout);
168 fputs (_("\
169 --rfc-3339=FMT output date/time in RFC 3339 format.\n\
170 FMT='date', 'seconds', or 'ns'\n\
171 for date and time to the indicated precision.\n\
172 Example: 2006-08-14 02:34:56-06:00\n\
173 "), stdout);
174 fputs (_("\
175 -r, --reference=FILE display the last modification time of FILE\n\
176 "), stdout);
177 fputs (_("\
178 -s, --set=STRING set time described by STRING\n\
179 -u, --utc, --universal print or set Coordinated Universal Time (UTC)\n\
180 "), stdout);
181 fputs (HELP_OPTION_DESCRIPTION, stdout);
182 fputs (VERSION_OPTION_DESCRIPTION, stdout);
183 fputs (_("\
185 FORMAT controls the output. Interpreted sequences are:\n\
187 %% a literal %\n\
188 %a locale's abbreviated weekday name (e.g., Sun)\n\
189 "), stdout);
190 fputs (_("\
191 %A locale's full weekday name (e.g., Sunday)\n\
192 %b locale's abbreviated month name (e.g., Jan)\n\
193 %B locale's full month name (e.g., January)\n\
194 %c locale's date and time (e.g., Thu Mar 3 23:05:25 2005)\n\
195 "), stdout);
196 fputs (_("\
197 %C century; like %Y, except omit last two digits (e.g., 20)\n\
198 %d day of month (e.g., 01)\n\
199 %D date; same as %m/%d/%y\n\
200 %e day of month, space padded; same as %_d\n\
201 "), stdout);
202 fputs (_("\
203 %F full date; like %+4Y-%m-%d\n\
204 %g last two digits of year of ISO week number (see %G)\n\
205 %G year of ISO week number (see %V); normally useful only with %V\n\
206 "), stdout);
207 fputs (_("\
208 %h same as %b\n\
209 %H hour (00..23)\n\
210 %I hour (01..12)\n\
211 %j day of year (001..366)\n\
212 "), stdout);
213 fputs (_("\
214 %k hour, space padded ( 0..23); same as %_H\n\
215 %l hour, space padded ( 1..12); same as %_I\n\
216 %m month (01..12)\n\
217 %M minute (00..59)\n\
218 "), stdout);
219 fputs (_("\
220 %n a newline\n\
221 %N nanoseconds (000000000..999999999)\n\
222 %p locale's equivalent of either AM or PM; blank if not known\n\
223 %P like %p, but lower case\n\
224 %q quarter of year (1..4)\n\
225 %r locale's 12-hour clock time (e.g., 11:11:04 PM)\n\
226 %R 24-hour hour and minute; same as %H:%M\n\
227 %s seconds since the Epoch (1970-01-01 00:00 UTC)\n\
228 "), stdout);
229 fputs (_("\
230 %S second (00..60)\n\
231 %t a tab\n\
232 %T time; same as %H:%M:%S\n\
233 %u day of week (1..7); 1 is Monday\n\
234 "), stdout);
235 fputs (_("\
236 %U week number of year, with Sunday as first day of week (00..53)\n\
237 %V ISO week number, with Monday as first day of week (01..53)\n\
238 %w day of week (0..6); 0 is Sunday\n\
239 %W week number of year, with Monday as first day of week (00..53)\n\
240 "), stdout);
241 fputs (_("\
242 %x locale's date representation (e.g., 12/31/99)\n\
243 %X locale's time representation (e.g., 23:13:48)\n\
244 %y last two digits of year (00..99)\n\
245 %Y year\n\
246 "), stdout);
247 fputs (_("\
248 %z +hhmm numeric time zone (e.g., -0400)\n\
249 %:z +hh:mm numeric time zone (e.g., -04:00)\n\
250 %::z +hh:mm:ss numeric time zone (e.g., -04:00:00)\n\
251 %:::z numeric time zone with : to necessary precision (e.g., -04, +05:30)\n\
252 %Z alphabetic time zone abbreviation (e.g., EDT)\n\
254 By default, date pads numeric fields with zeroes.\n\
255 "), stdout);
256 fputs (_("\
257 The following optional flags may follow '%':\n\
259 - (hyphen) do not pad the field\n\
260 _ (underscore) pad with spaces\n\
261 0 (zero) pad with zeros\n\
262 + pad with zeros, and put '+' before future years with >4 digits\n\
263 ^ use upper case if possible\n\
264 # use opposite case if possible\n\
265 "), stdout);
266 fputs (_("\
268 After any flags comes an optional field width, as a decimal number;\n\
269 then an optional modifier, which is either\n\
270 E to use the locale's alternate representations if available, or\n\
271 O to use the locale's alternate numeric symbols if available.\n\
272 "), stdout);
273 fputs (_("\
275 Examples:\n\
276 Convert seconds since the Epoch (1970-01-01 UTC) to a date\n\
277 $ date --date='@2147483647'\n\
279 Show the time on the west coast of the US (use tzselect(1) to find TZ)\n\
280 $ TZ='America/Los_Angeles' date\n\
282 Show the local time for 9AM next Friday on the west coast of the US\n\
283 $ date --date='TZ=\"America/Los_Angeles\" 09:00 next Fri'\n\
284 "), stdout);
285 emit_ancillary_info (PROGRAM_NAME);
287 exit (status);
290 /* Yield the number of decimal digits needed to output a time with the
291 nanosecond resolution RES, without losing information. */
293 static int
294 res_width (long int res)
296 int i = 9;
297 for (long long int r = 1; (r *= 10) <= res; )
298 i--;
299 return i;
302 /* Return a newly allocated copy of FORMAT with each "%-N" adjusted to
303 be "%9N", "%6N", or whatever other resolution is appropriate for
304 the current platform. If no "%-N" appears, return NULL. */
306 static char *
307 adjust_resolution (char const *format)
309 char *copy = NULL;
311 for (char const *f = format; *f; f++)
312 if (f[0] == '%' && f[1] == '-' && f[2] == 'N')
314 if (!copy)
315 copy = xstrdup (format);
316 copy[f + 1 - format] = '0' + res_width (gettime_res ());
317 f += 2;
320 return copy;
323 /* Parse each line in INPUT_FILENAME as with --date and display each
324 resulting time and date. If the file cannot be opened, tell why
325 then exit. Issue a diagnostic for any lines that cannot be parsed.
326 Return true if successful. */
328 static bool
329 batch_convert (char const *input_filename, char const *format,
330 timezone_t tz, char const *tzstring)
332 bool ok;
333 FILE *in_stream;
334 char *line;
335 size_t buflen;
336 struct timespec when;
338 if (STREQ (input_filename, "-"))
340 input_filename = _("standard input");
341 in_stream = stdin;
343 else
345 in_stream = fopen (input_filename, "r");
346 if (in_stream == NULL)
348 die (EXIT_FAILURE, errno, "%s", quotef (input_filename));
352 line = NULL;
353 buflen = 0;
354 ok = true;
355 while (true)
357 ssize_t line_length = getline (&line, &buflen, in_stream);
358 if (line_length < 0)
360 /* FIXME: detect/handle error here. */
361 break;
364 if (! parse_datetime2 (&when, line, NULL,
365 parse_datetime_flags, tz, tzstring))
367 if (line[line_length - 1] == '\n')
368 line[line_length - 1] = '\0';
369 error (0, 0, _("invalid date %s"), quote (line));
370 ok = false;
372 else
374 ok &= show_date (format, when, tz);
378 if (fclose (in_stream) == EOF)
379 die (EXIT_FAILURE, errno, "%s", quotef (input_filename));
381 free (line);
383 return ok;
387 main (int argc, char **argv)
389 int optc;
390 char const *datestr = NULL;
391 char const *set_datestr = NULL;
392 struct timespec when;
393 bool set_date = false;
394 char const *format = NULL;
395 bool get_resolution = false;
396 char *batch_file = NULL;
397 char *reference = NULL;
398 struct stat refstats;
399 bool ok;
401 initialize_main (&argc, &argv);
402 set_program_name (argv[0]);
403 setlocale (LC_ALL, "");
404 bindtextdomain (PACKAGE, LOCALEDIR);
405 textdomain (PACKAGE);
407 atexit (close_stdout);
409 while ((optc = getopt_long (argc, argv, short_options, long_options, NULL))
410 != -1)
412 char const *new_format = NULL;
414 switch (optc)
416 case 'd':
417 datestr = optarg;
418 break;
419 case DEBUG_DATE_PARSING_OPTION:
420 parse_datetime_flags |= PARSE_DATETIME_DEBUG;
421 break;
422 case 'f':
423 batch_file = optarg;
424 break;
425 case RESOLUTION_OPTION:
426 get_resolution = true;
427 break;
428 case RFC_3339_OPTION:
430 static char const rfc_3339_format[][32] =
432 "%Y-%m-%d",
433 "%Y-%m-%d %H:%M:%S%:z",
434 "%Y-%m-%d %H:%M:%S.%N%:z"
436 enum Time_spec i =
437 XARGMATCH ("--rfc-3339", optarg,
438 time_spec_string + 2, time_spec + 2);
439 new_format = rfc_3339_format[i];
440 break;
442 case 'I':
444 static char const iso_8601_format[][32] =
446 "%Y-%m-%d",
447 "%Y-%m-%dT%H:%M:%S%:z",
448 "%Y-%m-%dT%H:%M:%S,%N%:z",
449 "%Y-%m-%dT%H%:z",
450 "%Y-%m-%dT%H:%M%:z"
452 enum Time_spec i =
453 (optarg
454 ? XARGMATCH ("--iso-8601", optarg, time_spec_string, time_spec)
455 : TIME_SPEC_DATE);
456 new_format = iso_8601_format[i];
457 break;
459 case 'r':
460 reference = optarg;
461 break;
462 case 'R':
463 new_format = rfc_email_format;
464 break;
465 case 's':
466 set_datestr = optarg;
467 set_date = true;
468 break;
469 case 'u':
470 /* POSIX says that 'date -u' is equivalent to setting the TZ
471 environment variable, so this option should do nothing other
472 than setting TZ. */
473 if (putenv (bad_cast ("TZ=UTC0")) != 0)
474 xalloc_die ();
475 TZSET;
476 break;
477 case_GETOPT_HELP_CHAR;
478 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
479 default:
480 usage (EXIT_FAILURE);
483 if (new_format)
485 if (format)
486 die (EXIT_FAILURE, 0, _("multiple output formats specified"));
487 format = new_format;
491 int option_specified_date = (!!datestr + !!batch_file + !!reference
492 + get_resolution);
494 if (option_specified_date > 1)
496 error (0, 0,
497 _("the options to specify dates for printing are mutually exclusive"));
498 usage (EXIT_FAILURE);
501 if (set_date && option_specified_date)
503 error (0, 0,
504 _("the options to print and set the time may not be used together"));
505 usage (EXIT_FAILURE);
508 if (optind < argc)
510 if (optind + 1 < argc)
512 error (0, 0, _("extra operand %s"), quote (argv[optind + 1]));
513 usage (EXIT_FAILURE);
516 if (argv[optind][0] == '+')
518 if (format)
519 die (EXIT_FAILURE, 0, _("multiple output formats specified"));
520 format = argv[optind++] + 1;
522 else if (set_date || option_specified_date)
524 error (0, 0,
525 _("the argument %s lacks a leading '+';\n"
526 "when using an option to specify date(s), any non-option\n"
527 "argument must be a format string beginning with '+'"),
528 quote (argv[optind]));
529 usage (EXIT_FAILURE);
533 if (!format)
535 if (get_resolution)
536 format = "%s.%N";
537 else
539 format = DATE_FMT_LANGINFO ();
541 /* Do not wrap the following literal format string with _(...).
542 For example, suppose LC_ALL is unset, LC_TIME=POSIX,
543 and LANG="ko_KR". In that case, POSIX says that LC_TIME
544 determines the format and contents of date and time strings
545 written by date, which means "date" must generate output
546 using the POSIX locale; but adding _() would cause "date"
547 to use a Korean translation of the format. */
548 if (! *format)
549 format = "%a %b %e %H:%M:%S %Z %Y";
553 char *format_copy = adjust_resolution (format);
554 char const *format_res = format_copy ? format_copy : format;
555 char const *tzstring = getenv ("TZ");
556 timezone_t tz = tzalloc (tzstring);
558 if (batch_file != NULL)
559 ok = batch_convert (batch_file, format_res, tz, tzstring);
560 else
562 bool valid_date = true;
563 ok = true;
565 if (!option_specified_date && !set_date)
567 if (optind < argc)
569 /* Prepare to set system clock to the specified date/time
570 given in the POSIX-format. */
571 set_date = true;
572 datestr = argv[optind];
573 valid_date = posixtime (&when.tv_sec,
574 datestr,
575 (PDS_TRAILING_YEAR
576 | PDS_CENTURY | PDS_SECONDS));
577 when.tv_nsec = 0; /* FIXME: posixtime should set this. */
579 else
581 /* Prepare to print the current date/time. */
582 gettime (&when);
585 else
587 /* (option_specified_date || set_date) */
588 if (reference != NULL)
590 if (stat (reference, &refstats) != 0)
591 die (EXIT_FAILURE, errno, "%s", quotef (reference));
592 when = get_stat_mtime (&refstats);
594 else if (get_resolution)
596 long int res = gettime_res ();
597 when.tv_sec = res / TIMESPEC_HZ;
598 when.tv_nsec = res % TIMESPEC_HZ;
600 else
602 if (set_datestr)
603 datestr = set_datestr;
604 valid_date = parse_datetime2 (&when, datestr, NULL,
605 parse_datetime_flags,
606 tz, tzstring);
610 if (! valid_date)
611 die (EXIT_FAILURE, 0, _("invalid date %s"), quote (datestr));
613 if (set_date)
615 /* Set the system clock to the specified date, then regardless of
616 the success of that operation, format and print that date. */
617 if (settime (&when) != 0)
619 error (0, errno, _("cannot set date"));
620 ok = false;
624 ok &= show_date (format_res, when, tz);
627 IF_LINT (tzfree (tz));
628 IF_LINT (free (format_copy));
630 return ok ? EXIT_SUCCESS : EXIT_FAILURE;
633 /* Display the date and/or time in WHEN according to the format specified
634 in FORMAT, followed by a newline. Return true if successful. */
636 static bool
637 show_date (char const *format, struct timespec when, timezone_t tz)
639 struct tm tm;
641 if (parse_datetime_flags & PARSE_DATETIME_DEBUG)
642 error (0, 0, _("output format: %s"), quote (format));
644 if (localtime_rz (tz, &when.tv_sec, &tm))
646 if (format == rfc_email_format)
647 setlocale (LC_TIME, "C");
648 fprintftime (stdout, format, &tm, tz, when.tv_nsec);
649 if (format == rfc_email_format)
650 setlocale (LC_TIME, "");
651 fputc ('\n', stdout);
652 return true;
654 else
656 char buf[INT_BUFSIZE_BOUND (intmax_t)];
657 error (0, 0, _("time %s is out of range"),
658 quote (timetostr (when.tv_sec, buf)));
659 return false;