1 # include "conf.h"
2 # include <sys/time.h>
3 # ifndef V6
4 # include <sys/types.h>
5 # include <sys/timeb.h>
6 # endif
7 # include "useful.h"
8 
9 SCCSID(@(#)arpadate.c	4.3		10/01/83);
10 
11 /*
12 **  ARPADATE -- Create date in ARPANET format
13 **
14 **	Parameters:
15 **		ud -- unix style date string.  if NULL, one is created.
16 **
17 **	Returns:
18 **		pointer to an ARPANET date field
19 **
20 **	Side Effects:
21 **		none
22 **
23 **	WARNING:
24 **		date is stored in a local buffer -- subsequent
25 **		calls will overwrite.
26 **
27 **	Bugs:
28 **		Timezone is computed from local time, rather than
29 **		from whereever (and whenever) the message was sent.
30 **		To do better is very hard.
31 **
32 **		Some sites are now inserting the timezone into the
33 **		local date.  This routine should figure out what
34 **		the format is and work appropriately.
35 */
36 
37 char *
38 arpadate(ud)
39 	register char *ud;
40 {
41 	register char *p;
42 	register char *q;
43 	static char b[40];
44 	extern char *ctime();
45 	register int i;
46 	extern struct tm *localtime();
47 	extern bool fconvert();
48 # ifdef V6
49 	long t;
50 	extern char *StdTimezone, *DstTimezone;
51 	extern long time();
52 # else
53 	struct timeb t;
54 	extern struct timeb *ftime();
55 	extern char *timezone();
56 # endif
57 
58 	/*
59 	**  Get current time.
60 	**	This will be used if a null argument is passed and
61 	**	to resolve the timezone.
62 	*/
63 
64 # ifdef V6
65 	(void) time(&t);
66 	if (ud == NULL)
67 		ud = ctime(&t);
68 # else
69 	ftime(&t);
70 	if (ud == NULL)
71 		ud = ctime(&t.time);
72 # endif
73 
74 	/*
75 	**  Crack the UNIX date line in a singularly unoriginal way.
76 	*/
77 
78 	q = b;
79 
80 	p = &ud[0];		/* Mon */
81 	*q++ = *p++;
82 	*q++ = *p++;
83 	*q++ = *p++;
84 	*q++ = ',';
85 	*q++ = ' ';
86 
87 	p = &ud[8];		/* 16 */
88 	if (*p == ' ')
89 		p++;
90 	else
91 		*q++ = *p++;
92 	*q++ = *p++;
93 	*q++ = ' ';
94 
95 	p = &ud[4];		/* Sep */
96 	*q++ = *p++;
97 	*q++ = *p++;
98 	*q++ = *p++;
99 	*q++ = ' ';
100 
101 	p = &ud[22];		/* 79 */
102 	*q++ = *p++;
103 	*q++ = *p++;
104 	*q++ = ' ';
105 
106 	p = &ud[11];		/* 01:03:52 */
107 	for (i = 8; i > 0; i--)
108 		*q++ = *p++;
109 
110 				/* -PST or -PDT */
111 # ifdef V6
112 	if (localtime(&t)->tm_isdst)
113 		p = DstTimezone;
114 	else
115 		p = StdTimezone;
116 # else
117 	p = timezone(t.timezone, localtime(&t.time)->tm_isdst);
118 # endif V6
119 	if ((strncmp(p, "GMT", 3) == 0 || strncmp(p, "gmt", 3) == 0) &&
120 	    p[3] != '\0')
121 	{
122 		/* hours from GMT */
123 		p += 3;
124 		*q++ = *p++;
125 		if (p[1] == ':')
126 			*q++ = '0';
127 		else
128 			*q++ = *p++;
129 		*q++ = *p++;
130 		p++;		/* skip ``:'' */
131 		*q++ = *p++;
132 		*q++ = *p++;
133 		*q = '\0';
134 	}
135 	else if (!fconvert(p, q))
136 	{
137 		*q++ = ' ';
138 		*q++ = *p++;
139 		*q++ = *p++;
140 		*q++ = *p++;
141 		*q = '\0';
142 	}
143 
144 	return (b);
145 }
146 /*
147 **  FCONVERT -- convert foreign timezones to ARPA timezones
148 **
149 **	This routine is essentially from Teus Hagen.
150 **
151 **	Parameters:
152 **		a -- timezone as returned from UNIX.
153 **		b -- place to put ARPA-style timezone.
154 **
155 **	Returns:
156 **		TRUE -- if a conversion was made (and b was filled in).
157 **		FALSE -- if this is not a recognized local time.
158 **
159 **	Side Effects:
160 **		none.
161 */
162 
163 /* UNIX to arpa conversion table */
164 struct foreign
165 {
166 	char *f_from;
167 	char *f_to;
168 };
169 
170 static struct foreign Foreign[] =
171 {
172 	{ "eet",	" -0200" },	/* eastern europe */
173 	{ "met",	" -0100" },	/* middle europe */
174 	{ "wet",	" GMT"   },	/* western europe */
175 	{ "eet dst",	" -0300" },	/* daylight saving times */
176 	{ "met dst",	" -0200" },
177 	{ "wet dst",	" -0100" },
178 	{ NULL,		NULL	 }
179 };
180 
181 bool
182 fconvert(a, b)
183 	register char *a;
184 	char *b;
185 {
186 	register struct foreign *euptr;
187 	register char *p;
188 
189 	makelower(a);
190 	for (euptr = Foreign; euptr->f_from != NULL; euptr++)
191 	{
192 		if (strcmp(euptr->f_from, a) == 0)
193 		{
194 			p = euptr->f_to;
195 			while (*p)
196 				*b++ = *p++;
197 			*b = '\0';
198 			return (TRUE);
199 		}
200 	}
201 	return (FALSE);
202 }
203