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