1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
|
/* date - print or set the system date and time
Copyright (C) 89, 90, 91, 92, 93, 1994 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
David MacKenzie <djm@gnu.ai.mit.edu> */
#include <config.h>
#include <stdio.h>
#include <getopt.h>
#include <sys/types.h>
#include "version.h"
#include "system.h"
#include "getline.h"
#ifdef TM_IN_SYS_TIME
#include <sys/time.h>
#else
#include <time.h>
#endif
#ifndef STDC_HEADERS
size_t strftime ();
time_t time ();
#endif
int stime ();
char *xrealloc ();
time_t get_date ();
time_t posixtime ();
void error ();
static void show_date ();
static void usage ();
/* The name this program was run with, for error messages. */
char *program_name;
/* If non-zero, display usage information and exit. */
static int show_help;
/* If non-zero, print the version on standard output and exit. */
static int show_version;
/* If non-zero, print or set Coordinated Universal Time. */
static int universal_time = 0;
static struct option const long_options[] =
{
{"date", required_argument, NULL, 'd'},
{"file", required_argument, NULL, 'f'},
{"help", no_argument, &show_help, 1},
{"set", required_argument, NULL, 's'},
{"uct", no_argument, NULL, 'u'},
{"utc", no_argument, NULL, 'u'},
{"universal", no_argument, NULL, 'u'},
{"version", no_argument, &show_version, 1},
{NULL, 0, NULL, 0}
};
/* Parse each line in INPUT_FILENAME as with --date and display the
each resulting time and date. If the file cannot be opened, tell why
then exit. Issue a diagnostic for any lines that cannot be parsed.
If any line cannot be parsed, return non-zero; otherwise return zero.
*/
static int
batch_convert (const char *input_filename, const char *format)
{
int have_read_stdin;
int status;
FILE *in_stream;
char *line;
int line_length;
int buflen;
time_t when;
if (strcmp (input_filename, "-") == 0)
{
input_filename = "standard input";
in_stream = stdin;
have_read_stdin = 1;
}
else
{
in_stream = fopen (input_filename, "r");
if (in_stream == NULL)
{
error (0, errno, "%s", input_filename);
}
have_read_stdin = 0;
}
line = NULL;
buflen = 0;
status = 0;
while (1)
{
line_length = getline (&line, &buflen, in_stream);
if (line_length < 0)
{
/* FIXME: detect/handle error here. */
break;
}
when = get_date (line, NULL);
if (when == -1)
{
error (0, 0, "invalid date `%s'", line);
status = 1;
}
else
{
show_date (format, when);
}
}
if (have_read_stdin && fclose (stdin) == EOF)
error (2, errno, "standard input");
if (line != NULL)
free (line);
return status;
}
void
main (argc, argv)
int argc;
char **argv;
{
int optc;
const char *datestr = NULL;
time_t when;
int set_date = 0;
int print_date = 0;
char *format;
char *batch_file = NULL;
int n_args;
int status;
program_name = argv[0];
while ((optc = getopt_long (argc, argv, "d:f:s:u", long_options, (int *) 0))
!= EOF)
switch (optc)
{
case 0:
break;
case 'd':
datestr = optarg;
print_date = 1;
break;
case 'f':
batch_file = optarg;
break;
case 's':
datestr = optarg;
set_date = 1;
break;
case 'u':
universal_time = 1;
break;
default:
usage (1);
}
if (show_version)
{
printf ("date - %s\n", version_string);
exit (0);
}
if (show_help)
usage (0);
n_args = argc - optind;
if (set_date && print_date)
{
error (0, 0,
"the options to print and set the time may not be used together");
usage (1);
}
if (n_args > 1)
{
error (0, 0, "too many non-option arguments");
usage (1);
}
if ((set_date || print_date || batch_file != NULL)
&& n_args == 1 && argv[optind][0] != '+')
{
error (0, 0, "\
when using the print, set time, or batch options, any\n\
non-option argument must be a format string beginning with `+'");
usage (1);
}
if (batch_file != NULL)
{
if (set_date || print_date)
{
error (0, 0, "\
the print and set options may not be used when reading dates from a file");
usage (1);
}
status = batch_convert (batch_file,
(n_args == 1 ? argv[optind] : NULL));
}
else
{
status = 0;
if (!print_date && !set_date)
{
if (n_args == 1 && argv[optind][0] != '+')
{
/* Prepare to set system clock to the specified date/time
given in the POSIX-format. */
set_date = 1;
datestr = argv[optind];
when = posixtime (datestr);
format = NULL;
}
else
{
/* Prepare to print the current date/time. */
print_date = 1;
datestr = "undefined";
time (&when);
format = (n_args == 1 ? argv[optind] + 1 : NULL);
}
}
else
{
/* (print_date || set_date) */
when = get_date (datestr, NULL);
format = (n_args == 1 ? argv[optind] + 1 : NULL);
}
if (when == -1)
error (1, 0, "invalid date `%s'", datestr);
if (set_date)
{
/* Set the system clock to the specified date, then regardless of
the success of that operation, format and print that date. */
if (stime (&when) == -1)
error (0, errno, "cannot set date");
}
show_date (format, when);
}
if (fclose (stdout) == EOF)
error (2, errno, "write error");
exit (status);
}
/* Display the date and/or time in WHEN according to the format specified
in FORMAT, followed by a newline. If FORMAT is NULL, use the
standard output format (ctime style but with a timezone inserted). */
static void
show_date (format, when)
const char *format;
time_t when;
{
struct tm *tm;
char *out = NULL;
size_t out_length = 0;
tm = (universal_time ? gmtime : localtime) (&when);
if (format == NULL)
{
/* Print the date in the default format. Vanilla ANSI C strftime
doesn't support %e, but POSIX requires it. If you don't use
a GNU strftime, make sure yours supports %e. */
format = (universal_time
? "%a %b %e %H:%M:%S UTC %Y"
: "%a %b %e %H:%M:%S %Z %Y");
}
else if (*format == '\0')
{
printf ("\n");
return;
}
do
{
out_length += 200;
out = (char *) xrealloc (out, out_length);
}
while (strftime (out, out_length, format, tm) == 0);
printf ("%s\n", out);
free (out);
}
static void
usage (status)
int status;
{
if (status != 0)
fprintf (stderr, "Try `%s --help' for more information.\n",
program_name);
else
{
printf ("\
Usage: %s [OPTION]... [+FORMAT]\n\
or: %s [OPTION] [MMDDhhmm[[CC]YY][.ss]]\n\
",
program_name, program_name);
printf ("\
\n\
-d, --date=STRING display time described by STRING, not `now'\n\
-f, --file=DATEFILE like --date once for each line of DATEFILE\n\
-s, --set=STRING set time described by STRING\n\
-u, --utc, --universal print or set Coordinated Universal Time\n\
--help display this help and exit\n\
--version output version information and exit\n\
");
printf ("\
\n\
FORMAT controls the output. The only valid option for the second form\n\
specifies Coordinated Universal Time. Interpreted sequences are:\n\
\n\
%%%% a literal %%\n\
%%a locale's abbreviated weekday name (Sun..Sat)\n\
%%A locale's full weekday name, variable length (Sunday..Saturday)\n\
%%b locale's abbreviated month name (Jan..Dec)\n\
%%B locale's full month name, variable length (January..December)\n\
%%c locale's date and time (Sat Nov 04 12:02:33 EST 1989)\n\
%%d day of month (01..31)\n\
%%D date (mm/dd/yy)\n\
%%h same as %%b\n\
%%H hour (00..23)\n\
%%I hour (01..12)\n\
%%j day of year (001..366)\n\
%%k hour ( 0..23)\n\
%%l hour ( 1..12)\n\
%%m month (01..12)\n\
%%M minute (00..59)\n\
%%n a newline\n\
%%p locale's AM or PM\n\
%%r time, 12-hour (hh:mm:ss [AP]M)\n\
%%s seconds since 00:00:00, Jan 1, 1970 (a GNU extension)\n\
%%S second (00..61)\n\
%%t a horizontal tab\n\
%%T time, 24-hour (hh:mm:ss)\n\
%%U week number of year with Sunday as first day of week (00..53)\n\
%%w day of week (0..6); 0 represents Sunday\n\
%%W week number of year with Monday as first day of week (00..53)\n\
%%x locale's date representation (mm/dd/yy)\n\
%%X locale's time representation (%%H:%%M:%%S)\n\
%%y last two digits of year (00..99)\n\
%%Y year (1970...)\n\
%%Z time zone (e.g., EDT), or nothing if no time zone is determinable\n\
\n\
By default, `date' pads numeric fields with zeroes. GNU `date'\n\
recognizes the following nonstandard modifiers between `%%' and a\n\
numeric directive.\n\
\n\
`-' (hyphen) do not pad the field\n\
`_' (underscore) pad the field with spaces\n\
");
}
exit (status);
}
|