xref: /titanic_44/usr/src/lib/libc/port/gen/cftime.c (revision ee169c7e77bc5d28a401dde8533cbd38afd24ae1)
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  */
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate /*
23*ee169c7eSGary Mills  * Copyright (c) 2014 Gary Mills
24*ee169c7eSGary Mills  *
257257d1b4Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
267c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297257d1b4Sraf /*	Copyright (c) 1988 AT&T	*/
307257d1b4Sraf /*	  All Rights Reserved  	*/
317257d1b4Sraf 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  * This routine converts time as follows.  The epoch is 0000  Jan  1
347c478bd9Sstevel@tonic-gate  * 1970  GMT.   The  argument  time  is  in seconds since then.  The
357c478bd9Sstevel@tonic-gate  * localtime(t) entry returns a pointer to an array containing:
367c478bd9Sstevel@tonic-gate  *
377c478bd9Sstevel@tonic-gate  *		  seconds (0-59)
387c478bd9Sstevel@tonic-gate  *		  minutes (0-59)
397c478bd9Sstevel@tonic-gate  *		  hours (0-23)
407c478bd9Sstevel@tonic-gate  *		  day of month (1-31)
417c478bd9Sstevel@tonic-gate  *		  month (0-11)
427c478bd9Sstevel@tonic-gate  *		  year
437c478bd9Sstevel@tonic-gate  *		  weekday (0-6, Sun is 0)
447c478bd9Sstevel@tonic-gate  *		  day of the year
457c478bd9Sstevel@tonic-gate  *		  daylight savings flag
467c478bd9Sstevel@tonic-gate  *
477c478bd9Sstevel@tonic-gate  * The routine corrects for daylight saving time and  will  work  in
487c478bd9Sstevel@tonic-gate  * any  time  zone provided "timezone" is adjusted to the difference
497c478bd9Sstevel@tonic-gate  * between Greenwich and local standard time (measured in seconds).
507c478bd9Sstevel@tonic-gate  *
517c478bd9Sstevel@tonic-gate  *	 ascftime(buf, format, t)	-> where t is produced by localtime
527c478bd9Sstevel@tonic-gate  *				           and returns a ptr to a character
537c478bd9Sstevel@tonic-gate  *				           string that has the ascii time in
547c478bd9Sstevel@tonic-gate  *				           the format specified by the format
557c478bd9Sstevel@tonic-gate  *				           argument (see date(1) for format
567c478bd9Sstevel@tonic-gate  *				           syntax).
577c478bd9Sstevel@tonic-gate  *
587c478bd9Sstevel@tonic-gate  *	 cftime(buf, format, t) 	-> just calls ascftime.
597c478bd9Sstevel@tonic-gate  *
607c478bd9Sstevel@tonic-gate  *
617c478bd9Sstevel@tonic-gate  *
627c478bd9Sstevel@tonic-gate  */
637c478bd9Sstevel@tonic-gate 
647257d1b4Sraf #include	"lint.h"
657c478bd9Sstevel@tonic-gate #include	<mtlib.h>
667c478bd9Sstevel@tonic-gate #include	<stddef.h>
677c478bd9Sstevel@tonic-gate #include	<time.h>
687c478bd9Sstevel@tonic-gate #include	<limits.h>
697c478bd9Sstevel@tonic-gate #include	<stdlib.h>
707c478bd9Sstevel@tonic-gate #include	<thread.h>
717c478bd9Sstevel@tonic-gate #include	<synch.h>
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate int
cftime(char * buf,char * format,const time_t * t)747c478bd9Sstevel@tonic-gate cftime(char *buf, char *format, const time_t *t)
757c478bd9Sstevel@tonic-gate {
767c478bd9Sstevel@tonic-gate 	struct tm res;
777c478bd9Sstevel@tonic-gate 	struct tm *p;
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	p = localtime_r(t, &res);
807c478bd9Sstevel@tonic-gate 	if (p == NULL) {
817c478bd9Sstevel@tonic-gate 		*buf = '\0';
827c478bd9Sstevel@tonic-gate 		return (0);
837c478bd9Sstevel@tonic-gate 	}
847c478bd9Sstevel@tonic-gate 	/* LINTED do not use ascftime() */
857c478bd9Sstevel@tonic-gate 	return (ascftime(buf, format, p));
867c478bd9Sstevel@tonic-gate }
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate int
ascftime(char * buf,const char * format,const struct tm * tm)897c478bd9Sstevel@tonic-gate ascftime(char *buf, const char *format, const struct tm *tm)
907c478bd9Sstevel@tonic-gate {
917c478bd9Sstevel@tonic-gate 	/* Set format string, if not already set */
927c478bd9Sstevel@tonic-gate 	if (format == NULL || *format == '\0')
937c478bd9Sstevel@tonic-gate 		if (((format = getenv("CFTIME")) == 0) || *format == 0)
94*ee169c7eSGary Mills 			format =  "%+";
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	return ((int)strftime(buf, LONG_MAX, format, tm));
977c478bd9Sstevel@tonic-gate }
98