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 */
28*00ae5933SToomas Soome /* All Rights Reserved */
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate * This routine converts time as follows.
327c478bd9Sstevel@tonic-gate * The epoch is 0000 Jan 1 1970 GMT.
337c478bd9Sstevel@tonic-gate * The argument time is in seconds since then.
347c478bd9Sstevel@tonic-gate * The localtime(t) entry returns a pointer to an array
357c478bd9Sstevel@tonic-gate * containing
367c478bd9Sstevel@tonic-gate * seconds (0-59)
377c478bd9Sstevel@tonic-gate * minutes (0-59)
387c478bd9Sstevel@tonic-gate * hours (0-23)
397c478bd9Sstevel@tonic-gate * day of month (1-31)
407c478bd9Sstevel@tonic-gate * month (0-11)
417c478bd9Sstevel@tonic-gate * year-1970
427c478bd9Sstevel@tonic-gate * weekday (0-6, Sun is 0)
437c478bd9Sstevel@tonic-gate * day of the year
447c478bd9Sstevel@tonic-gate * daylight savings flag
457c478bd9Sstevel@tonic-gate *
467c478bd9Sstevel@tonic-gate * The routine corrects for daylight saving
477c478bd9Sstevel@tonic-gate * time and will work in any time zone provided
487c478bd9Sstevel@tonic-gate * "timezone" is adjusted to the difference between
497c478bd9Sstevel@tonic-gate * Greenwich and local standard time (measured in seconds).
507c478bd9Sstevel@tonic-gate * In places like Michigan "daylight" must
517c478bd9Sstevel@tonic-gate * be initialized to 0 to prevent the conversion
527c478bd9Sstevel@tonic-gate * to daylight time.
537c478bd9Sstevel@tonic-gate * There is a table which accounts for the peculiarities
547c478bd9Sstevel@tonic-gate * undergone by daylight time in 1974-1975.
557c478bd9Sstevel@tonic-gate *
567c478bd9Sstevel@tonic-gate * The routine does not work
577c478bd9Sstevel@tonic-gate * in Saudi Arabia which runs on Solar time.
587c478bd9Sstevel@tonic-gate *
597c478bd9Sstevel@tonic-gate * asctime(tvec)
607c478bd9Sstevel@tonic-gate * where tvec is produced by localtime
617c478bd9Sstevel@tonic-gate * returns a ptr to a character string
627c478bd9Sstevel@tonic-gate * that has the ascii time in the form
637c478bd9Sstevel@tonic-gate * Thu Jan 01 00:00:00 1970\n\0
647c478bd9Sstevel@tonic-gate * 01234567890123456789012345
657c478bd9Sstevel@tonic-gate * 0 1 2
667c478bd9Sstevel@tonic-gate *
677c478bd9Sstevel@tonic-gate * ctime(t) just calls localtime, then asctime.
687c478bd9Sstevel@tonic-gate *
697c478bd9Sstevel@tonic-gate * tzset() looks for an environment variable named
707c478bd9Sstevel@tonic-gate * TZ.
717c478bd9Sstevel@tonic-gate * If the variable is present, it will set the external
727c478bd9Sstevel@tonic-gate * variables "timezone", "altzone", "daylight", and "tzname"
737c478bd9Sstevel@tonic-gate * appropriately. It is called by localtime, and
747c478bd9Sstevel@tonic-gate * may also be called explicitly by the user.
757c478bd9Sstevel@tonic-gate */
767c478bd9Sstevel@tonic-gate
777257d1b4Sraf #include "lint.h"
787c478bd9Sstevel@tonic-gate #include <mtlib.h>
797c478bd9Sstevel@tonic-gate #include <sys/types.h>
807c478bd9Sstevel@tonic-gate #include <time.h>
817c478bd9Sstevel@tonic-gate #include <errno.h>
827c478bd9Sstevel@tonic-gate #include <thread.h>
837c478bd9Sstevel@tonic-gate #include <synch.h>
847c478bd9Sstevel@tonic-gate #include "libc.h"
857c478bd9Sstevel@tonic-gate #include "tsd.h"
867c478bd9Sstevel@tonic-gate
877c478bd9Sstevel@tonic-gate #define dysize(A) (((A)%4)? 365: 366)
887c478bd9Sstevel@tonic-gate #define CBUFSIZ 26
897c478bd9Sstevel@tonic-gate
907c478bd9Sstevel@tonic-gate static char *
ct_numb(char * cp,int n,char pad)91a727d47aSjwadams ct_numb(char *cp, int n, char pad)
927c478bd9Sstevel@tonic-gate {
937c478bd9Sstevel@tonic-gate cp++;
947c478bd9Sstevel@tonic-gate if (n >= 10)
957c478bd9Sstevel@tonic-gate *cp++ = (n / 10) % 10 + '0';
967c478bd9Sstevel@tonic-gate else
97a727d47aSjwadams *cp++ = pad;
987c478bd9Sstevel@tonic-gate *cp++ = n % 10 + '0';
997c478bd9Sstevel@tonic-gate return (cp);
1007c478bd9Sstevel@tonic-gate }
1017c478bd9Sstevel@tonic-gate
1027c478bd9Sstevel@tonic-gate /*
1037c478bd9Sstevel@tonic-gate * POSIX.1c standard version of the function asctime_r.
1047c478bd9Sstevel@tonic-gate * User gets it via static asctime_r from the header file.
1057c478bd9Sstevel@tonic-gate */
1067c478bd9Sstevel@tonic-gate char *
__posix_asctime_r(const struct tm * t,char * cbuf)1077c478bd9Sstevel@tonic-gate __posix_asctime_r(const struct tm *t, char *cbuf)
1087c478bd9Sstevel@tonic-gate {
1097c478bd9Sstevel@tonic-gate char *cp;
1107c478bd9Sstevel@tonic-gate const char *ncp;
111a727d47aSjwadams const char *Date = "Day Mon 00 00:00:00 YYYY\n";
1127c478bd9Sstevel@tonic-gate const char *Day = "SunMonTueWedThuFriSat";
1137c478bd9Sstevel@tonic-gate const char *Month = "JanFebMarAprMayJunJulAugSepOctNovDec";
1147c478bd9Sstevel@tonic-gate
115a727d47aSjwadams int year = t->tm_year + 1900;
116a727d47aSjwadams
1177c478bd9Sstevel@tonic-gate cp = cbuf;
118*00ae5933SToomas Soome for (ncp = Date; (*cp++ = *ncp++) != '\0'; /* */)
1197257d1b4Sraf ;
1207c478bd9Sstevel@tonic-gate ncp = Day + (3 * t->tm_wday);
1217c478bd9Sstevel@tonic-gate cp = cbuf;
1227c478bd9Sstevel@tonic-gate *cp++ = *ncp++;
1237c478bd9Sstevel@tonic-gate *cp++ = *ncp++;
1247c478bd9Sstevel@tonic-gate *cp++ = *ncp++;
1257c478bd9Sstevel@tonic-gate cp++;
126a727d47aSjwadams ncp = Month + (3 * t->tm_mon);
1277c478bd9Sstevel@tonic-gate *cp++ = *ncp++;
1287c478bd9Sstevel@tonic-gate *cp++ = *ncp++;
1297c478bd9Sstevel@tonic-gate *cp++ = *ncp++;
130a727d47aSjwadams cp = ct_numb(cp, t->tm_mday, ' ');
131a727d47aSjwadams cp = ct_numb(cp, t->tm_hour, '0');
132a727d47aSjwadams cp = ct_numb(cp, t->tm_min, '0');
133a727d47aSjwadams cp = ct_numb(cp, t->tm_sec, '0');
134a727d47aSjwadams
135a727d47aSjwadams if (year < 0 || year >= 10000) {
136a727d47aSjwadams /* Only positive, 4-digit years are supported */
1377c478bd9Sstevel@tonic-gate errno = EOVERFLOW;
1387c478bd9Sstevel@tonic-gate return (NULL);
1397c478bd9Sstevel@tonic-gate }
140a727d47aSjwadams cp = ct_numb(cp, year / 100, '0');
141a727d47aSjwadams cp--;
142a727d47aSjwadams (void) ct_numb(cp, year, '0');
1437c478bd9Sstevel@tonic-gate return (cbuf);
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate
1467c478bd9Sstevel@tonic-gate /*
1477c478bd9Sstevel@tonic-gate * POSIX.1c Draft-6 version of the function asctime_r.
1487c478bd9Sstevel@tonic-gate * It was implemented by Solaris 2.3.
1497c478bd9Sstevel@tonic-gate */
1507c478bd9Sstevel@tonic-gate char *
asctime_r(const struct tm * t,char * cbuf,int buflen)1517c478bd9Sstevel@tonic-gate asctime_r(const struct tm *t, char *cbuf, int buflen)
1527c478bd9Sstevel@tonic-gate {
1537c478bd9Sstevel@tonic-gate if (buflen < CBUFSIZ) {
1547c478bd9Sstevel@tonic-gate errno = ERANGE;
1557c478bd9Sstevel@tonic-gate return (NULL);
1567c478bd9Sstevel@tonic-gate }
1577c478bd9Sstevel@tonic-gate return (__posix_asctime_r(t, cbuf));
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate
1607c478bd9Sstevel@tonic-gate char *
ctime(const time_t * t)1617c478bd9Sstevel@tonic-gate ctime(const time_t *t)
1627c478bd9Sstevel@tonic-gate {
1637c478bd9Sstevel@tonic-gate char *cbuf = tsdalloc(_T_CTIME, CBUFSIZ, NULL);
1647c478bd9Sstevel@tonic-gate struct tm *p;
1657c478bd9Sstevel@tonic-gate
1667c478bd9Sstevel@tonic-gate if (cbuf == NULL)
1677c478bd9Sstevel@tonic-gate return (NULL);
1687c478bd9Sstevel@tonic-gate p = localtime(t);
1697c478bd9Sstevel@tonic-gate if (p == NULL)
1707c478bd9Sstevel@tonic-gate return (NULL);
1717c478bd9Sstevel@tonic-gate return (__posix_asctime_r(p, cbuf));
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate
1747c478bd9Sstevel@tonic-gate char *
asctime(const struct tm * t)1757c478bd9Sstevel@tonic-gate asctime(const struct tm *t)
1767c478bd9Sstevel@tonic-gate {
1777c478bd9Sstevel@tonic-gate char *cbuf = tsdalloc(_T_CTIME, CBUFSIZ, NULL);
1787c478bd9Sstevel@tonic-gate
1797c478bd9Sstevel@tonic-gate if (cbuf == NULL)
1807c478bd9Sstevel@tonic-gate return (NULL);
1817c478bd9Sstevel@tonic-gate return (__posix_asctime_r(t, cbuf));
1827c478bd9Sstevel@tonic-gate }
183