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 fprintf (status == 0 ? stdout : stderr, "\
228 Usage: %s [OPTION]... [+FORMAT] [MMDDhhmm[[CC]YY][.ss]]\n\
233 fprintf (stderr, "Try `%s --help' for more information.\n",
239 -d, --date STRING display time described by STRING, not `now'\n\
240 -s, --set STRING set time described by STRING\n\
241 -u, --uct, --universal print or set Universal Coordinated Time\n\
242 --help display this help and exit\n\
243 --version output version information and exit\n\
247 FORMAT controls the output. Interpreted sequences are:\n\
250 %%A locale's full weekday name, variable length (Sunday..Saturday)\n\
251 %%B locale's full month name, variable length (January..December)\n\
252 %%D date (mm/dd/yy)\n\
255 %%M minute (00..59)\n\
256 %%S second (00..61)\n\
257 %%T time, 24-hour (hh:mm:ss)\n\
258 %%U week number of year with Sunday as first day of week (00..53)\n\
259 %%W week number of year with Monday as first day of week (00..53)\n\
260 %%X locale's time representation (%%H:%%M:%%S)\n\
261 %%Y year (1970...)\n\
262 %%Z time zone (e.g., EDT), or nothing if no time zone is determinable\n\
263 %%a locale's abbreviated weekday name (Sun..Sat)\n\
264 %%b locale's abbreviated month name (Jan..Dec)\n\
265 %%c locale's date and time (Sat Nov 04 12:02:33 EST 1989)\n\
266 %%d day of month (01..31)\n\
268 %%j day of year (001..366)\n\
271 %%m month (01..12)\n\
273 %%p locale's AM or PM\n\
274 %%r time, 12-hour (hh:mm:ss [AP]M)\n\
275 %%t a horizontal tab\n\
276 %%w day of week (0..6)\n\
277 %%x locale's date representation (mm/dd/yy)\n\
278 %%y last two digits of year (00..99)\n\