1 /*
2 Date manipulation routines
3 Copyright (C) 1999-2021, Joe Orton <joe@manyfish.co.uk>
4 Copyright (C) 2004 Jiang Lei <tristone@deluxe.ocn.ne.jp>
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public
17 License along with this library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
19 MA 02111-1307, USA
20
21 */
22
23 #include "config.h"
24
25 #include <sys/types.h>
26
27 #include <time.h>
28 #ifdef HAVE_STDLIB_H
29 #include <stdlib.h>
30 #endif
31 #include <stdio.h>
32
33 #ifdef HAVE_STRING_H
34 #include <string.h>
35 #endif
36
37 #ifdef WIN32
38 #include <windows.h> /* for TIME_ZONE_INFORMATION */
39 #endif
40
41 #include "ne_alloc.h"
42 #include "ne_dates.h"
43 #include "ne_string.h"
44
45 /* Generic date manipulation routines. */
46
47 /* ISO8601: 2001-01-01T12:30:00Z */
48 #define ISO8601_FORMAT_Z "%04d-%02d-%02dT%02d:%02d:%lfZ"
49 #define ISO8601_FORMAT_M "%04d-%02d-%02dT%02d:%02d:%lf-%02d:%02d"
50 #define ISO8601_FORMAT_P "%04d-%02d-%02dT%02d:%02d:%lf+%02d:%02d"
51
52 /* RFC1123: Sun, 06 Nov 1994 08:49:37 GMT */
53 #define RFC1123_FORMAT "%3s, %02d %3s %4d %02d:%02d:%02d GMT"
54 /* RFC850: Sunday, 06-Nov-94 08:49:37 GMT */
55 #define RFC1036_FORMAT "%10s %2d-%3s-%2d %2d:%2d:%2d GMT"
56 /* asctime: Wed Jun 30 21:49:08 1993 */
57 #define ASCTIME_FORMAT "%3s %3s %2d %2d:%2d:%2d %4d"
58
59 static const char rfc1123_weekdays[7][4] = {
60 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
61 };
62 static const char short_months[12][4] = {
63 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
64 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
65 };
66
67 #if defined(HAVE_STRUCT_TM_TM_GMTOFF)
68 #define GMTOFF(t) ((t).tm_gmtoff)
69 #elif defined(HAVE_STRUCT_TM___TM_GMTOFF)
70 #define GMTOFF(t) ((t).__tm_gmtoff)
71 #elif defined(WIN32)
72 #define GMTOFF(t) (gmt_to_local_win32())
73 #elif defined(HAVE_TIMEZONE)
74 /* FIXME: the following assumes fixed dst offset of 1 hour */
75 #define GMTOFF(t) (-timezone + ((t).tm_isdst > 0 ? 3600 : 0))
76 #else
77 /* FIXME: work out the offset anyway. */
78 #define GMTOFF(t) (0)
79 #endif
80
81 #ifdef WIN32
gmt_to_local_win32(void)82 time_t gmt_to_local_win32(void)
83 {
84 TIME_ZONE_INFORMATION tzinfo;
85 DWORD dwStandardDaylight;
86 long bias;
87
88 dwStandardDaylight = GetTimeZoneInformation(&tzinfo);
89 bias = tzinfo.Bias;
90
91 if (dwStandardDaylight == TIME_ZONE_ID_STANDARD)
92 bias += tzinfo.StandardBias;
93
94 if (dwStandardDaylight == TIME_ZONE_ID_DAYLIGHT)
95 bias += tzinfo.DaylightBias;
96
97 return (- bias * 60);
98 }
99 #endif
100
101 /* Returns the time/date GMT, in RFC1123-type format: eg
102 * Sun, 06 Nov 1994 08:49:37 GMT. */
ne_rfc1123_date(time_t anytime)103 char *ne_rfc1123_date(time_t anytime) {
104 struct tm *gmt;
105 char *ret;
106 gmt = gmtime(&anytime);
107 if (gmt == NULL)
108 return NULL;
109 ret = ne_malloc(29 + 1); /* dates are 29 chars long */
110 /* it goes: Sun, 06 Nov 1994 08:49:37 GMT */
111 ne_snprintf(ret, 30, RFC1123_FORMAT,
112 rfc1123_weekdays[gmt->tm_wday], gmt->tm_mday,
113 short_months[gmt->tm_mon], 1900 + gmt->tm_year,
114 gmt->tm_hour, gmt->tm_min, gmt->tm_sec);
115
116 return ret;
117 }
118
119 /* Takes an ISO-8601-formatted date string and returns the time_t.
120 * Returns (time_t)-1 if the parse fails. */
ne_iso8601_parse(const char * date)121 time_t ne_iso8601_parse(const char *date)
122 {
123 struct tm gmt = {0};
124 int off_hour, off_min;
125 double sec;
126 off_t fix;
127 time_t result;
128
129 /* it goes: ISO8601: 2001-01-01T12:30:00+03:30 */
130 if (sscanf(date, ISO8601_FORMAT_P,
131 &gmt.tm_year, &gmt.tm_mon, &gmt.tm_mday,
132 &gmt.tm_hour, &gmt.tm_min, &sec,
133 &off_hour, &off_min) == 8) {
134 gmt.tm_sec = (int)sec;
135 fix = - off_hour * 3600 - off_min * 60;
136 }
137 /* it goes: ISO8601: 2001-01-01T12:30:00-03:30 */
138 else if (sscanf(date, ISO8601_FORMAT_M,
139 &gmt.tm_year, &gmt.tm_mon, &gmt.tm_mday,
140 &gmt.tm_hour, &gmt.tm_min, &sec,
141 &off_hour, &off_min) == 8) {
142 gmt.tm_sec = (int)sec;
143 fix = off_hour * 3600 + off_min * 60;
144 }
145 /* it goes: ISO8601: 2001-01-01T12:30:00Z */
146 else if (sscanf(date, ISO8601_FORMAT_Z,
147 &gmt.tm_year, &gmt.tm_mon, &gmt.tm_mday,
148 &gmt.tm_hour, &gmt.tm_min, &sec) == 6) {
149 gmt.tm_sec = (int)sec;
150 fix = 0;
151 }
152 else {
153 return (time_t)-1;
154 }
155
156 gmt.tm_year -= 1900;
157 gmt.tm_isdst = -1;
158 gmt.tm_mon--;
159
160 result = mktime(&gmt) + fix;
161 return result + GMTOFF(gmt);
162 }
163
164 /* Takes an RFC1123-formatted date string and returns the time_t.
165 * Returns (time_t)-1 if the parse fails. */
ne_rfc1123_parse(const char * date)166 time_t ne_rfc1123_parse(const char *date)
167 {
168 struct tm gmt = {0};
169 char wkday[4], mon[4];
170 int n;
171 time_t result;
172
173 /* it goes: Sun, 06 Nov 1994 08:49:37 GMT */
174 if (sscanf(date, RFC1123_FORMAT,
175 wkday, &gmt.tm_mday, mon, &gmt.tm_year, &gmt.tm_hour,
176 &gmt.tm_min, &gmt.tm_sec) != 7)
177 return (time_t) -1;
178
179 gmt.tm_year -= 1900;
180 for (n=0; n<12; n++)
181 if (strcmp(mon, short_months[n]) == 0)
182 break;
183 /* tm_mon comes out as 12 if the month is corrupt, which is desired,
184 * since the mktime will then fail */
185 gmt.tm_mon = n;
186 gmt.tm_isdst = -1;
187 result = mktime(&gmt);
188 return result + GMTOFF(gmt);
189 }
190
191 /* Takes a string containing a RFC1036-style date and returns the time_t */
ne_rfc1036_parse(const char * date)192 time_t ne_rfc1036_parse(const char *date)
193 {
194 struct tm gmt = {0};
195 int n;
196 char wkday[11], mon[4];
197 time_t result;
198
199 /* RFC850/1036 style dates: Sunday, 06-Nov-94 08:49:37 GMT */
200 n = sscanf(date, RFC1036_FORMAT,
201 wkday, &gmt.tm_mday, mon, &gmt.tm_year,
202 &gmt.tm_hour, &gmt.tm_min, &gmt.tm_sec);
203 if (n != 7) {
204 return (time_t)-1;
205 }
206
207 for (n=0; n<12; n++)
208 if (strcmp(mon, short_months[n]) == 0)
209 break;
210 /* tm_mon comes out as 12 if the month is corrupt, which is desired,
211 * since the mktime will then fail */
212
213 /* Defeat Y2K bug. */
214 if (gmt.tm_year < 50)
215 gmt.tm_year += 100;
216
217 gmt.tm_mon = n;
218 gmt.tm_isdst = -1;
219 result = mktime(&gmt);
220 return result + GMTOFF(gmt);
221 }
222
223
224 /* (as)ctime dates are like:
225 * Wed Jun 30 21:49:08 1993
226 */
ne_asctime_parse(const char * date)227 time_t ne_asctime_parse(const char *date)
228 {
229 struct tm gmt = {0};
230 int n;
231 char wkday[4], mon[4];
232 time_t result;
233
234 if (sscanf(date, ASCTIME_FORMAT,
235 wkday, mon, &gmt.tm_mday,
236 &gmt.tm_hour, &gmt.tm_min, &gmt.tm_sec,
237 &gmt.tm_year) != 7)
238 return (time_t)-1;
239
240 gmt.tm_year -= 1900;
241 for (n=0; n<12; n++)
242 if (strcmp(mon, short_months[n]) == 0)
243 break;
244 /* tm_mon comes out as 12 if the month is corrupt, which is desired,
245 * since the mktime will then fail */
246 gmt.tm_mon = n;
247 gmt.tm_isdst = -1;
248 result = mktime(&gmt);
249 return result + GMTOFF(gmt);
250 }
251
252 /* HTTP-date parser */
ne_httpdate_parse(const char * date)253 time_t ne_httpdate_parse(const char *date)
254 {
255 time_t tmp;
256 tmp = ne_rfc1123_parse(date);
257 if (tmp == -1) {
258 tmp = ne_rfc1036_parse(date);
259 if (tmp == -1)
260 tmp = ne_asctime_parse(date);
261 }
262 return tmp;
263 }
264