1 /* date - print or set the system date and time
2 Copyright (C) 1989, 1991 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 2, or (at your option)
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, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19 -d DATESTR Display the date DATESTR.
20 -s DATESTR Set the date to DATESTR.
21 -u Display or set the date in universal instead of local time.
22 +FORMAT Specify custom date output format, described below.
23 MMDDhhmm[[CC]YY][.ss] Set the date in the format described below.
25 If one non-option argument is given, it is used as the date to which
26 to set the system clock, and must have the format:
28 DD day in month (01..31)
31 CC first 2 digits of year (optional, defaults to current) (00..99)
32 YY last 2 digits of year (optional, defaults to current) (00..99)
35 If a non-option argument that starts with a `+' is specified, it
36 is used to control the format in which the date is printed; it
37 can contain any of the `%' substitutions allowed by the strftime
38 function. A newline is always added at the end of the output.
40 David MacKenzie <djm@gnu.ai.mit.edu> */
43 #if defined (CONFIG_BROKETS)
44 /* We use <config.h> instead of "config.h" so that a compilation
45 using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
46 (which it would do because it found this file in $srcdir). */
55 #include <sys/types.h>
80 static void show_date ();
83 /* putenv string to use Universal Coordinated Time.
84 POSIX.2 says it should be "TZ=UCT0" or "TZ=GMT0". */
86 #if defined(hpux) || defined(__hpux__) || defined(ultrix) || defined(__ultrix__) || defined(USG)
87 #define TZ_UCT "TZ=GMT0"
93 /* The name this program was run with, for error messages. */
96 /* If non-zero, display usage information and exit. */
99 /* If non-zero, print the version on standard output and exit. */
100 static int show_version
;
102 static struct option
const long_options
[] =
104 {"date", required_argument
, NULL
, 'd'},
105 {"help", no_argument
, &show_help
, 1},
106 {"set", required_argument
, NULL
, 's'},
107 {"uct", no_argument
, NULL
, 'u'},
108 {"universal", no_argument
, NULL
, 'u'},
109 {"version", no_argument
, &show_version
, 1},
119 char *datestr
= NULL
;
122 int universal_time
= 0;
124 program_name
= argv
[0];
126 while ((optc
= getopt_long (argc
, argv
, "d:s:u", long_options
, (int *) 0))
148 printf ("%s\n", version_string
);
155 if (argc
- optind
> 1)
158 if (universal_time
&& putenv (TZ_UCT
) != 0)
159 error (1, 0, "virtual memory exhausted");
164 when
= get_date (datestr
, NULL
);
166 if (argc
- optind
== 1 && argv
[optind
][0] != '+')
168 when
= posixtime (argv
[optind
]);
173 error (1, 0, "invalid date");
175 if (set_date
&& stime (&when
) == -1)
176 error (0, errno
, "cannot set date");
178 if (argc
- optind
== 1 && argv
[optind
][0] == '+')
179 show_date (argv
[optind
] + 1, when
);
181 show_date ((char *) NULL
, when
);
186 /* Display the date and/or time in WHEN according to the format specified
187 in FORMAT, followed by a newline. If FORMAT is NULL, use the
188 standard output format (ctime style but with a timezone inserted). */
191 show_date (format
, when
)
197 size_t out_length
= 0;
199 tm
= localtime (&when
);
202 /* Print the date in the default format. Vanilla ANSI C strftime
203 doesn't support %e, but POSIX requires it. If you don't use
204 a GNU strftime, make sure yours supports %e. */
205 format
= "%a %b %e %H:%M:%S %Z %Y";
206 else if (*format
== '\0')
215 out
= (char *) xrealloc (out
, out_length
);
217 while (strftime (out
, out_length
, format
, tm
) == 0);
219 printf ("%s\n", out
);
227 Usage: %s [{--help,--version}] [-u] [-d datestr] [-s datestr]\n\
228 [--date datestr] [--set datestr] [--uct] [--universal]\n\
229 [+FORMAT] [MMDDhhmm[[CC]YY][.ss]]\n",