doc: clarify the operation of wc -L
[coreutils.git] / src / date.c
blobeaee8b27384ad26c7215dccb5d7ca9092518c356
1 /* date - print or set the system date and time
2 Copyright (C) 1989-2015 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 <http://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 "error.h"
30 #include "parse-datetime.h"
31 #include "posixtm.h"
32 #include "quote.h"
33 #include "stat-time.h"
34 #include "fprintftime.h"
36 /* The official name of this program (e.g., no 'g' prefix). */
37 #define PROGRAM_NAME "date"
39 #define AUTHORS proper_name ("David MacKenzie")
41 static bool show_date (const char *format, struct timespec when);
43 enum Time_spec
45 /* Display only the date. */
46 TIME_SPEC_DATE,
47 /* Display date, hours, minutes, and seconds. */
48 TIME_SPEC_SECONDS,
49 /* Similar, but display nanoseconds. */
50 TIME_SPEC_NS,
52 /* Put these last, since they aren't valid for --rfc-3339. */
54 /* Display date and hour. */
55 TIME_SPEC_HOURS,
56 /* Display date, hours, and minutes. */
57 TIME_SPEC_MINUTES
60 static char const *const time_spec_string[] =
62 /* Put "hours" and "minutes" first, since they aren't valid for
63 --rfc-3339. */
64 "hours", "minutes",
65 "date", "seconds", "ns", NULL
67 static enum Time_spec const time_spec[] =
69 TIME_SPEC_HOURS, TIME_SPEC_MINUTES,
70 TIME_SPEC_DATE, TIME_SPEC_SECONDS, TIME_SPEC_NS
72 ARGMATCH_VERIFY (time_spec_string, time_spec);
74 /* A format suitable for Internet RFC 2822. */
75 static char const rfc_2822_format[] = "%a, %d %b %Y %H:%M:%S %z";
77 /* For long options that have no equivalent short option, use a
78 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
79 enum
81 RFC_3339_OPTION = CHAR_MAX + 1
84 static char const short_options[] = "d:f:I::r:Rs:u";
86 static struct option const long_options[] =
88 {"date", required_argument, NULL, 'd'},
89 {"file", required_argument, NULL, 'f'},
90 {"iso-8601", optional_argument, NULL, 'I'},
91 {"reference", required_argument, NULL, 'r'},
92 {"rfc-822", no_argument, NULL, 'R'},
93 {"rfc-2822", no_argument, NULL, 'R'},
94 {"rfc-3339", required_argument, NULL, RFC_3339_OPTION},
95 {"set", required_argument, NULL, 's'},
96 {"uct", no_argument, NULL, 'u'},
97 {"utc", no_argument, NULL, 'u'},
98 {"universal", no_argument, NULL, 'u'},
99 {GETOPT_HELP_OPTION_DECL},
100 {GETOPT_VERSION_OPTION_DECL},
101 {NULL, 0, NULL, 0}
104 #if LOCALTIME_CACHE
105 # define TZSET tzset ()
106 #else
107 # define TZSET /* empty */
108 #endif
110 #ifdef _DATE_FMT
111 # define DATE_FMT_LANGINFO() nl_langinfo (_DATE_FMT)
112 #else
113 # define DATE_FMT_LANGINFO() ""
114 #endif
116 void
117 usage (int status)
119 if (status != EXIT_SUCCESS)
120 emit_try_help ();
121 else
123 printf (_("\
124 Usage: %s [OPTION]... [+FORMAT]\n\
125 or: %s [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]\n\
127 program_name, program_name);
128 fputs (_("\
129 Display the current time in the given FORMAT, or set the system date.\n\
130 "), stdout);
132 emit_mandatory_arg_note ();
134 fputs (_("\
135 -d, --date=STRING display time described by STRING, not 'now'\n\
136 -f, --file=DATEFILE like --date; once for each line of DATEFILE\n\
137 "), stdout);
138 fputs (_("\
139 -I[FMT], --iso-8601[=FMT] output date/time in ISO 8601 format.\n\
140 FMT='date' for date only (the default),\n\
141 'hours', 'minutes', 'seconds', or 'ns'\n\
142 for date and time to the indicated precision.\n\
143 Example: 2006-08-14T02:34:56-0600\n\
144 "), stdout);
145 fputs (_("\
146 -R, --rfc-2822 output date and time in RFC 2822 format.\n\
147 Example: Mon, 14 Aug 2006 02:34:56 -0600\n\
148 "), stdout);
149 fputs (_("\
150 --rfc-3339=FMT output date/time in RFC 3339 format.\n\
151 FMT='date', 'seconds', or 'ns'\n\
152 for date and time to the indicated precision.\n\
153 Example: 2006-08-14 02:34:56-06:00\n\
154 "), stdout);
155 fputs (_("\
156 -r, --reference=FILE display the last modification time of FILE\n\
157 "), stdout);
158 fputs (_("\
159 -s, --set=STRING set time described by STRING\n\
160 -u, --utc, --universal print or set Coordinated Universal Time (UTC)\n\
161 "), stdout);
162 fputs (HELP_OPTION_DESCRIPTION, stdout);
163 fputs (VERSION_OPTION_DESCRIPTION, stdout);
164 fputs (_("\
166 FORMAT controls the output. Interpreted sequences are:\n\
168 %% a literal %\n\
169 %a locale's abbreviated weekday name (e.g., Sun)\n\
170 "), stdout);
171 fputs (_("\
172 %A locale's full weekday name (e.g., Sunday)\n\
173 %b locale's abbreviated month name (e.g., Jan)\n\
174 %B locale's full month name (e.g., January)\n\
175 %c locale's date and time (e.g., Thu Mar 3 23:05:25 2005)\n\
176 "), stdout);
177 fputs (_("\
178 %C century; like %Y, except omit last two digits (e.g., 20)\n\
179 %d day of month (e.g., 01)\n\
180 %D date; same as %m/%d/%y\n\
181 %e day of month, space padded; same as %_d\n\
182 "), stdout);
183 fputs (_("\
184 %F full date; same as %Y-%m-%d\n\
185 %g last two digits of year of ISO week number (see %G)\n\
186 %G year of ISO week number (see %V); normally useful only with %V\n\
187 "), stdout);
188 fputs (_("\
189 %h same as %b\n\
190 %H hour (00..23)\n\
191 %I hour (01..12)\n\
192 %j day of year (001..366)\n\
193 "), stdout);
194 fputs (_("\
195 %k hour, space padded ( 0..23); same as %_H\n\
196 %l hour, space padded ( 1..12); same as %_I\n\
197 %m month (01..12)\n\
198 %M minute (00..59)\n\
199 "), stdout);
200 fputs (_("\
201 %n a newline\n\
202 %N nanoseconds (000000000..999999999)\n\
203 %p locale's equivalent of either AM or PM; blank if not known\n\
204 %P like %p, but lower case\n\
205 %r locale's 12-hour clock time (e.g., 11:11:04 PM)\n\
206 %R 24-hour hour and minute; same as %H:%M\n\
207 %s seconds since 1970-01-01 00:00:00 UTC\n\
208 "), stdout);
209 fputs (_("\
210 %S second (00..60)\n\
211 %t a tab\n\
212 %T time; same as %H:%M:%S\n\
213 %u day of week (1..7); 1 is Monday\n\
214 "), stdout);
215 fputs (_("\
216 %U week number of year, with Sunday as first day of week (00..53)\n\
217 %V ISO week number, with Monday as first day of week (01..53)\n\
218 %w day of week (0..6); 0 is Sunday\n\
219 %W week number of year, with Monday as first day of week (00..53)\n\
220 "), stdout);
221 fputs (_("\
222 %x locale's date representation (e.g., 12/31/99)\n\
223 %X locale's time representation (e.g., 23:13:48)\n\
224 %y last two digits of year (00..99)\n\
225 %Y year\n\
226 "), stdout);
227 fputs (_("\
228 %z +hhmm numeric time zone (e.g., -0400)\n\
229 %:z +hh:mm numeric time zone (e.g., -04:00)\n\
230 %::z +hh:mm:ss numeric time zone (e.g., -04:00:00)\n\
231 %:::z numeric time zone with : to necessary precision (e.g., -04, +05:30)\n\
232 %Z alphabetic time zone abbreviation (e.g., EDT)\n\
234 By default, date pads numeric fields with zeroes.\n\
235 "), stdout);
236 fputs (_("\
237 The following optional flags may follow '%':\n\
239 - (hyphen) do not pad the field\n\
240 _ (underscore) pad with spaces\n\
241 0 (zero) pad with zeros\n\
242 ^ use upper case if possible\n\
243 # use opposite case if possible\n\
244 "), stdout);
245 fputs (_("\
247 After any flags comes an optional field width, as a decimal number;\n\
248 then an optional modifier, which is either\n\
249 E to use the locale's alternate representations if available, or\n\
250 O to use the locale's alternate numeric symbols if available.\n\
251 "), stdout);
252 fputs (_("\
254 Examples:\n\
255 Convert seconds since the epoch (1970-01-01 UTC) to a date\n\
256 $ date --date='@2147483647'\n\
258 Show the time on the west coast of the US (use tzselect(1) to find TZ)\n\
259 $ TZ='America/Los_Angeles' date\n\
261 Show the local time for 9AM next Friday on the west coast of the US\n\
262 $ date --date='TZ=\"America/Los_Angeles\" 09:00 next Fri'\n\
263 "), stdout);
264 emit_ancillary_info (PROGRAM_NAME);
266 exit (status);
269 /* Parse each line in INPUT_FILENAME as with --date and display each
270 resulting time and date. If the file cannot be opened, tell why
271 then exit. Issue a diagnostic for any lines that cannot be parsed.
272 Return true if successful. */
274 static bool
275 batch_convert (const char *input_filename, const char *format)
277 bool ok;
278 FILE *in_stream;
279 char *line;
280 size_t buflen;
281 struct timespec when;
283 if (STREQ (input_filename, "-"))
285 input_filename = _("standard input");
286 in_stream = stdin;
288 else
290 in_stream = fopen (input_filename, "r");
291 if (in_stream == NULL)
293 error (EXIT_FAILURE, errno, "%s", quote (input_filename));
297 line = NULL;
298 buflen = 0;
299 ok = true;
300 while (1)
302 ssize_t line_length = getline (&line, &buflen, in_stream);
303 if (line_length < 0)
305 /* FIXME: detect/handle error here. */
306 break;
309 if (! parse_datetime (&when, line, NULL))
311 if (line[line_length - 1] == '\n')
312 line[line_length - 1] = '\0';
313 error (0, 0, _("invalid date %s"), quote (line));
314 ok = false;
316 else
318 ok &= show_date (format, when);
322 if (fclose (in_stream) == EOF)
323 error (EXIT_FAILURE, errno, "%s", quote (input_filename));
325 free (line);
327 return ok;
331 main (int argc, char **argv)
333 int optc;
334 const char *datestr = NULL;
335 const char *set_datestr = NULL;
336 struct timespec when;
337 bool set_date = false;
338 char const *format = NULL;
339 char *batch_file = NULL;
340 char *reference = NULL;
341 struct stat refstats;
342 bool ok;
343 int option_specified_date;
345 initialize_main (&argc, &argv);
346 set_program_name (argv[0]);
347 setlocale (LC_ALL, "");
348 bindtextdomain (PACKAGE, LOCALEDIR);
349 textdomain (PACKAGE);
351 atexit (close_stdout);
353 while ((optc = getopt_long (argc, argv, short_options, long_options, NULL))
354 != -1)
356 char const *new_format = NULL;
358 switch (optc)
360 case 'd':
361 datestr = optarg;
362 break;
363 case 'f':
364 batch_file = optarg;
365 break;
366 case RFC_3339_OPTION:
368 static char const rfc_3339_format[][32] =
370 "%Y-%m-%d",
371 "%Y-%m-%d %H:%M:%S%:z",
372 "%Y-%m-%d %H:%M:%S.%N%:z"
374 enum Time_spec i =
375 XARGMATCH ("--rfc-3339", optarg,
376 time_spec_string + 2, time_spec + 2);
377 new_format = rfc_3339_format[i];
378 break;
380 case 'I':
382 static char const iso_8601_format[][32] =
384 "%Y-%m-%d",
385 "%Y-%m-%dT%H:%M:%S%z",
386 "%Y-%m-%dT%H:%M:%S,%N%z",
387 "%Y-%m-%dT%H%z",
388 "%Y-%m-%dT%H:%M%z"
390 enum Time_spec i =
391 (optarg
392 ? XARGMATCH ("--iso-8601", optarg, time_spec_string, time_spec)
393 : TIME_SPEC_DATE);
394 new_format = iso_8601_format[i];
395 break;
397 case 'r':
398 reference = optarg;
399 break;
400 case 'R':
401 new_format = rfc_2822_format;
402 break;
403 case 's':
404 set_datestr = optarg;
405 set_date = true;
406 break;
407 case 'u':
408 /* POSIX says that 'date -u' is equivalent to setting the TZ
409 environment variable, so this option should do nothing other
410 than setting TZ. */
411 if (putenv (bad_cast ("TZ=UTC0")) != 0)
412 xalloc_die ();
413 TZSET;
414 break;
415 case_GETOPT_HELP_CHAR;
416 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
417 default:
418 usage (EXIT_FAILURE);
421 if (new_format)
423 if (format)
424 error (EXIT_FAILURE, 0, _("multiple output formats specified"));
425 format = new_format;
429 option_specified_date = ((datestr ? 1 : 0)
430 + (batch_file ? 1 : 0)
431 + (reference ? 1 : 0));
433 if (option_specified_date > 1)
435 error (0, 0,
436 _("the options to specify dates for printing are mutually exclusive"));
437 usage (EXIT_FAILURE);
440 if (set_date && option_specified_date)
442 error (0, 0,
443 _("the options to print and set the time may not be used together"));
444 usage (EXIT_FAILURE);
447 if (optind < argc)
449 if (optind + 1 < argc)
451 error (0, 0, _("extra operand %s"), quote (argv[optind + 1]));
452 usage (EXIT_FAILURE);
455 if (argv[optind][0] == '+')
457 if (format)
458 error (EXIT_FAILURE, 0, _("multiple output formats specified"));
459 format = argv[optind++] + 1;
461 else if (set_date || option_specified_date)
463 error (0, 0,
464 _("the argument %s lacks a leading '+';\n"
465 "when using an option to specify date(s), any non-option\n"
466 "argument must be a format string beginning with '+'"),
467 quote (argv[optind]));
468 usage (EXIT_FAILURE);
472 if (!format)
474 format = DATE_FMT_LANGINFO ();
475 if (! *format)
477 /* Do not wrap the following literal format string with _(...).
478 For example, suppose LC_ALL is unset, LC_TIME=POSIX,
479 and LANG="ko_KR". In that case, POSIX says that LC_TIME
480 determines the format and contents of date and time strings
481 written by date, which means "date" must generate output
482 using the POSIX locale; but adding _() would cause "date"
483 to use a Korean translation of the format. */
484 format = "%a %b %e %H:%M:%S %Z %Y";
488 if (batch_file != NULL)
489 ok = batch_convert (batch_file, format);
490 else
492 bool valid_date = true;
493 ok = true;
495 if (!option_specified_date && !set_date)
497 if (optind < argc)
499 /* Prepare to set system clock to the specified date/time
500 given in the POSIX-format. */
501 set_date = true;
502 datestr = argv[optind];
503 valid_date = posixtime (&when.tv_sec,
504 datestr,
505 (PDS_TRAILING_YEAR
506 | PDS_CENTURY | PDS_SECONDS));
507 when.tv_nsec = 0; /* FIXME: posixtime should set this. */
509 else
511 /* Prepare to print the current date/time. */
512 gettime (&when);
515 else
517 /* (option_specified_date || set_date) */
518 if (reference != NULL)
520 if (stat (reference, &refstats) != 0)
521 error (EXIT_FAILURE, errno, "%s", reference);
522 when = get_stat_mtime (&refstats);
524 else
526 if (set_datestr)
527 datestr = set_datestr;
528 valid_date = parse_datetime (&when, datestr, NULL);
532 if (! valid_date)
533 error (EXIT_FAILURE, 0, _("invalid date %s"), quote (datestr));
535 if (set_date)
537 /* Set the system clock to the specified date, then regardless of
538 the success of that operation, format and print that date. */
539 if (settime (&when) != 0)
541 error (0, errno, _("cannot set date"));
542 ok = false;
546 ok &= show_date (format, when);
549 return ok ? EXIT_SUCCESS : EXIT_FAILURE;
552 /* Display the date and/or time in WHEN according to the format specified
553 in FORMAT, followed by a newline. Return true if successful. */
555 static bool
556 show_date (const char *format, struct timespec when)
558 struct tm *tm;
560 tm = localtime (&when.tv_sec);
561 if (! tm)
563 char buf[INT_BUFSIZE_BOUND (intmax_t)];
564 error (0, 0, _("time %s is out of range"), timetostr (when.tv_sec, buf));
565 return false;
568 if (format == rfc_2822_format)
569 setlocale (LC_TIME, "C");
570 fprintftime (stdout, format, tm, 0, when.tv_nsec);
571 fputc ('\n', stdout);
572 if (format == rfc_2822_format)
573 setlocale (LC_TIME, "");
575 return true;