1 /* Display or set the current time and date. */
3 /* Copyright 1985, 1987, 1988 The Regents of the University of California.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
9 1. Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 2. Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14 3. Neither the name of the University nor the names of its contributors
15 may be used to endorse or promote products derived from this software
16 without specific prior written permission.
18 THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
19 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 ** The two things date knows about time are. . .
38 #define TM_YEAR_BASE 1900
39 #endif /* !defined TM_YEAR_BASE */
43 #endif /* !defined SECSPERMIN */
45 extern char ** environ
;
50 extern char * tzname
[];
53 static int retval
= EXIT_SUCCESS
;
55 static void display(const char *, time_t);
56 static void dogmt(void);
57 static void errensure(void);
58 static void timeout(FILE *, const char *, const struct tm
*);
59 static void usage(void);
62 main(const int argc
, char *argv
[])
64 register const char * format
;
65 register const char * cp
;
67 register bool rflag
= false;
73 setlocale(LC_ALL
, "");
74 #endif /* defined(LC_ALL) */
77 bindtextdomain(TZ_DOMAIN
, TZ_DOMAINDIR
);
78 #endif /* defined(TEXTDOMAINDIR) */
79 textdomain(TZ_DOMAIN
);
80 #endif /* HAVE_GETTEXT */
83 while ((ch
= getopt(argc
, argv
, "ucr:")) != EOF
&& ch
!= -1) {
87 case 'u': /* do it in UT */
91 case 'r': /* seconds since 1970 */
94 _("date: error: multiple -r's used"));
99 secs
= strtoimax (optarg
, &endarg
, 0);
100 if (*endarg
|| optarg
== endarg
)
102 else if (! (time_t_min
<= secs
&& secs
<= time_t_max
))
113 while (optind
< argc
) {
120 _("date: error: multiple formats in command line\n"));
124 fprintf(stderr
, _("date: unknown operand: %s\n"), cp
);
136 static char ** fakeenv
;
138 if (fakeenv
== NULL
) {
142 static char tzegmt0
[] = "TZ=GMT0";
144 for (n
= 0; environ
[n
] != NULL
; ++n
)
146 fakeenv
= malloc((n
+ 2) * sizeof *fakeenv
);
147 if (fakeenv
== NULL
) {
148 perror(_("Memory exhausted"));
153 fakeenv
[to
++] = tzegmt0
;
154 for (from
= 1; environ
[from
] != NULL
; ++from
)
155 if (strncmp(environ
[from
], "TZ=", 3) != 0)
156 fakeenv
[to
++] = environ
[from
];
165 if (retval
== EXIT_SUCCESS
)
166 retval
= EXIT_FAILURE
;
173 _("date: usage: date [-u] [-c] [-r seconds]"
180 display(char const *format
, time_t now
)
184 tmp
= localtime(&now
);
187 _("date: error: time out of range\n"));
191 timeout(stdout
, format
? format
: "%+", tmp
);
195 if (ferror(stdout
) || ferror(stderr
)) {
197 _("date: error: couldn't write results\n"));
205 timeout(FILE *fp
, char const *format
, struct tm
const *tmp
)
215 fprintf(stderr
, _("date: error: time out of range\n"));
226 _("date: error: can't get memory\n"));
231 result
= strftime(cp
, size
, format
, tmp
);
232 if (result
!= 0 || cp
[0] == '\0')
235 cp
= realloc(cp
, size
);
237 fwrite(cp
, 1, result
, fp
);