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