xref: /onnv-gate/usr/src/lib/libc/port/gen/ctime_r.c (revision 6812:febeba71273d)
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
5*6812Sraf  * Common Development and Distribution License (the "License").
6*6812Sraf  * 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  */
21*6812Sraf 
220Sstevel@tonic-gate /*
23*6812Sraf  * 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 
30*6812Sraf #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 
79*6812Sraf #include "lint.h"
800Sstevel@tonic-gate #include <time.h>
810Sstevel@tonic-gate #include <sys/types.h>
820Sstevel@tonic-gate #include <errno.h>
830Sstevel@tonic-gate #include <thread.h>
840Sstevel@tonic-gate #include <synch.h>
850Sstevel@tonic-gate #include <mtlib.h>
860Sstevel@tonic-gate #include "libc.h"
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 
890Sstevel@tonic-gate /*
900Sstevel@tonic-gate  * POSIX.1c Draft-6 version of the function ctime_r.
910Sstevel@tonic-gate  * It was implemented by Solaris 2.3.
920Sstevel@tonic-gate  */
930Sstevel@tonic-gate char *
ctime_r(const time_t * t,char * buffer,int buflen)940Sstevel@tonic-gate ctime_r(const time_t *t, char *buffer, int buflen)
950Sstevel@tonic-gate {
960Sstevel@tonic-gate 	struct tm res;
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	if (localtime_r(t, &res) == NULL)
990Sstevel@tonic-gate 		return (NULL);
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	if (asctime_r(&res, buffer, buflen) == NULL)
1020Sstevel@tonic-gate 		return (NULL);
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	return (buffer);
1050Sstevel@tonic-gate }
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate /*
1080Sstevel@tonic-gate  * POSIX.1c standard version of the function ctime_r.
1090Sstevel@tonic-gate  * User gets it via static ctime_r from the header file.
1100Sstevel@tonic-gate  */
1110Sstevel@tonic-gate char *
__posix_ctime_r(const time_t * t,char * buffer)1120Sstevel@tonic-gate __posix_ctime_r(const time_t *t, char *buffer)
1130Sstevel@tonic-gate {
1140Sstevel@tonic-gate 	struct tm res;
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	if (localtime_r(t, &res) == NULL)
1170Sstevel@tonic-gate 		return (NULL);
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	if (__posix_asctime_r(&res, buffer) == NULL)
1200Sstevel@tonic-gate 		return (NULL);
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 	return (buffer);
1230Sstevel@tonic-gate }
124