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