xref: /onnv-gate/usr/src/lib/libast/common/comp/strftime.c (revision 12068:08a39a083754)
14887Schin /***********************************************************************
24887Schin *                                                                      *
34887Schin *               This software is part of the ast package               *
4*12068SRoger.Faulkner@Oracle.COM *          Copyright (c) 1985-2010 AT&T Intellectual Property          *
54887Schin *                      and is licensed under the                       *
64887Schin *                  Common Public License, Version 1.0                  *
78462SApril.Chin@Sun.COM *                    by AT&T Intellectual Property                     *
84887Schin *                                                                      *
94887Schin *                A copy of the License is available at                 *
104887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
114887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
124887Schin *                                                                      *
134887Schin *              Information and Software Systems Research               *
144887Schin *                            AT&T Research                             *
154887Schin *                           Florham Park NJ                            *
164887Schin *                                                                      *
174887Schin *                 Glenn Fowler <gsf@research.att.com>                  *
184887Schin *                  David Korn <dgk@research.att.com>                   *
194887Schin *                   Phong Vo <kpv@research.att.com>                    *
204887Schin *                                                                      *
214887Schin ***********************************************************************/
224887Schin #pragma prototyped
234887Schin /*
244887Schin  * strftime implementation
254887Schin  */
264887Schin 
274887Schin #define strftime	______strftime
284887Schin 
294887Schin #include <ast.h>
304887Schin #include <tm.h>
314887Schin 
324887Schin #undef	strftime
334887Schin 
344887Schin #undef	_def_map_ast
354887Schin #include <ast_map.h>
364887Schin 
374887Schin #undef	_lib_strftime	/* we can pass X/Open */
384887Schin 
394887Schin #if _lib_strftime
404887Schin 
414887Schin NoN(strftime)
424887Schin 
434887Schin #else
444887Schin 
454887Schin #if defined(__EXPORT__)
464887Schin #define extern	__EXPORT__
474887Schin #endif
484887Schin 
494887Schin extern size_t
504887Schin strftime(char* buf, size_t len, const char* format, const struct tm* tm)
514887Schin {
524887Schin 	register char*	s;
534887Schin 	time_t		t;
544887Schin 	Tm_t		tl;
554887Schin 
564887Schin 	memset(&tl, 0, sizeof(tl));
574887Schin 
584887Schin 	/*
594887Schin 	 * nl_langinfo() may call strftime() with bogus tm except for
604887Schin 	 * one value -- what a way to go
614887Schin 	 */
624887Schin 
634887Schin 	if (tm->tm_sec < 0 || tm->tm_sec > 60 ||
644887Schin 	    tm->tm_min < 0 || tm->tm_min > 59 ||
654887Schin 	    tm->tm_hour < 0 || tm->tm_hour > 23 ||
664887Schin 	    tm->tm_wday < 0 || tm->tm_wday > 6 ||
674887Schin 	    tm->tm_mday < 1 || tm->tm_mday > 31 ||
684887Schin 	    tm->tm_mon < 0 || tm->tm_mon > 11 ||
694887Schin 	    tm->tm_year < 0 || tm->tm_year > (2138 - 1900))
704887Schin 	{
714887Schin 		if (tm->tm_sec >= 0 && tm->tm_sec <= 60)
724887Schin 			tl.tm_sec = tm->tm_sec;
734887Schin 		if (tm->tm_min >= 0 && tm->tm_min <= 59)
744887Schin 			tl.tm_min = tm->tm_min;
754887Schin 		if (tm->tm_hour >= 0 && tm->tm_hour <= 23)
764887Schin 			tl.tm_hour = tm->tm_hour;
774887Schin 		if (tm->tm_wday >= 0 && tm->tm_wday <= 6)
784887Schin 			tl.tm_wday = tm->tm_wday;
794887Schin 		if (tm->tm_mday >= 0 && tm->tm_mday <= 31)
804887Schin 			tl.tm_mday = tm->tm_mday;
814887Schin 		if (tm->tm_mon >= 0 && tm->tm_mon <= 11)
824887Schin 			tl.tm_mon = tm->tm_mon;
834887Schin 		if (tm->tm_year >= 0 && tm->tm_year <= (2138 - 1900))
844887Schin 			tl.tm_year = tm->tm_year;
854887Schin 	}
864887Schin 	else
874887Schin 	{
884887Schin 		tl.tm_sec = tm->tm_sec;
894887Schin 		tl.tm_min = tm->tm_min;
904887Schin 		tl.tm_hour = tm->tm_hour;
914887Schin 		tl.tm_mday = tm->tm_mday;
924887Schin 		tl.tm_mon = tm->tm_mon;
934887Schin 		tl.tm_year = tm->tm_year;
944887Schin 		tl.tm_wday = tm->tm_wday;
954887Schin 		tl.tm_yday = tm->tm_yday;
964887Schin 		tl.tm_isdst = tm->tm_isdst;
974887Schin 	}
984887Schin 	t = tmtime(&tl, TM_LOCALZONE);
994887Schin 	if (!(s = tmfmt(buf, len, format, &t)))
1004887Schin 		return 0;
1014887Schin 	return s - buf;
1024887Schin }
1034887Schin 
1044887Schin #endif
105