Commit | Line | Data |
---|---|---|
d185cb11 | 1 | /* |
dc45ba8c | 2 | * Copyright (c) 1983 Eric P. Allman |
bee79b64 KB |
3 | * Copyright (c) 1988 Regents of the University of California. |
4 | * All rights reserved. | |
5 | * | |
af359dea C |
6 | * Redistribution and use in source and binary forms, with or without |
7 | * modification, are permitted provided that the following conditions | |
8 | * are met: | |
9 | * 1. Redistributions of source code must retain the above copyright | |
10 | * notice, this list of conditions and the following disclaimer. | |
11 | * 2. Redistributions in binary form must reproduce the above copyright | |
12 | * notice, this list of conditions and the following disclaimer in the | |
13 | * documentation and/or other materials provided with the distribution. | |
14 | * 3. All advertising materials mentioning features or use of this software | |
15 | * must display the following acknowledgement: | |
16 | * This product includes software developed by the University of | |
17 | * California, Berkeley and its contributors. | |
18 | * 4. Neither the name of the University nor the names of its contributors | |
19 | * may be used to endorse or promote products derived from this software | |
20 | * without specific prior written permission. | |
21 | * | |
22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
32 | * SUCH DAMAGE. | |
bee79b64 | 33 | */ |
d185cb11 DF |
34 | |
35 | #ifndef lint | |
1c15e888 | 36 | static char sccsid[] = "@(#)arpadate.c 5.11 (Berkeley) 6/1/90"; |
bee79b64 | 37 | #endif /* not lint */ |
d185cb11 | 38 | |
b5fd168f | 39 | # include "conf.h" |
34fe0a9b | 40 | # include <time.h> |
1a12c7d6 | 41 | # include <sys/types.h> |
b14547d5 | 42 | # include "useful.h" |
b3cbe40f EA |
43 | |
44 | /* | |
45 | ** ARPADATE -- Create date in ARPANET format | |
46 | ** | |
47 | ** Parameters: | |
1a12c7d6 | 48 | ** ud -- unix style date string. if NULL, one is created. |
b3cbe40f EA |
49 | ** |
50 | ** Returns: | |
51 | ** pointer to an ARPANET date field | |
52 | ** | |
53 | ** Side Effects: | |
54 | ** none | |
55 | ** | |
56 | ** WARNING: | |
57 | ** date is stored in a local buffer -- subsequent | |
58 | ** calls will overwrite. | |
9e3c0a28 EA |
59 | ** |
60 | ** Bugs: | |
61 | ** Timezone is computed from local time, rather than | |
62 | ** from whereever (and whenever) the message was sent. | |
63 | ** To do better is very hard. | |
74c5fe7c EA |
64 | ** |
65 | ** Some sites are now inserting the timezone into the | |
66 | ** local date. This routine should figure out what | |
67 | ** the format is and work appropriately. | |
b3cbe40f EA |
68 | */ |
69 | ||
5c3f609e | 70 | char * |
1a12c7d6 EA |
71 | arpadate(ud) |
72 | register char *ud; | |
b3cbe40f | 73 | { |
b3cbe40f | 74 | register char *p; |
1a12c7d6 | 75 | register char *q; |
91dec0d2 KB |
76 | register int off; |
77 | register int i; | |
78 | register struct tm *lt; | |
79 | time_t t; | |
80 | struct tm gmt; | |
b3cbe40f | 81 | static char b[40]; |
91dec0d2 | 82 | extern struct tm *localtime(), *gmtime(); |
5c3f609e | 83 | extern char *ctime(); |
91dec0d2 | 84 | extern time_t time(); |
b3cbe40f | 85 | |
74c5fe7c EA |
86 | /* |
87 | ** Get current time. | |
88 | ** This will be used if a null argument is passed and | |
89 | ** to resolve the timezone. | |
90 | */ | |
91 | ||
74c5fe7c | 92 | (void) time(&t); |
1a12c7d6 EA |
93 | if (ud == NULL) |
94 | ud = ctime(&t); | |
1a12c7d6 | 95 | |
74c5fe7c EA |
96 | /* |
97 | ** Crack the UNIX date line in a singularly unoriginal way. | |
98 | */ | |
99 | ||
1a12c7d6 EA |
100 | q = b; |
101 | ||
4bac339c EA |
102 | p = &ud[0]; /* Mon */ |
103 | *q++ = *p++; | |
104 | *q++ = *p++; | |
105 | *q++ = *p++; | |
106 | *q++ = ','; | |
107 | *q++ = ' '; | |
108 | ||
b3cbe40f EA |
109 | p = &ud[8]; /* 16 */ |
110 | if (*p == ' ') | |
111 | p++; | |
1a12c7d6 EA |
112 | else |
113 | *q++ = *p++; | |
114 | *q++ = *p++; | |
1d2ff212 | 115 | *q++ = ' '; |
1a12c7d6 | 116 | |
d87a0dbb EA |
117 | p = &ud[4]; /* Sep */ |
118 | *q++ = *p++; | |
119 | *q++ = *p++; | |
120 | *q++ = *p++; | |
1d2ff212 | 121 | *q++ = ' '; |
1a12c7d6 | 122 | |
4bac339c | 123 | p = &ud[22]; /* 79 */ |
d87a0dbb EA |
124 | *q++ = *p++; |
125 | *q++ = *p++; | |
1a12c7d6 EA |
126 | *q++ = ' '; |
127 | ||
128 | p = &ud[11]; /* 01:03:52 */ | |
d87a0dbb | 129 | for (i = 8; i > 0; i--) |
1a12c7d6 EA |
130 | *q++ = *p++; |
131 | ||
91dec0d2 KB |
132 | /* |
133 | * should really get the timezone from the time in "ud" (which | |
134 | * is only different if a non-null arg was passed which is different | |
135 | * from the current time), but for all practical purposes, returning | |
136 | * the current local zone will do (its all that is ever needed). | |
137 | */ | |
138 | gmt = *gmtime(&t); | |
139 | lt = localtime(&t); | |
140 | ||
141 | off = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min; | |
142 | ||
143 | /* assume that offset isn't more than a day ... */ | |
144 | if (lt->tm_year < gmt.tm_year) | |
145 | off -= 24 * 60; | |
146 | else if (lt->tm_year > gmt.tm_year) | |
147 | off += 24 * 60; | |
148 | else if (lt->tm_yday < gmt.tm_yday) | |
149 | off -= 24 * 60; | |
150 | else if (lt->tm_yday > gmt.tm_yday) | |
151 | off += 24 * 60; | |
152 | ||
153 | *q++ = ' '; | |
154 | if (off == 0) { | |
155 | *q++ = 'G'; | |
156 | *q++ = 'M'; | |
157 | *q++ = 'T'; | |
158 | } else { | |
159 | if (off < 0) { | |
160 | off = -off; | |
161 | *q++ = '-'; | |
162 | } else | |
163 | *q++ = '+'; | |
164 | ||
165 | if (off >= 24*60) /* should be impossible */ | |
166 | off = 23*60+59; /* if not, insert silly value */ | |
167 | ||
168 | *q++ = (off / 600) + '0'; | |
169 | *q++ = (off / 60) % 10 + '0'; | |
170 | off %= 60; | |
171 | *q++ = (off / 10) + '0'; | |
172 | *q++ = (off % 10) + '0'; | |
1a12c7d6 | 173 | } |
91dec0d2 | 174 | *q = '\0'; |
1a12c7d6 | 175 | |
b3cbe40f EA |
176 | return (b); |
177 | } |