1 /*
2  * Copyright (c) 1983 Eric P. Allman
3  * Copyright (c) 1988 Regents of the University of California.
4  * All rights reserved.
5  *
6  * %sccs.include.redist.c%
7  */
8 
9 #ifndef lint
10 static char sccsid[] = "@(#)arpadate.c	5.12 (Berkeley) 08/09/92";
11 #endif /* not lint */
12 
13 # include "conf.h"
14 # include <time.h>
15 # include <sys/types.h>
16 # include "useful.h"
17 
18 /*
19 **  ARPADATE -- Create date in ARPANET format
20 **
21 **	Parameters:
22 **		ud -- unix style date string.  if NULL, one is created.
23 **
24 **	Returns:
25 **		pointer to an ARPANET date field
26 **
27 **	Side Effects:
28 **		none
29 **
30 **	WARNING:
31 **		date is stored in a local buffer -- subsequent
32 **		calls will overwrite.
33 **
34 **	Bugs:
35 **		Timezone is computed from local time, rather than
36 **		from whereever (and whenever) the message was sent.
37 **		To do better is very hard.
38 **
39 **		Some sites are now inserting the timezone into the
40 **		local date.  This routine should figure out what
41 **		the format is and work appropriately.
42 */
43 
44 char *
45 arpadate(ud)
46 	register char *ud;
47 {
48 	register char *p;
49 	register char *q;
50 	register int off;
51 	register int i;
52 	register struct tm *lt;
53 	time_t t;
54 	struct tm gmt;
55 	static char b[40];
56 	extern struct tm *localtime(), *gmtime();
57 	extern char *ctime();
58 	extern time_t time();
59 
60 	/*
61 	**  Get current time.
62 	**	This will be used if a null argument is passed and
63 	**	to resolve the timezone.
64 	*/
65 
66 	(void) time(&t);
67 	if (ud == NULL)
68 		ud = ctime(&t);
69 
70 	/*
71 	**  Crack the UNIX date line in a singularly unoriginal way.
72 	*/
73 
74 	q = b;
75 
76 	p = &ud[0];		/* Mon */
77 	*q++ = *p++;
78 	*q++ = *p++;
79 	*q++ = *p++;
80 	*q++ = ',';
81 	*q++ = ' ';
82 
83 	p = &ud[8];		/* 16 */
84 	if (*p == ' ')
85 		p++;
86 	else
87 		*q++ = *p++;
88 	*q++ = *p++;
89 	*q++ = ' ';
90 
91 	p = &ud[4];		/* Sep */
92 	*q++ = *p++;
93 	*q++ = *p++;
94 	*q++ = *p++;
95 	*q++ = ' ';
96 
97 	p = &ud[22];		/* 1979 */
98 	if (*p >= '6')
99 	{
100 		*q++ = '1';
101 		*q++ = '9';
102 	}
103 	else
104 	{
105 		*q++ = '2';
106 		*q++ = '0';
107 	}
108 	*q++ = *p++;
109 	*q++ = *p++;
110 	*q++ = ' ';
111 
112 	p = &ud[11];		/* 01:03:52 */
113 	for (i = 8; i > 0; i--)
114 		*q++ = *p++;
115 
116 	/*
117 	 * should really get the timezone from the time in "ud" (which
118 	 * is only different if a non-null arg was passed which is different
119 	 * from the current time), but for all practical purposes, returning
120 	 * the current local zone will do (its all that is ever needed).
121 	 */
122 	gmt = *gmtime(&t);
123 	lt = localtime(&t);
124 
125 	off = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min;
126 
127 	/* assume that offset isn't more than a day ... */
128 	if (lt->tm_year < gmt.tm_year)
129 		off -= 24 * 60;
130 	else if (lt->tm_year > gmt.tm_year)
131 		off += 24 * 60;
132 	else if (lt->tm_yday < gmt.tm_yday)
133 		off -= 24 * 60;
134 	else if (lt->tm_yday > gmt.tm_yday)
135 		off += 24 * 60;
136 
137 	*q++ = ' ';
138 	if (off == 0) {
139 		*q++ = 'G';
140 		*q++ = 'M';
141 		*q++ = 'T';
142 	} else {
143 		if (off < 0) {
144 			off = -off;
145 			*q++ = '-';
146 		} else
147 			*q++ = '+';
148 
149 		if (off >= 24*60)		/* should be impossible */
150 			off = 23*60+59;		/* if not, insert silly value */
151 
152 		*q++ = (off / 600) + '0';
153 		*q++ = (off / 60) % 10 + '0';
154 		off %= 60;
155 		*q++ = (off / 10) + '0';
156 		*q++ = (off % 10) + '0';
157 	}
158 	*q = '\0';
159 
160 	return (b);
161 }
162