1 # include <time.h>
2 # ifndef V6
3 # include <sys/types.h>
4 # include <sys/timeb.h>
5 # endif
6 # include "useful.h"
7 
8 SCCSID(@(#)arpadate.c	3.9		12/06/81);
9 
10 /*
11 **  ARPADATE -- Create date in ARPANET format
12 **
13 **	Parameters:
14 **		ud -- unix style date string.  if NULL, one is created.
15 **
16 **	Returns:
17 **		pointer to an ARPANET date field
18 **
19 **	Side Effects:
20 **		none
21 **
22 **	WARNING:
23 **		date is stored in a local buffer -- subsequent
24 **		calls will overwrite.
25 **
26 **	Bugs:
27 **		Timezone is computed from local time, rather than
28 **		from whereever (and whenever) the message was sent.
29 **		To do better is very hard.
30 **
31 **		Some sites are now inserting the timezone into the
32 **		local date.  This routine should figure out what
33 **		the format is and work appropriately.
34 */
35 
36 char *
37 arpadate(ud)
38 	register char *ud;
39 {
40 	register char *p;
41 	register char *q;
42 	static char b[40];
43 	extern char *ctime();
44 	register int i;
45 	extern struct tm *localtime();
46 # ifdef V6
47 	long t;
48 	extern char *StdTimezone, *DstTimezone;
49 	extern long time();
50 # else
51 	struct timeb t;
52 	extern struct timeb *ftime();
53 	extern char *timezone();
54 # endif
55 
56 	/*
57 	**  Get current time.
58 	**	This will be used if a null argument is passed and
59 	**	to resolve the timezone.
60 	*/
61 
62 # ifdef V6
63 	(void) time(&t);
64 	if (ud == NULL)
65 		ud = ctime(&t);
66 # else
67 	ftime(&t);
68 	if (ud == NULL)
69 		ud = ctime(&t.time);
70 # endif
71 
72 	/*
73 	**  Crack the UNIX date line in a singularly unoriginal way.
74 	*/
75 
76 	q = b;
77 
78 	p = &ud[8];		/* 16 */
79 	if (*p == ' ')
80 		p++;
81 	else
82 		*q++ = *p++;
83 	*q++ = *p++;
84 	*q++ = '-';
85 
86 	p = &ud[4];		/* Sep */
87 	*q++ = *p++;
88 	*q++ = *p++;
89 	*q++ = *p++;
90 	*q++ = '-';
91 
92 	p = &ud[22];		/* 1979 */
93 	*q++ = *p++;
94 	*q++ = *p++;
95 	*q++ = ' ';
96 
97 	p = &ud[11];		/* 01:03:52 */
98 	for (i = 8; i > 0; i--)
99 		*q++ = *p++;
100 
101 				/* -PST or -PDT */
102 # ifdef V6
103 	if (localtime(&t)->tm_isdst)
104 		p = DstTimezone;
105 	else
106 		p = StdTimezone;
107 # else
108 	p = timezone(t.timezone, localtime(&t.time)->tm_isdst);
109 # endif V6
110 	if (p[3] != '\0')
111 	{
112 		/* hours from GMT */
113 		p += 3;
114 		*q++ = *p++;
115 		if (p[1] == ':')
116 			*q++ = '0';
117 		else
118 			*q++ = *p++;
119 		*q++ = *p++;
120 		p++;		/* skip ``:'' */
121 		*q++ = *p++;
122 		*q++ = *p++;
123 	}
124 	else
125 	{
126 		*q++ = '-';
127 		*q++ = *p++;
128 		*q++ = *p++;
129 		*q++ = *p++;
130 	}
131 
132 	p = &ud[0];		/* Mon */
133 	*q++ = ' ';
134 	*q++ = '(';
135 	*q++ = *p++;
136 	*q++ = *p++;
137 	*q++ = *p++;
138 	*q++ = ')';
139 
140 	*q = '\0';
141 	return (b);
142 }
143