xref: /onnv-gate/usr/src/lib/libc/port/gen/cftime.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 
22*6812Sraf /*
23*6812Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24*6812Sraf  * Use is subject to license terms.
25*6812Sraf  */
26*6812Sraf 
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.  The epoch is 0000  Jan  1
340Sstevel@tonic-gate  * 1970  GMT.   The  argument  time  is  in seconds since then.  The
350Sstevel@tonic-gate  * localtime(t) entry returns a pointer to an array containing:
360Sstevel@tonic-gate  *
370Sstevel@tonic-gate  *		  seconds (0-59)
380Sstevel@tonic-gate  *		  minutes (0-59)
390Sstevel@tonic-gate  *		  hours (0-23)
400Sstevel@tonic-gate  *		  day of month (1-31)
410Sstevel@tonic-gate  *		  month (0-11)
420Sstevel@tonic-gate  *		  year
430Sstevel@tonic-gate  *		  weekday (0-6, Sun is 0)
440Sstevel@tonic-gate  *		  day of the year
450Sstevel@tonic-gate  *		  daylight savings flag
460Sstevel@tonic-gate  *
470Sstevel@tonic-gate  * The routine corrects for daylight saving time and  will  work  in
480Sstevel@tonic-gate  * any  time  zone provided "timezone" is adjusted to the difference
490Sstevel@tonic-gate  * between Greenwich and local standard time (measured in seconds).
500Sstevel@tonic-gate  *
510Sstevel@tonic-gate  *	 ascftime(buf, format, t)	-> where t is produced by localtime
520Sstevel@tonic-gate  *				           and returns a ptr to a character
530Sstevel@tonic-gate  *				           string that has the ascii time in
540Sstevel@tonic-gate  *				           the format specified by the format
550Sstevel@tonic-gate  *				           argument (see date(1) for format
560Sstevel@tonic-gate  *				           syntax).
570Sstevel@tonic-gate  *
580Sstevel@tonic-gate  *	 cftime(buf, format, t) 	-> just calls ascftime.
590Sstevel@tonic-gate  *
600Sstevel@tonic-gate  *
610Sstevel@tonic-gate  *
620Sstevel@tonic-gate  */
630Sstevel@tonic-gate 
64*6812Sraf #include	"lint.h"
650Sstevel@tonic-gate #include	<mtlib.h>
660Sstevel@tonic-gate #include	<stddef.h>
670Sstevel@tonic-gate #include	<time.h>
680Sstevel@tonic-gate #include	<limits.h>
690Sstevel@tonic-gate #include	<stdlib.h>
700Sstevel@tonic-gate #include	<thread.h>
710Sstevel@tonic-gate #include	<synch.h>
720Sstevel@tonic-gate 
730Sstevel@tonic-gate int
cftime(char * buf,char * format,const time_t * t)740Sstevel@tonic-gate cftime(char *buf, char *format, const time_t *t)
750Sstevel@tonic-gate {
760Sstevel@tonic-gate 	struct tm res;
770Sstevel@tonic-gate 	struct tm *p;
780Sstevel@tonic-gate 
790Sstevel@tonic-gate 	p = localtime_r(t, &res);
800Sstevel@tonic-gate 	if (p == NULL) {
810Sstevel@tonic-gate 		*buf = '\0';
820Sstevel@tonic-gate 		return (0);
830Sstevel@tonic-gate 	}
840Sstevel@tonic-gate 	/* LINTED do not use ascftime() */
850Sstevel@tonic-gate 	return (ascftime(buf, format, p));
860Sstevel@tonic-gate }
870Sstevel@tonic-gate 
880Sstevel@tonic-gate int
ascftime(char * buf,const char * format,const struct tm * tm)890Sstevel@tonic-gate ascftime(char *buf, const char *format, const struct tm *tm)
900Sstevel@tonic-gate {
910Sstevel@tonic-gate 	/* Set format string, if not already set */
920Sstevel@tonic-gate 	if (format == NULL || *format == '\0')
930Sstevel@tonic-gate 		if (((format = getenv("CFTIME")) == 0) || *format == 0)
940Sstevel@tonic-gate 			format =  "%C";
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 	return ((int)strftime(buf, LONG_MAX, format, tm));
970Sstevel@tonic-gate }
98