TODO: add an item for a chmod optimization
[platform/upstream/coreutils.git] / src / date.c
1 /* date - print or set the system date and time
2    Copyright (C) 1989-2008 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>.
16
17    David MacKenzie <djm@gnu.ai.mit.edu> */
18
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
26
27 #include "system.h"
28 #include "argmatch.h"
29 #include "error.h"
30 #include "getdate.h"
31 #include "posixtm.h"
32 #include "quote.h"
33 #include "stat-time.h"
34 #include "fprintftime.h"
35
36 /* The official name of this program (e.g., no `g' prefix).  */
37 #define PROGRAM_NAME "date"
38
39 #define AUTHORS proper_name ("David MacKenzie")
40
41 int putenv ();
42
43 static bool show_date (const char *format, struct timespec when);
44
45 enum Time_spec
46 {
47   /* Display only the date.  */
48   TIME_SPEC_DATE,
49   /* Display date, hours, minutes, and seconds.  */
50   TIME_SPEC_SECONDS,
51   /* Similar, but display nanoseconds. */
52   TIME_SPEC_NS,
53
54   /* Put these last, since they aren't valid for --rfc-3339.  */
55
56   /* Display date and hour.  */
57   TIME_SPEC_HOURS,
58   /* Display date, hours, and minutes.  */
59   TIME_SPEC_MINUTES
60 };
61
62 static char const *const time_spec_string[] =
63 {
64   /* Put "hours" and "minutes" first, since they aren't valid for
65      --rfc-3339.  */
66   "hours", "minutes",
67   "date", "seconds", "ns", NULL
68 };
69 static enum Time_spec const time_spec[] =
70 {
71   TIME_SPEC_HOURS, TIME_SPEC_MINUTES,
72   TIME_SPEC_DATE, TIME_SPEC_SECONDS, TIME_SPEC_NS
73 };
74 ARGMATCH_VERIFY (time_spec_string, time_spec);
75
76 /* A format suitable for Internet RFC 2822.  */
77 static char const rfc_2822_format[] = "%a, %d %b %Y %H:%M:%S %z";
78
79 /* For long options that have no equivalent short option, use a
80    non-character as a pseudo short option, starting with CHAR_MAX + 1.  */
81 enum
82 {
83   RFC_3339_OPTION = CHAR_MAX + 1
84 };
85
86 static char const short_options[] = "d:f:I::r:Rs:u";
87
88 static struct option const long_options[] =
89 {
90   {"date", required_argument, NULL, 'd'},
91   {"file", required_argument, NULL, 'f'},
92   {"iso-8601", optional_argument, NULL, 'I'}, /* Deprecated.  */
93   {"reference", required_argument, NULL, 'r'},
94   {"rfc-822", no_argument, NULL, 'R'},
95   {"rfc-2822", no_argument, NULL, 'R'},
96   {"rfc-3339", required_argument, NULL, RFC_3339_OPTION},
97   {"set", required_argument, NULL, 's'},
98   {"uct", no_argument, NULL, 'u'},
99   {"utc", no_argument, NULL, 'u'},
100   {"universal", no_argument, NULL, 'u'},
101   {GETOPT_HELP_OPTION_DECL},
102   {GETOPT_VERSION_OPTION_DECL},
103   {NULL, 0, NULL, 0}
104 };
105
106 #if LOCALTIME_CACHE
107 # define TZSET tzset ()
108 #else
109 # define TZSET /* empty */
110 #endif
111
112 #ifdef _DATE_FMT
113 # define DATE_FMT_LANGINFO() nl_langinfo (_DATE_FMT)
114 #else
115 # define DATE_FMT_LANGINFO() ""
116 #endif
117
118 void
119 usage (int status)
120 {
121   if (status != EXIT_SUCCESS)
122     fprintf (stderr, _("Try `%s --help' for more information.\n"),
123              program_name);
124   else
125     {
126       printf (_("\
127 Usage: %s [OPTION]... [+FORMAT]\n\
128   or:  %s [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]\n\
129 "),
130               program_name, program_name);
131       fputs (_("\
132 Display the current time in the given FORMAT, or set the system date.\n\
133 \n\
134   -d, --date=STRING         display time described by STRING, not `now'\n\
135   -f, --file=DATEFILE       like --date once for each line of DATEFILE\n\
136 "), stdout);
137       fputs (_("\
138   -r, --reference=FILE      display the last modification time of FILE\n\
139   -R, --rfc-2822            output date and time in RFC 2822 format.\n\
140                             Example: Mon, 07 Aug 2006 12:34:56 -0600\n\
141 "), stdout);
142       fputs (_("\
143       --rfc-3339=TIMESPEC   output date and time in RFC 3339 format.\n\
144                             TIMESPEC=`date', `seconds', or `ns' for\n\
145                             date and time to the indicated precision.\n\
146                             Date and time components are separated by\n\
147                             a single space: 2006-08-07 12:34:56-06:00\n\
148   -s, --set=STRING          set time described by STRING\n\
149   -u, --utc, --universal    print or set Coordinated Universal Time\n\
150 "), stdout);
151       fputs (HELP_OPTION_DESCRIPTION, stdout);
152       fputs (VERSION_OPTION_DESCRIPTION, stdout);
153       fputs (_("\
154 \n\
155 FORMAT controls the output.  Interpreted sequences are:\n\
156 \n\
157   %%   a literal %\n\
158   %a   locale's abbreviated weekday name (e.g., Sun)\n\
159 "), stdout);
160       fputs (_("\
161   %A   locale's full weekday name (e.g., Sunday)\n\
162   %b   locale's abbreviated month name (e.g., Jan)\n\
163   %B   locale's full month name (e.g., January)\n\
164   %c   locale's date and time (e.g., Thu Mar  3 23:05:25 2005)\n\
165 "), stdout);
166       fputs (_("\
167   %C   century; like %Y, except omit last two digits (e.g., 20)\n\
168   %d   day of month (e.g, 01)\n\
169   %D   date; same as %m/%d/%y\n\
170   %e   day of month, space padded; same as %_d\n\
171 "), stdout);
172       fputs (_("\
173   %F   full date; same as %Y-%m-%d\n\
174   %g   last two digits of year of ISO week number (see %G)\n\
175   %G   year of ISO week number (see %V); normally useful only with %V\n\
176 "), stdout);
177       fputs (_("\
178   %h   same as %b\n\
179   %H   hour (00..23)\n\
180   %I   hour (01..12)\n\
181   %j   day of year (001..366)\n\
182 "), stdout);
183       fputs (_("\
184   %k   hour ( 0..23)\n\
185   %l   hour ( 1..12)\n\
186   %m   month (01..12)\n\
187   %M   minute (00..59)\n\
188 "), stdout);
189       fputs (_("\
190   %n   a newline\n\
191   %N   nanoseconds (000000000..999999999)\n\
192   %p   locale's equivalent of either AM or PM; blank if not known\n\
193   %P   like %p, but lower case\n\
194   %r   locale's 12-hour clock time (e.g., 11:11:04 PM)\n\
195   %R   24-hour hour and minute; same as %H:%M\n\
196   %s   seconds since 1970-01-01 00:00:00 UTC\n\
197 "), stdout);
198       fputs (_("\
199   %S   second (00..60)\n\
200   %t   a tab\n\
201   %T   time; same as %H:%M:%S\n\
202   %u   day of week (1..7); 1 is Monday\n\
203 "), stdout);
204       fputs (_("\
205   %U   week number of year, with Sunday as first day of week (00..53)\n\
206   %V   ISO week number, with Monday as first day of week (01..53)\n\
207   %w   day of week (0..6); 0 is Sunday\n\
208   %W   week number of year, with Monday as first day of week (00..53)\n\
209 "), stdout);
210       fputs (_("\
211   %x   locale's date representation (e.g., 12/31/99)\n\
212   %X   locale's time representation (e.g., 23:13:48)\n\
213   %y   last two digits of year (00..99)\n\
214   %Y   year\n\
215 "), stdout);
216       fputs (_("\
217   %z   +hhmm numeric timezone (e.g., -0400)\n\
218   %:z  +hh:mm numeric timezone (e.g., -04:00)\n\
219   %::z  +hh:mm:ss numeric time zone (e.g., -04:00:00)\n\
220   %:::z  numeric time zone with : to necessary precision (e.g., -04, +05:30)\n\
221   %Z   alphabetic time zone abbreviation (e.g., EDT)\n\
222 \n\
223 By default, date pads numeric fields with zeroes.\n\
224 "), stdout);
225       fputs (_("\
226 The following optional flags may follow `%':\n\
227 \n\
228   -  (hyphen) do not pad the field\n\
229   _  (underscore) pad with spaces\n\
230   0  (zero) pad with zeros\n\
231   ^  use upper case if possible\n\
232   #  use opposite case if possible\n\
233 "), stdout);
234       fputs (_("\
235 \n\
236 After any flags comes an optional field width, as a decimal number;\n\
237 then an optional modifier, which is either\n\
238 E to use the locale's alternate representations if available, or\n\
239 O to use the locale's alternate numeric symbols if available.\n\
240 "), stdout);
241       emit_bug_reporting_address ();
242     }
243   exit (status);
244 }
245
246 /* Parse each line in INPUT_FILENAME as with --date and display each
247    resulting time and date.  If the file cannot be opened, tell why
248    then exit.  Issue a diagnostic for any lines that cannot be parsed.
249    Return true if successful.  */
250
251 static bool
252 batch_convert (const char *input_filename, const char *format)
253 {
254   bool ok;
255   FILE *in_stream;
256   char *line;
257   size_t buflen;
258   struct timespec when;
259
260   if (STREQ (input_filename, "-"))
261     {
262       input_filename = _("standard input");
263       in_stream = stdin;
264     }
265   else
266     {
267       in_stream = fopen (input_filename, "r");
268       if (in_stream == NULL)
269         {
270           error (EXIT_FAILURE, errno, "%s", quote (input_filename));
271         }
272     }
273
274   line = NULL;
275   buflen = 0;
276   ok = true;
277   while (1)
278     {
279       ssize_t line_length = getline (&line, &buflen, in_stream);
280       if (line_length < 0)
281         {
282           /* FIXME: detect/handle error here.  */
283           break;
284         }
285
286       if (! get_date (&when, line, NULL))
287         {
288           if (line[line_length - 1] == '\n')
289             line[line_length - 1] = '\0';
290           error (0, 0, _("invalid date %s"), quote (line));
291           ok = false;
292         }
293       else
294         {
295           ok &= show_date (format, when);
296         }
297     }
298
299   if (fclose (in_stream) == EOF)
300     error (EXIT_FAILURE, errno, "%s", quote (input_filename));
301
302   free (line);
303
304   return ok;
305 }
306
307 int
308 main (int argc, char **argv)
309 {
310   int optc;
311   const char *datestr = NULL;
312   const char *set_datestr = NULL;
313   struct timespec when;
314   bool set_date = false;
315   char const *format = NULL;
316   char *batch_file = NULL;
317   char *reference = NULL;
318   struct stat refstats;
319   bool ok;
320   int option_specified_date;
321
322   initialize_main (&argc, &argv);
323   set_program_name (argv[0]);
324   setlocale (LC_ALL, "");
325   bindtextdomain (PACKAGE, LOCALEDIR);
326   textdomain (PACKAGE);
327
328   atexit (close_stdout);
329
330   while ((optc = getopt_long (argc, argv, short_options, long_options, NULL))
331          != -1)
332     {
333       char const *new_format = NULL;
334
335       switch (optc)
336         {
337         case 'd':
338           datestr = optarg;
339           break;
340         case 'f':
341           batch_file = optarg;
342           break;
343         case RFC_3339_OPTION:
344           {
345             static char const rfc_3339_format[][32] =
346               {
347                 "%Y-%m-%d",
348                 "%Y-%m-%d %H:%M:%S%:z",
349                 "%Y-%m-%d %H:%M:%S.%N%:z"
350               };
351             enum Time_spec i =
352               XARGMATCH ("--rfc-3339", optarg,
353                          time_spec_string + 2, time_spec + 2);
354             new_format = rfc_3339_format[i];
355             break;
356           }
357         case 'I':
358           {
359             static char const iso_8601_format[][32] =
360               {
361                 "%Y-%m-%d",
362                 "%Y-%m-%dT%H:%M:%S%z",
363                 "%Y-%m-%dT%H:%M:%S,%N%z",
364                 "%Y-%m-%dT%H%z",
365                 "%Y-%m-%dT%H:%M%z"
366               };
367             enum Time_spec i =
368               (optarg
369                ? XARGMATCH ("--iso-8601", optarg, time_spec_string, time_spec)
370                : TIME_SPEC_DATE);
371             new_format = iso_8601_format[i];
372             break;
373           }
374         case 'r':
375           reference = optarg;
376           break;
377         case 'R':
378           new_format = rfc_2822_format;
379           break;
380         case 's':
381           set_datestr = optarg;
382           set_date = true;
383           break;
384         case 'u':
385           /* POSIX says that `date -u' is equivalent to setting the TZ
386              environment variable, so this option should do nothing other
387              than setting TZ.  */
388           if (putenv ("TZ=UTC0") != 0)
389             xalloc_die ();
390           TZSET;
391           break;
392         case_GETOPT_HELP_CHAR;
393         case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
394         default:
395           usage (EXIT_FAILURE);
396         }
397
398       if (new_format)
399         {
400           if (format)
401             error (EXIT_FAILURE, 0, _("multiple output formats specified"));
402           format = new_format;
403         }
404     }
405
406   option_specified_date = ((datestr ? 1 : 0)
407                            + (batch_file ? 1 : 0)
408                            + (reference ? 1 : 0));
409
410   if (option_specified_date > 1)
411     {
412       error (0, 0,
413         _("the options to specify dates for printing are mutually exclusive"));
414       usage (EXIT_FAILURE);
415     }
416
417   if (set_date && option_specified_date)
418     {
419       error (0, 0,
420           _("the options to print and set the time may not be used together"));
421       usage (EXIT_FAILURE);
422     }
423
424   if (optind < argc)
425     {
426       if (optind + 1 < argc)
427         {
428           error (0, 0, _("extra operand %s"), quote (argv[optind + 1]));
429           usage (EXIT_FAILURE);
430         }
431
432       if (argv[optind][0] == '+')
433         {
434           if (format)
435             error (EXIT_FAILURE, 0, _("multiple output formats specified"));
436           format = argv[optind++] + 1;
437         }
438       else if (set_date || option_specified_date)
439         {
440           error (0, 0,
441                  _("the argument %s lacks a leading `+';\n"
442                    "when using an option to specify date(s), any non-option\n"
443                    "argument must be a format string beginning with `+'"),
444                  quote (argv[optind]));
445           usage (EXIT_FAILURE);
446         }
447     }
448
449   if (!format)
450     {
451       format = DATE_FMT_LANGINFO ();
452       if (! *format)
453         {
454           /* Do not wrap the following literal format string with _(...).
455              For example, suppose LC_ALL is unset, LC_TIME="POSIX",
456              and LANG="ko_KR".  In that case, POSIX says that LC_TIME
457              determines the format and contents of date and time strings
458              written by date, which means "date" must generate output
459              using the POSIX locale; but adding _() would cause "date"
460              to use a Korean translation of the format.  */
461           format = "%a %b %e %H:%M:%S %Z %Y";
462         }
463     }
464
465   if (batch_file != NULL)
466     ok = batch_convert (batch_file, format);
467   else
468     {
469       bool valid_date = true;
470       ok = true;
471
472       if (!option_specified_date && !set_date)
473         {
474           if (optind < argc)
475             {
476               /* Prepare to set system clock to the specified date/time
477                  given in the POSIX-format.  */
478               set_date = true;
479               datestr = argv[optind];
480               valid_date = posixtime (&when.tv_sec,
481                                       datestr,
482                                       (PDS_TRAILING_YEAR
483                                        | PDS_CENTURY | PDS_SECONDS));
484               when.tv_nsec = 0; /* FIXME: posixtime should set this.  */
485             }
486           else
487             {
488               /* Prepare to print the current date/time.  */
489               gettime (&when);
490             }
491         }
492       else
493         {
494           /* (option_specified_date || set_date) */
495           if (reference != NULL)
496             {
497               if (stat (reference, &refstats) != 0)
498                 error (EXIT_FAILURE, errno, "%s", reference);
499               when = get_stat_mtime (&refstats);
500             }
501           else
502             {
503               if (set_datestr)
504                 datestr = set_datestr;
505               valid_date = get_date (&when, datestr, NULL);
506             }
507         }
508
509       if (! valid_date)
510         error (EXIT_FAILURE, 0, _("invalid date %s"), quote (datestr));
511
512       if (set_date)
513         {
514           /* Set the system clock to the specified date, then regardless of
515              the success of that operation, format and print that date.  */
516           if (settime (&when) != 0)
517             {
518               error (0, errno, _("cannot set date"));
519               ok = false;
520             }
521         }
522
523       ok &= show_date (format, when);
524     }
525
526   exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
527 }
528
529 /* Display the date and/or time in WHEN according to the format specified
530    in FORMAT, followed by a newline.  Return true if successful.  */
531
532 static bool
533 show_date (const char *format, struct timespec when)
534 {
535   struct tm *tm;
536
537   tm = localtime (&when.tv_sec);
538   if (! tm)
539     {
540       char buf[INT_BUFSIZE_BOUND (intmax_t)];
541       error (0, 0, _("time %s is out of range"), timetostr (when.tv_sec, buf));
542       return false;
543     }
544
545   if (format == rfc_2822_format)
546     setlocale (LC_TIME, "C");
547   fprintftime (stdout, format, tm, 0, when.tv_nsec);
548   fputc ('\n', stdout);
549   if (format == rfc_2822_format)
550     setlocale (LC_TIME, "");
551
552   return true;
553 }