asctime.c revision 225736
1170525Syongari/*
2170525Syongari** This file is in the public domain, so clarified as of
3170525Syongari** 1996-06-05 by Arthur David Olson.
4170525Syongari*/
5170525Syongari
6170525Syongari/*
7170525Syongari** Avoid the temptation to punt entirely to strftime;
8170525Syongari** the output of strftime is supposed to be locale specific
9170525Syongari** whereas the output of asctime is supposed to be constant.
10170525Syongari*/
11170525Syongari
12170525Syongari#include <sys/cdefs.h>
13170525Syongari#ifndef lint
14170525Syongari#ifndef NOID
15170525Syongaristatic char	elsieid[] __unused = "@(#)asctime.c	8.5";
16170525Syongari#endif /* !defined NOID */
17170525Syongari#endif /* !defined lint */
18170525Syongari__FBSDID("$FreeBSD: stable/9/contrib/tzcode/stdtime/asctime.c 214411 2010-10-27 07:14:46Z edwin $");
19170525Syongari
20170525Syongari/*LINTLIBRARY*/
21170525Syongari
22170525Syongari#include "namespace.h"
23170525Syongari#include "private.h"
24170525Syongari#include "un-namespace.h"
25170525Syongari#include "tzfile.h"
26170525Syongari
27170525Syongari/*
28170525Syongari** Some systems only handle "%.2d"; others only handle "%02d";
29170525Syongari** "%02.2d" makes (most) everybody happy.
30170525Syongari** At least some versions of gcc warn about the %02.2d;
31170525Syongari** we conditionalize below to avoid the warning.
32170525Syongari*/
33170525Syongari/*
34170525Syongari** All years associated with 32-bit time_t values are exactly four digits long;
35170525Syongari** some years associated with 64-bit time_t values are not.
36170525Syongari** Vintage programs are coded for years that are always four digits long
37170525Syongari** and may assume that the newline always lands in the same place.
38170525Syongari** For years that are less than four digits, we pad the output with
39170525Syongari** leading zeroes to get the newline in the traditional place.
40170525Syongari** The -4 ensures that we get four characters of output even if
41170525Syongari** we call a strftime variant that produces fewer characters for some years.
42170525Syongari** The ISO C 1999 and POSIX 1003.1-2004 standards prohibit padding the year,
43170525Syongari** but many implementations pad anyway; most likely the standards are buggy.
44170525Syongari*/
45170525Syongari#ifdef __GNUC__
46170525Syongari#define ASCTIME_FMT	"%.3s %.3s%3d %2.2d:%2.2d:%2.2d %-4s\n"
47170525Syongari#else /* !defined __GNUC__ */
48170525Syongari#define ASCTIME_FMT	"%.3s %.3s%3d %02.2d:%02.2d:%02.2d %-4s\n"
49170525Syongari#endif /* !defined __GNUC__ */
50170525Syongari/*
51170525Syongari** For years that are more than four digits we put extra spaces before the year
52170525Syongari** so that code trying to overwrite the newline won't end up overwriting
53170525Syongari** a digit within a year and truncating the year (operating on the assumption
54170525Syongari** that no output is better than wrong output).
55170525Syongari*/
56170525Syongari#ifdef __GNUC__
57170525Syongari#define ASCTIME_FMT_B	"%.3s %.3s%3d %2.2d:%2.2d:%2.2d     %s\n"
58170525Syongari#else /* !defined __GNUC__ */
59170525Syongari#define ASCTIME_FMT_B	"%.3s %.3s%3d %02.2d:%02.2d:%02.2d     %s\n"
60170525Syongari#endif /* !defined __GNUC__ */
61170525Syongari
62170525Syongari#define STD_ASCTIME_BUF_SIZE	26
63170525Syongari/*
64170525Syongari** Big enough for something such as
65170525Syongari** ??? ???-2147483648 -2147483648:-2147483648:-2147483648     -2147483648\n
66170525Syongari** (two three-character abbreviations, five strings denoting integers,
67170525Syongari** seven explicit spaces, two explicit colons, a newline,
68170525Syongari** and a trailing ASCII nul).
69170525Syongari** The values above are for systems where an int is 32 bits and are provided
70170525Syongari** as an example; the define below calculates the maximum for the system at
71170525Syongari** hand.
72170525Syongari*/
73170525Syongari#define MAX_ASCTIME_BUF_SIZE	(2*3+5*INT_STRLEN_MAXIMUM(int)+7+2+1+1)
74170525Syongari
75170525Syongaristatic char	buf_asctime[MAX_ASCTIME_BUF_SIZE];
76170525Syongari
77170525Syongari/*
78170525Syongari** A la ISO/IEC 9945-1, ANSI/IEEE Std 1003.1, 2004 Edition.
79170525Syongari*/
80170525Syongari
81170525Syongarichar *
82170525Syongariasctime_r(timeptr, buf)
83170525Syongariconst struct tm *	timeptr;
84170525Syongarichar *			buf;
85170525Syongari{
86170525Syongari	static const char	wday_name[][3] = {
87170525Syongari		"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
88170525Syongari	};
89170525Syongari	static const char	mon_name[][3] = {
90170525Syongari		"Jan", "Feb", "Mar", "Apr", "May", "Jun",
91170525Syongari		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
92227908Smarius	};
93170525Syongari	const char *	wn;
94170525Syongari	const char *	mn;
95170525Syongari	char			year[INT_STRLEN_MAXIMUM(int) + 2];
96170525Syongari	char			result[MAX_ASCTIME_BUF_SIZE];
97170525Syongari
98170525Syongari	if (timeptr == NULL) {
99170525Syongari		errno = EINVAL;
100221407Smarius		return strcpy(buf, "??? ??? ?? ??:??:?? ????\n");
101170525Syongari	}
102170525Syongari	if (timeptr->tm_wday < 0 || timeptr->tm_wday >= DAYSPERWEEK)
103170525Syongari		wn = "???";
104170525Syongari	else	wn = wday_name[timeptr->tm_wday];
105170525Syongari	if (timeptr->tm_mon < 0 || timeptr->tm_mon >= MONSPERYEAR)
106170525Syongari		mn = "???";
107170525Syongari	else	mn = mon_name[timeptr->tm_mon];
108170525Syongari	/*
109170525Syongari	** Use strftime's %Y to generate the year, to avoid overflow problems
110221407Smarius	** when computing timeptr->tm_year + TM_YEAR_BASE.
111221407Smarius	** Assume that strftime is unaffected by other out-of-range members
112221407Smarius	** (e.g., timeptr->tm_mday) when processing "%Y".
113221407Smarius	*/
114170525Syongari	(void) strftime(year, sizeof year, "%Y", timeptr);
115170525Syongari	/*
116170525Syongari	** We avoid using snprintf since it's not available on all systems.
117221407Smarius	*/
118221407Smarius	(void) sprintf(result,
119221407Smarius		((strlen(year) <= 4) ? ASCTIME_FMT : ASCTIME_FMT_B),
120221407Smarius		wn, mn,
121221407Smarius		timeptr->tm_mday, timeptr->tm_hour,
122221407Smarius		timeptr->tm_min, timeptr->tm_sec,
123170525Syongari		year);
124170525Syongari	if (strlen(result) < STD_ASCTIME_BUF_SIZE || buf == buf_asctime)
125170525Syongari		return strcpy(buf, result);
126170525Syongari	else {
127170525Syongari#ifdef EOVERFLOW
128170525Syongari		errno = EOVERFLOW;
129170525Syongari#else /* !defined EOVERFLOW */
130170525Syongari		errno = EINVAL;
131170525Syongari#endif /* !defined EOVERFLOW */
132170525Syongari		return NULL;
133170525Syongari	}
134221407Smarius}
135221407Smarius
136170525Syongari/*
137170525Syongari** A la ISO/IEC 9945-1, ANSI/IEEE Std 1003.1, 2004 Edition.
138170525Syongari*/
139170525Syongari
140170525Syongarichar *
141170525Syongariasctime(timeptr)
142170525Syongariconst struct tm *	timeptr;
143170525Syongari{
144170525Syongari	return asctime_r(timeptr, buf_asctime);
145170525Syongari}
146170525Syongari