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	3.12		07/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 # ifdef V6
48 	long t;
49 	extern char *StdTimezone, *DstTimezone;
50 	extern long time();
51 # else
52 	struct timeb t;
53 	extern struct timeb *ftime();
54 	extern char *timezone();
55 # endif
56 
57 	/*
58 	**  Get current time.
59 	**	This will be used if a null argument is passed and
60 	**	to resolve the timezone.
61 	*/
62 
63 # ifdef V6
64 	(void) time(&t);
65 	if (ud == NULL)
66 		ud = ctime(&t);
67 # else
68 	ftime(&t);
69 	if (ud == NULL)
70 		ud = ctime(&t.time);
71 # endif
72 
73 	/*
74 	**  Crack the UNIX date line in a singularly unoriginal way.
75 	*/
76 
77 	q = b;
78 
79 	p = &ud[8];		/* 16 */
80 	if (*p == ' ')
81 		p++;
82 	else
83 		*q++ = *p++;
84 	*q++ = *p++;
85 	*q++ = ' ';
86 
87 	p = &ud[4];		/* Sep */
88 	*q++ = *p++;
89 	*q++ = *p++;
90 	*q++ = *p++;
91 	*q++ = ' ';
92 
93 	p = &ud[22];		/* 1979 */
94 	*q++ = *p++;
95 	*q++ = *p++;
96 	*q++ = ' ';
97 
98 	p = &ud[11];		/* 01:03:52 */
99 	for (i = 8; i > 0; i--)
100 		*q++ = *p++;
101 
102 				/* -PST or -PDT */
103 # ifdef V6
104 	if (localtime(&t)->tm_isdst)
105 		p = DstTimezone;
106 	else
107 		p = StdTimezone;
108 # else
109 	p = timezone(t.timezone, localtime(&t.time)->tm_isdst);
110 # endif V6
111 	if (p[3] != '\0')
112 	{
113 		/* hours from GMT */
114 		p += 3;
115 		*q++ = *p++;
116 		if (p[1] == ':')
117 			*q++ = '0';
118 		else
119 			*q++ = *p++;
120 		*q++ = *p++;
121 		p++;		/* skip ``:'' */
122 		*q++ = *p++;
123 		*q++ = *p++;
124 	}
125 	else
126 	{
127 		*q++ = ' ';
128 		*q++ = *p++;
129 		*q++ = *p++;
130 		*q++ = *p++;
131 	}
132 
133 	p = &ud[0];		/* Mon */
134 	*q++ = ' ';
135 	*q++ = '(';
136 	*q++ = *p++;
137 	*q++ = *p++;
138 	*q++ = *p++;
139 	*q++ = ')';
140 
141 	*q = '\0';
142 	return (b);
143 }
144