xref: /onnv-gate/usr/src/lib/libc/port/gen/ctime.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
30*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate /*
34*0Sstevel@tonic-gate  * This routine converts time as follows.
35*0Sstevel@tonic-gate  * The epoch is 0000 Jan 1 1970 GMT.
36*0Sstevel@tonic-gate  * The argument time is in seconds since then.
37*0Sstevel@tonic-gate  * The localtime(t) entry returns a pointer to an array
38*0Sstevel@tonic-gate  * containing
39*0Sstevel@tonic-gate  *  seconds (0-59)
40*0Sstevel@tonic-gate  *  minutes (0-59)
41*0Sstevel@tonic-gate  *  hours (0-23)
42*0Sstevel@tonic-gate  *  day of month (1-31)
43*0Sstevel@tonic-gate  *  month (0-11)
44*0Sstevel@tonic-gate  *  year-1970
45*0Sstevel@tonic-gate  *  weekday (0-6, Sun is 0)
46*0Sstevel@tonic-gate  *  day of the year
47*0Sstevel@tonic-gate  *  daylight savings flag
48*0Sstevel@tonic-gate  *
49*0Sstevel@tonic-gate  * The routine corrects for daylight saving
50*0Sstevel@tonic-gate  * time and will work in any time zone provided
51*0Sstevel@tonic-gate  * "timezone" is adjusted to the difference between
52*0Sstevel@tonic-gate  * Greenwich and local standard time (measured in seconds).
53*0Sstevel@tonic-gate  * In places like Michigan "daylight" must
54*0Sstevel@tonic-gate  * be initialized to 0 to prevent the conversion
55*0Sstevel@tonic-gate  * to daylight time.
56*0Sstevel@tonic-gate  * There is a table which accounts for the peculiarities
57*0Sstevel@tonic-gate  * undergone by daylight time in 1974-1975.
58*0Sstevel@tonic-gate  *
59*0Sstevel@tonic-gate  * The routine does not work
60*0Sstevel@tonic-gate  * in Saudi Arabia which runs on Solar time.
61*0Sstevel@tonic-gate  *
62*0Sstevel@tonic-gate  * asctime(tvec)
63*0Sstevel@tonic-gate  * where tvec is produced by localtime
64*0Sstevel@tonic-gate  * returns a ptr to a character string
65*0Sstevel@tonic-gate  * that has the ascii time in the form
66*0Sstevel@tonic-gate  *	Thu Jan 01 00:00:00 1970\n\0
67*0Sstevel@tonic-gate  *	01234567890123456789012345
68*0Sstevel@tonic-gate  *	0	  1	    2
69*0Sstevel@tonic-gate  *
70*0Sstevel@tonic-gate  * ctime(t) just calls localtime, then asctime.
71*0Sstevel@tonic-gate  *
72*0Sstevel@tonic-gate  * tzset() looks for an environment variable named
73*0Sstevel@tonic-gate  * TZ.
74*0Sstevel@tonic-gate  * If the variable is present, it will set the external
75*0Sstevel@tonic-gate  * variables "timezone", "altzone", "daylight", and "tzname"
76*0Sstevel@tonic-gate  * appropriately. It is called by localtime, and
77*0Sstevel@tonic-gate  * may also be called explicitly by the user.
78*0Sstevel@tonic-gate  */
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate #pragma weak asctime_r = _asctime_r
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate #include "synonyms.h"
83*0Sstevel@tonic-gate #include <mtlib.h>
84*0Sstevel@tonic-gate #include <sys/types.h>
85*0Sstevel@tonic-gate #include <time.h>
86*0Sstevel@tonic-gate #include <errno.h>
87*0Sstevel@tonic-gate #include <thread.h>
88*0Sstevel@tonic-gate #include <synch.h>
89*0Sstevel@tonic-gate #include "libc.h"
90*0Sstevel@tonic-gate #include "tsd.h"
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate #define	dysize(A) (((A)%4)? 365: 366)
93*0Sstevel@tonic-gate #define	CBUFSIZ 26
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate static char *
96*0Sstevel@tonic-gate ct_numb(char *cp, int n)
97*0Sstevel@tonic-gate {
98*0Sstevel@tonic-gate 	cp++;
99*0Sstevel@tonic-gate 	if (n >= 10)
100*0Sstevel@tonic-gate 		*cp++ = (n / 10) % 10 + '0';
101*0Sstevel@tonic-gate 	else
102*0Sstevel@tonic-gate 		*cp++ = ' ';		/* Pad with blanks */
103*0Sstevel@tonic-gate 	*cp++ = n % 10 + '0';
104*0Sstevel@tonic-gate 	return (cp);
105*0Sstevel@tonic-gate }
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate /*
108*0Sstevel@tonic-gate  * POSIX.1c standard version of the function asctime_r.
109*0Sstevel@tonic-gate  * User gets it via static asctime_r from the header file.
110*0Sstevel@tonic-gate  */
111*0Sstevel@tonic-gate char *
112*0Sstevel@tonic-gate __posix_asctime_r(const struct tm *t, char *cbuf)
113*0Sstevel@tonic-gate {
114*0Sstevel@tonic-gate 	char *cp;
115*0Sstevel@tonic-gate 	const char *ncp;
116*0Sstevel@tonic-gate 	const int *tp;
117*0Sstevel@tonic-gate 	const char *Date = "Day Mon 00 00:00:00 1900\n";
118*0Sstevel@tonic-gate 	const char *Day  = "SunMonTueWedThuFriSat";
119*0Sstevel@tonic-gate 	const char *Month = "JanFebMarAprMayJunJulAugSepOctNovDec";
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate 	cp = cbuf;
122*0Sstevel@tonic-gate 	for (ncp = Date; *cp++ = *ncp++; /* */);
123*0Sstevel@tonic-gate 	ncp = Day + (3 * t->tm_wday);
124*0Sstevel@tonic-gate 	cp = cbuf;
125*0Sstevel@tonic-gate 	*cp++ = *ncp++;
126*0Sstevel@tonic-gate 	*cp++ = *ncp++;
127*0Sstevel@tonic-gate 	*cp++ = *ncp++;
128*0Sstevel@tonic-gate 	cp++;
129*0Sstevel@tonic-gate 	tp = &t->tm_mon;
130*0Sstevel@tonic-gate 	ncp = Month + ((*tp) * 3);
131*0Sstevel@tonic-gate 	*cp++ = *ncp++;
132*0Sstevel@tonic-gate 	*cp++ = *ncp++;
133*0Sstevel@tonic-gate 	*cp++ = *ncp++;
134*0Sstevel@tonic-gate 	cp = ct_numb(cp, *--tp);
135*0Sstevel@tonic-gate 	cp = ct_numb(cp, *--tp + 100);
136*0Sstevel@tonic-gate 	cp = ct_numb(cp, *--tp + 100);
137*0Sstevel@tonic-gate 	--tp;
138*0Sstevel@tonic-gate 	cp = ct_numb(cp, *tp + 100);
139*0Sstevel@tonic-gate 	if (t->tm_year < 100) {
140*0Sstevel@tonic-gate 		/* Common case: "19" already in buffer */
141*0Sstevel@tonic-gate 		cp += 2;
142*0Sstevel@tonic-gate 	} else if (t->tm_year < 8100) {
143*0Sstevel@tonic-gate 		cp = ct_numb(cp, (1900 + t->tm_year) / 100);
144*0Sstevel@tonic-gate 		cp--;
145*0Sstevel@tonic-gate 	} else {
146*0Sstevel@tonic-gate 		/* Only 4-digit years are supported */
147*0Sstevel@tonic-gate 		errno = EOVERFLOW;
148*0Sstevel@tonic-gate 		return (NULL);
149*0Sstevel@tonic-gate 	}
150*0Sstevel@tonic-gate 	(void) ct_numb(cp, t->tm_year + 100);
151*0Sstevel@tonic-gate 	return (cbuf);
152*0Sstevel@tonic-gate }
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate /*
155*0Sstevel@tonic-gate  * POSIX.1c Draft-6 version of the function asctime_r.
156*0Sstevel@tonic-gate  * It was implemented by Solaris 2.3.
157*0Sstevel@tonic-gate  */
158*0Sstevel@tonic-gate char *
159*0Sstevel@tonic-gate asctime_r(const struct tm *t, char *cbuf, int buflen)
160*0Sstevel@tonic-gate {
161*0Sstevel@tonic-gate 	if (buflen < CBUFSIZ) {
162*0Sstevel@tonic-gate 		errno = ERANGE;
163*0Sstevel@tonic-gate 		return (NULL);
164*0Sstevel@tonic-gate 	}
165*0Sstevel@tonic-gate 	return (__posix_asctime_r(t, cbuf));
166*0Sstevel@tonic-gate }
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate char *
169*0Sstevel@tonic-gate ctime(const time_t *t)
170*0Sstevel@tonic-gate {
171*0Sstevel@tonic-gate 	char *cbuf = tsdalloc(_T_CTIME, CBUFSIZ, NULL);
172*0Sstevel@tonic-gate 	struct tm *p;
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate 	if (cbuf == NULL)
175*0Sstevel@tonic-gate 		return (NULL);
176*0Sstevel@tonic-gate 	p = localtime(t);
177*0Sstevel@tonic-gate 	if (p == NULL)
178*0Sstevel@tonic-gate 		return (NULL);
179*0Sstevel@tonic-gate 	return (__posix_asctime_r(p, cbuf));
180*0Sstevel@tonic-gate }
181*0Sstevel@tonic-gate 
182*0Sstevel@tonic-gate char *
183*0Sstevel@tonic-gate asctime(const struct tm *t)
184*0Sstevel@tonic-gate {
185*0Sstevel@tonic-gate 	char *cbuf = tsdalloc(_T_CTIME, CBUFSIZ, NULL);
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate 	if (cbuf == NULL)
188*0Sstevel@tonic-gate 		return (NULL);
189*0Sstevel@tonic-gate 	return (__posix_asctime_r(t, cbuf));
190*0Sstevel@tonic-gate }
191