OpenCoverage

date.c

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

Generated by Squish Coco 4.1.2