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.11 (Berkeley) 06/01/90";
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];		/* 79 */
98 	*q++ = *p++;
99 	*q++ = *p++;
100 	*q++ = ' ';
101 
102 	p = &ud[11];		/* 01:03:52 */
103 	for (i = 8; i > 0; i--)
104 		*q++ = *p++;
105 
106 	/*
107 	 * should really get the timezone from the time in "ud" (which
108 	 * is only different if a non-null arg was passed which is different
109 	 * from the current time), but for all practical purposes, returning
110 	 * the current local zone will do (its all that is ever needed).
111 	 */
112 	gmt = *gmtime(&t);
113 	lt = localtime(&t);
114 
115 	off = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min;
116 
117 	/* assume that offset isn't more than a day ... */
118 	if (lt->tm_year < gmt.tm_year)
119 		off -= 24 * 60;
120 	else if (lt->tm_year > gmt.tm_year)
121 		off += 24 * 60;
122 	else if (lt->tm_yday < gmt.tm_yday)
123 		off -= 24 * 60;
124 	else if (lt->tm_yday > gmt.tm_yday)
125 		off += 24 * 60;
126 
127 	*q++ = ' ';
128 	if (off == 0) {
129 		*q++ = 'G';
130 		*q++ = 'M';
131 		*q++ = 'T';
132 	} else {
133 		if (off < 0) {
134 			off = -off;
135 			*q++ = '-';
136 		} else
137 			*q++ = '+';
138 
139 		if (off >= 24*60)		/* should be impossible */
140 			off = 23*60+59;		/* if not, insert silly value */
141 
142 		*q++ = (off / 600) + '0';
143 		*q++ = (off / 60) % 10 + '0';
144 		off %= 60;
145 		*q++ = (off / 10) + '0';
146 		*q++ = (off % 10) + '0';
147 	}
148 	*q = '\0';
149 
150 	return (b);
151 }
152