17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57257d1b4Sraf * Common Development and Distribution License (the "License").
67257d1b4Sraf * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217257d1b4Sraf
227c478bd9Sstevel@tonic-gate /*
237257d1b4Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */
287c478bd9Sstevel@tonic-gate /* All Rights Reserved */
297c478bd9Sstevel@tonic-gate
307257d1b4Sraf #pragma ident "%Z%%M% %I% %E% SMI"
317c478bd9Sstevel@tonic-gate
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate * This routine converts time as follows.
347c478bd9Sstevel@tonic-gate * The epoch is 0000 Jan 1 1970 GMT.
357c478bd9Sstevel@tonic-gate * The argument time is in seconds since then.
367c478bd9Sstevel@tonic-gate * The localtime(t) entry returns a pointer to an array
377c478bd9Sstevel@tonic-gate * containing
387c478bd9Sstevel@tonic-gate * seconds (0-59)
397c478bd9Sstevel@tonic-gate * minutes (0-59)
407c478bd9Sstevel@tonic-gate * hours (0-23)
417c478bd9Sstevel@tonic-gate * day of month (1-31)
427c478bd9Sstevel@tonic-gate * month (0-11)
437c478bd9Sstevel@tonic-gate * year-1970
447c478bd9Sstevel@tonic-gate * weekday (0-6, Sun is 0)
457c478bd9Sstevel@tonic-gate * day of the year
467c478bd9Sstevel@tonic-gate * daylight savings flag
477c478bd9Sstevel@tonic-gate *
487c478bd9Sstevel@tonic-gate * The routine corrects for daylight saving
497c478bd9Sstevel@tonic-gate * time and will work in any time zone provided
507c478bd9Sstevel@tonic-gate * "timezone" is adjusted to the difference between
517c478bd9Sstevel@tonic-gate * Greenwich and local standard time (measured in seconds).
527c478bd9Sstevel@tonic-gate * In places like Michigan "daylight" must
537c478bd9Sstevel@tonic-gate * be initialized to 0 to prevent the conversion
547c478bd9Sstevel@tonic-gate * to daylight time.
557c478bd9Sstevel@tonic-gate * There is a table which accounts for the peculiarities
567c478bd9Sstevel@tonic-gate * undergone by daylight time in 1974-1975.
577c478bd9Sstevel@tonic-gate *
587c478bd9Sstevel@tonic-gate * The routine does not work
597c478bd9Sstevel@tonic-gate * in Saudi Arabia which runs on Solar time.
607c478bd9Sstevel@tonic-gate *
617c478bd9Sstevel@tonic-gate * asctime(tvec)
627c478bd9Sstevel@tonic-gate * where tvec is produced by localtime
637c478bd9Sstevel@tonic-gate * returns a ptr to a character string
647c478bd9Sstevel@tonic-gate * that has the ascii time in the form
657c478bd9Sstevel@tonic-gate * Thu Jan 01 00:00:00 1970\n\0
667c478bd9Sstevel@tonic-gate * 01234567890123456789012345
677c478bd9Sstevel@tonic-gate * 0 1 2
687c478bd9Sstevel@tonic-gate *
697c478bd9Sstevel@tonic-gate * ctime(t) just calls localtime, then asctime.
707c478bd9Sstevel@tonic-gate *
717c478bd9Sstevel@tonic-gate * tzset() looks for an environment variable named
727c478bd9Sstevel@tonic-gate * TZ.
737c478bd9Sstevel@tonic-gate * If the variable is present, it will set the external
747c478bd9Sstevel@tonic-gate * variables "timezone", "altzone", "daylight", and "tzname"
757c478bd9Sstevel@tonic-gate * appropriately. It is called by localtime, and
767c478bd9Sstevel@tonic-gate * may also be called explicitly by the user.
777c478bd9Sstevel@tonic-gate */
787c478bd9Sstevel@tonic-gate
797257d1b4Sraf #include "lint.h"
807c478bd9Sstevel@tonic-gate #include <mtlib.h>
817c478bd9Sstevel@tonic-gate #include <sys/types.h>
827c478bd9Sstevel@tonic-gate #include <time.h>
837c478bd9Sstevel@tonic-gate #include <errno.h>
847c478bd9Sstevel@tonic-gate #include <thread.h>
857c478bd9Sstevel@tonic-gate #include <synch.h>
867c478bd9Sstevel@tonic-gate #include "libc.h"
877c478bd9Sstevel@tonic-gate #include "tsd.h"
887c478bd9Sstevel@tonic-gate
897c478bd9Sstevel@tonic-gate #define dysize(A) (((A)%4)? 365: 366)
907c478bd9Sstevel@tonic-gate #define CBUFSIZ 26
917c478bd9Sstevel@tonic-gate
927c478bd9Sstevel@tonic-gate static char *
ct_numb(char * cp,int n,char pad)93*a727d47aSjwadams ct_numb(char *cp, int n, char pad)
947c478bd9Sstevel@tonic-gate {
957c478bd9Sstevel@tonic-gate cp++;
967c478bd9Sstevel@tonic-gate if (n >= 10)
977c478bd9Sstevel@tonic-gate *cp++ = (n / 10) % 10 + '0';
987c478bd9Sstevel@tonic-gate else
99*a727d47aSjwadams *cp++ = pad;
1007c478bd9Sstevel@tonic-gate *cp++ = n % 10 + '0';
1017c478bd9Sstevel@tonic-gate return (cp);
1027c478bd9Sstevel@tonic-gate }
1037c478bd9Sstevel@tonic-gate
1047c478bd9Sstevel@tonic-gate /*
1057c478bd9Sstevel@tonic-gate * POSIX.1c standard version of the function asctime_r.
1067c478bd9Sstevel@tonic-gate * User gets it via static asctime_r from the header file.
1077c478bd9Sstevel@tonic-gate */
1087c478bd9Sstevel@tonic-gate char *
__posix_asctime_r(const struct tm * t,char * cbuf)1097c478bd9Sstevel@tonic-gate __posix_asctime_r(const struct tm *t, char *cbuf)
1107c478bd9Sstevel@tonic-gate {
1117c478bd9Sstevel@tonic-gate char *cp;
1127c478bd9Sstevel@tonic-gate const char *ncp;
113*a727d47aSjwadams const char *Date = "Day Mon 00 00:00:00 YYYY\n";
1147c478bd9Sstevel@tonic-gate const char *Day = "SunMonTueWedThuFriSat";
1157c478bd9Sstevel@tonic-gate const char *Month = "JanFebMarAprMayJunJulAugSepOctNovDec";
1167c478bd9Sstevel@tonic-gate
117*a727d47aSjwadams int year = t->tm_year + 1900;
118*a727d47aSjwadams
1197c478bd9Sstevel@tonic-gate cp = cbuf;
1207257d1b4Sraf for (ncp = Date; *cp++ = *ncp++; /* */)
1217257d1b4Sraf ;
1227c478bd9Sstevel@tonic-gate ncp = Day + (3 * t->tm_wday);
1237c478bd9Sstevel@tonic-gate cp = cbuf;
1247c478bd9Sstevel@tonic-gate *cp++ = *ncp++;
1257c478bd9Sstevel@tonic-gate *cp++ = *ncp++;
1267c478bd9Sstevel@tonic-gate *cp++ = *ncp++;
1277c478bd9Sstevel@tonic-gate cp++;
128*a727d47aSjwadams ncp = Month + (3 * t->tm_mon);
1297c478bd9Sstevel@tonic-gate *cp++ = *ncp++;
1307c478bd9Sstevel@tonic-gate *cp++ = *ncp++;
1317c478bd9Sstevel@tonic-gate *cp++ = *ncp++;
132*a727d47aSjwadams cp = ct_numb(cp, t->tm_mday, ' ');
133*a727d47aSjwadams cp = ct_numb(cp, t->tm_hour, '0');
134*a727d47aSjwadams cp = ct_numb(cp, t->tm_min, '0');
135*a727d47aSjwadams cp = ct_numb(cp, t->tm_sec, '0');
136*a727d47aSjwadams
137*a727d47aSjwadams if (year < 0 || year >= 10000) {
138*a727d47aSjwadams /* Only positive, 4-digit years are supported */
1397c478bd9Sstevel@tonic-gate errno = EOVERFLOW;
1407c478bd9Sstevel@tonic-gate return (NULL);
1417c478bd9Sstevel@tonic-gate }
142*a727d47aSjwadams cp = ct_numb(cp, year / 100, '0');
143*a727d47aSjwadams cp--;
144*a727d47aSjwadams (void) ct_numb(cp, year, '0');
1457c478bd9Sstevel@tonic-gate return (cbuf);
1467c478bd9Sstevel@tonic-gate }
1477c478bd9Sstevel@tonic-gate
1487c478bd9Sstevel@tonic-gate /*
1497c478bd9Sstevel@tonic-gate * POSIX.1c Draft-6 version of the function asctime_r.
1507c478bd9Sstevel@tonic-gate * It was implemented by Solaris 2.3.
1517c478bd9Sstevel@tonic-gate */
1527c478bd9Sstevel@tonic-gate char *
asctime_r(const struct tm * t,char * cbuf,int buflen)1537c478bd9Sstevel@tonic-gate asctime_r(const struct tm *t, char *cbuf, int buflen)
1547c478bd9Sstevel@tonic-gate {
1557c478bd9Sstevel@tonic-gate if (buflen < CBUFSIZ) {
1567c478bd9Sstevel@tonic-gate errno = ERANGE;
1577c478bd9Sstevel@tonic-gate return (NULL);
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate return (__posix_asctime_r(t, cbuf));
1607c478bd9Sstevel@tonic-gate }
1617c478bd9Sstevel@tonic-gate
1627c478bd9Sstevel@tonic-gate char *
ctime(const time_t * t)1637c478bd9Sstevel@tonic-gate ctime(const time_t *t)
1647c478bd9Sstevel@tonic-gate {
1657c478bd9Sstevel@tonic-gate char *cbuf = tsdalloc(_T_CTIME, CBUFSIZ, NULL);
1667c478bd9Sstevel@tonic-gate struct tm *p;
1677c478bd9Sstevel@tonic-gate
1687c478bd9Sstevel@tonic-gate if (cbuf == NULL)
1697c478bd9Sstevel@tonic-gate return (NULL);
1707c478bd9Sstevel@tonic-gate p = localtime(t);
1717c478bd9Sstevel@tonic-gate if (p == NULL)
1727c478bd9Sstevel@tonic-gate return (NULL);
1737c478bd9Sstevel@tonic-gate return (__posix_asctime_r(p, cbuf));
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate
1767c478bd9Sstevel@tonic-gate char *
asctime(const struct tm * t)1777c478bd9Sstevel@tonic-gate asctime(const struct tm *t)
1787c478bd9Sstevel@tonic-gate {
1797c478bd9Sstevel@tonic-gate char *cbuf = tsdalloc(_T_CTIME, CBUFSIZ, NULL);
1807c478bd9Sstevel@tonic-gate
1817c478bd9Sstevel@tonic-gate if (cbuf == NULL)
1827c478bd9Sstevel@tonic-gate return (NULL);
1837c478bd9Sstevel@tonic-gate return (__posix_asctime_r(t, cbuf));
1847c478bd9Sstevel@tonic-gate }
185