10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
56812Sraf * Common Development and Distribution License (the "License").
66812Sraf * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
216812Sraf
220Sstevel@tonic-gate /*
236812Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */
280Sstevel@tonic-gate /* All Rights Reserved */
290Sstevel@tonic-gate
306812Sraf #pragma ident "%Z%%M% %I% %E% SMI"
310Sstevel@tonic-gate
320Sstevel@tonic-gate /*
330Sstevel@tonic-gate * This routine converts time as follows.
340Sstevel@tonic-gate * The epoch is 0000 Jan 1 1970 GMT.
350Sstevel@tonic-gate * The argument time is in seconds since then.
360Sstevel@tonic-gate * The localtime(t) entry returns a pointer to an array
370Sstevel@tonic-gate * containing
380Sstevel@tonic-gate * seconds (0-59)
390Sstevel@tonic-gate * minutes (0-59)
400Sstevel@tonic-gate * hours (0-23)
410Sstevel@tonic-gate * day of month (1-31)
420Sstevel@tonic-gate * month (0-11)
430Sstevel@tonic-gate * year-1970
440Sstevel@tonic-gate * weekday (0-6, Sun is 0)
450Sstevel@tonic-gate * day of the year
460Sstevel@tonic-gate * daylight savings flag
470Sstevel@tonic-gate *
480Sstevel@tonic-gate * The routine corrects for daylight saving
490Sstevel@tonic-gate * time and will work in any time zone provided
500Sstevel@tonic-gate * "timezone" is adjusted to the difference between
510Sstevel@tonic-gate * Greenwich and local standard time (measured in seconds).
520Sstevel@tonic-gate * In places like Michigan "daylight" must
530Sstevel@tonic-gate * be initialized to 0 to prevent the conversion
540Sstevel@tonic-gate * to daylight time.
550Sstevel@tonic-gate * There is a table which accounts for the peculiarities
560Sstevel@tonic-gate * undergone by daylight time in 1974-1975.
570Sstevel@tonic-gate *
580Sstevel@tonic-gate * The routine does not work
590Sstevel@tonic-gate * in Saudi Arabia which runs on Solar time.
600Sstevel@tonic-gate *
610Sstevel@tonic-gate * asctime(tvec)
620Sstevel@tonic-gate * where tvec is produced by localtime
630Sstevel@tonic-gate * returns a ptr to a character string
640Sstevel@tonic-gate * that has the ascii time in the form
650Sstevel@tonic-gate * Thu Jan 01 00:00:00 1970\n\0
660Sstevel@tonic-gate * 01234567890123456789012345
670Sstevel@tonic-gate * 0 1 2
680Sstevel@tonic-gate *
690Sstevel@tonic-gate * ctime(t) just calls localtime, then asctime.
700Sstevel@tonic-gate *
710Sstevel@tonic-gate * tzset() looks for an environment variable named
720Sstevel@tonic-gate * TZ.
730Sstevel@tonic-gate * If the variable is present, it will set the external
740Sstevel@tonic-gate * variables "timezone", "altzone", "daylight", and "tzname"
750Sstevel@tonic-gate * appropriately. It is called by localtime, and
760Sstevel@tonic-gate * may also be called explicitly by the user.
770Sstevel@tonic-gate */
780Sstevel@tonic-gate
796812Sraf #include "lint.h"
800Sstevel@tonic-gate #include <mtlib.h>
810Sstevel@tonic-gate #include <sys/types.h>
820Sstevel@tonic-gate #include <time.h>
830Sstevel@tonic-gate #include <errno.h>
840Sstevel@tonic-gate #include <thread.h>
850Sstevel@tonic-gate #include <synch.h>
860Sstevel@tonic-gate #include "libc.h"
870Sstevel@tonic-gate #include "tsd.h"
880Sstevel@tonic-gate
890Sstevel@tonic-gate #define dysize(A) (((A)%4)? 365: 366)
900Sstevel@tonic-gate #define CBUFSIZ 26
910Sstevel@tonic-gate
920Sstevel@tonic-gate static char *
ct_numb(char * cp,int n,char pad)93*7180Sjwadams ct_numb(char *cp, int n, char pad)
940Sstevel@tonic-gate {
950Sstevel@tonic-gate cp++;
960Sstevel@tonic-gate if (n >= 10)
970Sstevel@tonic-gate *cp++ = (n / 10) % 10 + '0';
980Sstevel@tonic-gate else
99*7180Sjwadams *cp++ = pad;
1000Sstevel@tonic-gate *cp++ = n % 10 + '0';
1010Sstevel@tonic-gate return (cp);
1020Sstevel@tonic-gate }
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate /*
1050Sstevel@tonic-gate * POSIX.1c standard version of the function asctime_r.
1060Sstevel@tonic-gate * User gets it via static asctime_r from the header file.
1070Sstevel@tonic-gate */
1080Sstevel@tonic-gate char *
__posix_asctime_r(const struct tm * t,char * cbuf)1090Sstevel@tonic-gate __posix_asctime_r(const struct tm *t, char *cbuf)
1100Sstevel@tonic-gate {
1110Sstevel@tonic-gate char *cp;
1120Sstevel@tonic-gate const char *ncp;
113*7180Sjwadams const char *Date = "Day Mon 00 00:00:00 YYYY\n";
1140Sstevel@tonic-gate const char *Day = "SunMonTueWedThuFriSat";
1150Sstevel@tonic-gate const char *Month = "JanFebMarAprMayJunJulAugSepOctNovDec";
1160Sstevel@tonic-gate
117*7180Sjwadams int year = t->tm_year + 1900;
118*7180Sjwadams
1190Sstevel@tonic-gate cp = cbuf;
1206812Sraf for (ncp = Date; *cp++ = *ncp++; /* */)
1216812Sraf ;
1220Sstevel@tonic-gate ncp = Day + (3 * t->tm_wday);
1230Sstevel@tonic-gate cp = cbuf;
1240Sstevel@tonic-gate *cp++ = *ncp++;
1250Sstevel@tonic-gate *cp++ = *ncp++;
1260Sstevel@tonic-gate *cp++ = *ncp++;
1270Sstevel@tonic-gate cp++;
128*7180Sjwadams ncp = Month + (3 * t->tm_mon);
1290Sstevel@tonic-gate *cp++ = *ncp++;
1300Sstevel@tonic-gate *cp++ = *ncp++;
1310Sstevel@tonic-gate *cp++ = *ncp++;
132*7180Sjwadams cp = ct_numb(cp, t->tm_mday, ' ');
133*7180Sjwadams cp = ct_numb(cp, t->tm_hour, '0');
134*7180Sjwadams cp = ct_numb(cp, t->tm_min, '0');
135*7180Sjwadams cp = ct_numb(cp, t->tm_sec, '0');
136*7180Sjwadams
137*7180Sjwadams if (year < 0 || year >= 10000) {
138*7180Sjwadams /* Only positive, 4-digit years are supported */
1390Sstevel@tonic-gate errno = EOVERFLOW;
1400Sstevel@tonic-gate return (NULL);
1410Sstevel@tonic-gate }
142*7180Sjwadams cp = ct_numb(cp, year / 100, '0');
143*7180Sjwadams cp--;
144*7180Sjwadams (void) ct_numb(cp, year, '0');
1450Sstevel@tonic-gate return (cbuf);
1460Sstevel@tonic-gate }
1470Sstevel@tonic-gate
1480Sstevel@tonic-gate /*
1490Sstevel@tonic-gate * POSIX.1c Draft-6 version of the function asctime_r.
1500Sstevel@tonic-gate * It was implemented by Solaris 2.3.
1510Sstevel@tonic-gate */
1520Sstevel@tonic-gate char *
asctime_r(const struct tm * t,char * cbuf,int buflen)1530Sstevel@tonic-gate asctime_r(const struct tm *t, char *cbuf, int buflen)
1540Sstevel@tonic-gate {
1550Sstevel@tonic-gate if (buflen < CBUFSIZ) {
1560Sstevel@tonic-gate errno = ERANGE;
1570Sstevel@tonic-gate return (NULL);
1580Sstevel@tonic-gate }
1590Sstevel@tonic-gate return (__posix_asctime_r(t, cbuf));
1600Sstevel@tonic-gate }
1610Sstevel@tonic-gate
1620Sstevel@tonic-gate char *
ctime(const time_t * t)1630Sstevel@tonic-gate ctime(const time_t *t)
1640Sstevel@tonic-gate {
1650Sstevel@tonic-gate char *cbuf = tsdalloc(_T_CTIME, CBUFSIZ, NULL);
1660Sstevel@tonic-gate struct tm *p;
1670Sstevel@tonic-gate
1680Sstevel@tonic-gate if (cbuf == NULL)
1690Sstevel@tonic-gate return (NULL);
1700Sstevel@tonic-gate p = localtime(t);
1710Sstevel@tonic-gate if (p == NULL)
1720Sstevel@tonic-gate return (NULL);
1730Sstevel@tonic-gate return (__posix_asctime_r(p, cbuf));
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate
1760Sstevel@tonic-gate char *
asctime(const struct tm * t)1770Sstevel@tonic-gate asctime(const struct tm *t)
1780Sstevel@tonic-gate {
1790Sstevel@tonic-gate char *cbuf = tsdalloc(_T_CTIME, CBUFSIZ, NULL);
1800Sstevel@tonic-gate
1810Sstevel@tonic-gate if (cbuf == NULL)
1820Sstevel@tonic-gate return (NULL);
1830Sstevel@tonic-gate return (__posix_asctime_r(t, cbuf));
1840Sstevel@tonic-gate }
185