xref: /titanic_44/usr/src/lib/libast/common/comp/strftime.c (revision bd670b35a010421b6e1a5536c34453a827007c81)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2009 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *            http://www.opensource.org/licenses/cpl1.0.txt             *
11 *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                 Glenn Fowler <gsf@research.att.com>                  *
18 *                  David Korn <dgk@research.att.com>                   *
19 *                   Phong Vo <kpv@research.att.com>                    *
20 *                                                                      *
21 ***********************************************************************/
22 #pragma prototyped
23 /*
24  * strftime implementation
25  */
26 
27 #define strftime	______strftime
28 
29 #include <ast.h>
30 #include <tm.h>
31 
32 #undef	strftime
33 
34 #undef	_def_map_ast
35 #include <ast_map.h>
36 
37 #undef	_lib_strftime	/* we can pass X/Open */
38 
39 #if _lib_strftime
40 
41 NoN(strftime)
42 
43 #else
44 
45 #if defined(__EXPORT__)
46 #define extern	__EXPORT__
47 #endif
48 
49 extern size_t
50 strftime(char* buf, size_t len, const char* format, const struct tm* tm)
51 {
52 	register char*	s;
53 	time_t		t;
54 	Tm_t		tl;
55 
56 	memset(&tl, 0, sizeof(tl));
57 
58 	/*
59 	 * nl_langinfo() may call strftime() with bogus tm except for
60 	 * one value -- what a way to go
61 	 */
62 
63 	if (tm->tm_sec < 0 || tm->tm_sec > 60 ||
64 	    tm->tm_min < 0 || tm->tm_min > 59 ||
65 	    tm->tm_hour < 0 || tm->tm_hour > 23 ||
66 	    tm->tm_wday < 0 || tm->tm_wday > 6 ||
67 	    tm->tm_mday < 1 || tm->tm_mday > 31 ||
68 	    tm->tm_mon < 0 || tm->tm_mon > 11 ||
69 	    tm->tm_year < 0 || tm->tm_year > (2138 - 1900))
70 	{
71 		if (tm->tm_sec >= 0 && tm->tm_sec <= 60)
72 			tl.tm_sec = tm->tm_sec;
73 		if (tm->tm_min >= 0 && tm->tm_min <= 59)
74 			tl.tm_min = tm->tm_min;
75 		if (tm->tm_hour >= 0 && tm->tm_hour <= 23)
76 			tl.tm_hour = tm->tm_hour;
77 		if (tm->tm_wday >= 0 && tm->tm_wday <= 6)
78 			tl.tm_wday = tm->tm_wday;
79 		if (tm->tm_mday >= 0 && tm->tm_mday <= 31)
80 			tl.tm_mday = tm->tm_mday;
81 		if (tm->tm_mon >= 0 && tm->tm_mon <= 11)
82 			tl.tm_mon = tm->tm_mon;
83 		if (tm->tm_year >= 0 && tm->tm_year <= (2138 - 1900))
84 			tl.tm_year = tm->tm_year;
85 	}
86 	else
87 	{
88 		tl.tm_sec = tm->tm_sec;
89 		tl.tm_min = tm->tm_min;
90 		tl.tm_hour = tm->tm_hour;
91 		tl.tm_mday = tm->tm_mday;
92 		tl.tm_mon = tm->tm_mon;
93 		tl.tm_year = tm->tm_year;
94 		tl.tm_wday = tm->tm_wday;
95 		tl.tm_yday = tm->tm_yday;
96 		tl.tm_isdst = tm->tm_isdst;
97 	}
98 	t = tmtime(&tl, TM_LOCALZONE);
99 	if (!(s = tmfmt(buf, len, format, &t)))
100 		return 0;
101 	return s - buf;
102 }
103 
104 #endif
105