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