arpadate.c revision 64562
1/*
2 * Copyright (c) 1998, 1999 Sendmail, Inc. and its suppliers.
3 *	All rights reserved.
4 * Copyright (c) 1983, 1995-1997 Eric P. Allman.  All rights reserved.
5 * Copyright (c) 1988, 1993
6 *	The Regents of the University of California.  All rights reserved.
7 *
8 * By using this file, you agree to the terms and conditions set
9 * forth in the LICENSE file which can be found at the top level of
10 * the sendmail distribution.
11 *
12 */
13
14#ifndef lint
15static char id[] = "@(#)$Id: arpadate.c,v 8.23 1999/09/23 19:59:18 ca Exp $";
16#endif /* ! lint */
17
18#include <sendmail.h>
19
20/*
21**  ARPADATE -- Create date in ARPANET format
22**
23**	Parameters:
24**		ud -- unix style date string.  if NULL, one is created.
25**
26**	Returns:
27**		pointer to an ARPANET date field
28**
29**	Side Effects:
30**		none
31**
32**	WARNING:
33**		date is stored in a local buffer -- subsequent
34**		calls will overwrite.
35**
36**	Bugs:
37**		Timezone is computed from local time, rather than
38**		from wherever (and whenever) the message was sent.
39**		To do better is very hard.
40**
41**		Some sites are now inserting the timezone into the
42**		local date.  This routine should figure out what
43**		the format is and work appropriately.
44*/
45
46#ifndef TZNAME_MAX
47# define TZNAME_MAX	50	/* max size of timezone */
48#endif /* ! TZNAME_MAX */
49
50/* values for TZ_TYPE */
51#define TZ_NONE		0	/* no character timezone support */
52#define TZ_TM_NAME	1	/* use tm->tm_name */
53#define TZ_TM_ZONE	2	/* use tm->tm_zone */
54#define TZ_TZNAME	3	/* use tzname[] */
55#define TZ_TIMEZONE	4	/* use timezone() */
56
57char *
58arpadate(ud)
59	register char *ud;
60{
61	register char *p;
62	register char *q;
63	register int off;
64	register int i;
65	register struct tm *lt;
66	time_t t;
67	struct tm gmt;
68	char *tz;
69	static char b[43 + TZNAME_MAX];
70
71	/*
72	**  Get current time.
73	**	This will be used if a null argument is passed and
74	**	to resolve the timezone.
75	*/
76
77	t = curtime();
78	if (ud == NULL)
79		ud = ctime(&t);
80
81	/*
82	**  Crack the UNIX date line in a singularly unoriginal way.
83	*/
84
85	q = b;
86
87	p = &ud[0];		/* Mon */
88	*q++ = *p++;
89	*q++ = *p++;
90	*q++ = *p++;
91	*q++ = ',';
92	*q++ = ' ';
93
94	p = &ud[8];		/* 16 */
95	if (*p == ' ')
96		p++;
97	else
98		*q++ = *p++;
99	*q++ = *p++;
100	*q++ = ' ';
101
102	p = &ud[4];		/* Sep */
103	*q++ = *p++;
104	*q++ = *p++;
105	*q++ = *p++;
106	*q++ = ' ';
107
108	p = &ud[20];		/* 1979 */
109	*q++ = *p++;
110	*q++ = *p++;
111	*q++ = *p++;
112	*q++ = *p++;
113	*q++ = ' ';
114
115	p = &ud[11];		/* 01:03:52 */
116	for (i = 8; i > 0; i--)
117		*q++ = *p++;
118
119	/*
120	 * should really get the timezone from the time in "ud" (which
121	 * is only different if a non-null arg was passed which is different
122	 * from the current time), but for all practical purposes, returning
123	 * the current local zone will do (its all that is ever needed).
124	 */
125	gmt = *gmtime(&t);
126	lt = localtime(&t);
127
128	off = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min;
129
130	/* assume that offset isn't more than a day ... */
131	if (lt->tm_year < gmt.tm_year)
132		off -= 24 * 60;
133	else if (lt->tm_year > gmt.tm_year)
134		off += 24 * 60;
135	else if (lt->tm_yday < gmt.tm_yday)
136		off -= 24 * 60;
137	else if (lt->tm_yday > gmt.tm_yday)
138		off += 24 * 60;
139
140	*q++ = ' ';
141	if (off == 0)
142	{
143		*q++ = 'G';
144		*q++ = 'M';
145		*q++ = 'T';
146	}
147	else
148	{
149		tz = NULL;
150#if TZ_TYPE == TZ_TM_NAME
151		tz = lt->tm_name;
152#endif /* TZ_TYPE == TZ_TM_NAME */
153#if TZ_TYPE == TZ_TM_ZONE
154		tz = lt->tm_zone;
155#endif /* TZ_TYPE == TZ_TM_ZONE */
156#if TZ_TYPE == TZ_TZNAME
157		{
158			extern char *tzname[];
159
160			if (lt->tm_isdst > 0)
161				tz = tzname[1];
162			else if (lt->tm_isdst == 0)
163				tz = tzname[0];
164			else
165				tz = NULL;
166		}
167#endif /* TZ_TYPE == TZ_TZNAME */
168#if TZ_TYPE == TZ_TIMEZONE
169		{
170			extern char *timezone();
171
172			tz = timezone(off, lt->tm_isdst);
173		}
174#endif /* TZ_TYPE == TZ_TIMEZONE */
175		if (off < 0)
176		{
177			off = -off;
178			*q++ = '-';
179		}
180		else
181			*q++ = '+';
182
183		if (off >= 24*60)		/* should be impossible */
184			off = 23*60+59;		/* if not, insert silly value */
185
186		*q++ = (off / 600) + '0';
187		*q++ = (off / 60) % 10 + '0';
188		off %= 60;
189		*q++ = (off / 10) + '0';
190		*q++ = (off % 10) + '0';
191		if (tz != NULL && *tz != '\0')
192		{
193			*q++ = ' ';
194			*q++ = '(';
195			while (*tz != '\0' && q < &b[sizeof b - 3])
196				*q++ = *tz++;
197			*q++ = ')';
198		}
199	}
200	*q = '\0';
201
202	return b;
203}
204