2 * TUX - Integrated Application Protocols Layer and Object Cache
4 * Copyright (C) 2000, 2001, Ingo Molnar <mingo@redhat.com>
6 * times.c: time conversion routines.
8 * Original time convserion code Copyright (C) 1999 by Arjan van de Ven
11 /****************************************************************
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 ****************************************************************/
28 #include <linux/time.h>
29 #include <linux/kernel.h>
30 #include <linux/slab.h>
31 #include <linux/ctype.h>
37 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
40 static char *monthName[12] = {
41 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
42 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
45 char itoa_h[60]={'0','0','0','0','0','0','0','0','0','0',
46 '1','1','1','1','1','1','1','1','1','1',
47 '2','2','2','2','2','2','2','2','2','2',
48 '3','3','3','3','3','3','3','3','3','3',
49 '4','4','4','4','4','4','4','4','4','4',
50 '5','5','5','5','5','5','5','5','5','5'};
52 char itoa_l[60]={'0','1','2','3','4','5','6','7','8','9',
53 '0','1','2','3','4','5','6','7','8','9',
54 '0','1','2','3','4','5','6','7','8','9',
55 '0','1','2','3','4','5','6','7','8','9',
56 '0','1','2','3','4','5','6','7','8','9',
57 '0','1','2','3','4','5','6','7','8','9'};
59 int time_unix2ls(time_t zulu, char *buf)
62 int H=0,Min=0,S=0,WD=0;
66 if (zulu > xtime.tv_sec)
70 while (I<TUX_NUMYEARS) {
71 if (TimeDays[I][0]>zulu)
83 if (TimeDays[I][I2]>zulu)
90 rest=zulu - TimeDays[Y][M];
106 /* Format: Day, 01 Mon 1999 01:01:01 GMT */
108 delta = xtime.tv_sec - zulu;
109 if (delta > 6*30*24*60)
111 return sprintf( buf, "%s %02i %04i", monthName[M], D+1, Y);
114 return sprintf( buf, "%s %02i %02i:%02i",
115 monthName[M], D+1, H, Min);
118 static int MonthHash[32] =
119 {0,0,7,0,0,0,0,0,0,0,0,3,0,0,0,2,6,0,5,0,9,8,4,0,0,11,1,10,0,0,0,0};
121 #define is_digit(c) ((c) >= '0' && (c) <= '9')
123 static inline int skip_atoi(char **s)
127 while (is_digit(**s))
128 i = i*10 + *((*s)++) - '0';
132 time_t mimetime_to_unixtime(char *Q)
142 if (strlen(s)<30) return 0;
143 if (s[3]!=',') return 0;
144 if (s[19]!=':') return 0;
146 s+=5; /* Skip day of week */
147 D = skip_atoi(s2); /* Day of month */
149 Hash = (char)s[0]+(char)s[2];
150 Hash = (Hash<<1) + (char)s[1];
154 Y = skip_atoi(s2); /* Year */
156 H = skip_atoi(s2); /* Hour */
158 Min = skip_atoi(s2); /* Minutes */
160 S = skip_atoi(s2); /* Seconds */
162 if ((s[0]!='G')||(s[1]!='M')||(s[2]!='T'))
164 return 0; /* No GMT */
167 if (Y<TUX_YEAROFFSET) Y = TUX_YEAROFFSET;
168 if (Y>TUX_YEAROFFSET+9) Y = TUX_YEAROFFSET+9;
170 Temp = TimeDays[Y-TUX_YEAROFFSET][M];
171 Temp += D*86400+H*3600+Min*60+S;
176 // writes the full http date, corresponding to time_t received
178 void last_mod_time(char * curr, const time_t t)
180 int day, tod, year, wday, mon, hour, min, sec;
194 wday = (day + 4) % 7;
199 /* day 0 is march 1, 2000 */
200 year = 5 + day / 146097;
206 /* from now on, day is nonnegative */
228 mon = (day + 5) / 306;
229 day = day + 5 - 306 * mon;
237 sprintf(curr, "%s, %.2d %s %d %.2d:%.2d:%.2d GMT", dayName[wday],
238 day+1, monthName[mon], year, hour, min, sec);
241 // writes the full date in ISO8601 format,
242 // corresponding to time_t received
243 // example: 20011126224910
245 int mdtm_time(char * curr, const time_t t)
247 int day, tod, year, wday, mon, hour, min, sec;
261 wday = (day + 4) % 7;
266 /* day 0 is march 1, 2000 */
267 year = 5 + day / 146097;
273 /* from now on, day is nonnegative */
295 mon = (day + 5) / 306;
296 day = day + 5 - 306 * mon;
304 return sprintf(curr, "213 %.4d%.2d%.2d%.2d%.2d%.2d\r\n",
305 year, mon+1, day+1, hour, min, sec);
308 static inline int make_num(const char *s)
310 if (*s >= '0' && *s <= '9')
311 return 10 * (*s - '0') + *(s + 1) - '0';
313 return *(s + 1) - '0';
316 static inline int make_month(const char *s)
320 for (i = 0; i < 12; i++)
321 if (!strncmp(monthName[i], s, 3))
326 time_t parse_time(const char *str, const int str_len)
336 /* Thu, 09 Jan 1993 01:29:59 GMT */
341 mday = make_num(str+5);
342 mon = make_month(str + 8);
343 year = 100 * make_num(str + 12) + make_num(str + 14);
344 hour = make_num(str + 17);
345 min = make_num(str + 20);
346 sec = make_num(str + 23);
350 s = strchr(str, ',');
351 if (!s || (str_len - (s - str) < 24)) {
352 /* Wed Jun 9 01:29:59 1993 */
357 mon = make_month(str+4);
358 mday = make_num(str+8);
359 hour = make_num(str+11);
360 min = make_num(str+14);
361 sec = make_num(str+17);
362 year = make_num(str+20)*100 + make_num(str+22);
365 /* Thursday, 10-Jun-93 01:29:59 GMT */
367 mday = make_num(s + 2);
368 mon = make_month(s + 5);
369 year = make_num(s + 9) + 1900;
372 hour = make_num(s + 12);
373 min = make_num(s + 15);
374 sec = make_num(s + 18);
378 if (sec < 0 || sec > 59)
380 if (min < 0 || min > 59)
382 if (hour < 0 || hour > 23)
384 if (mday < 1 || mday > 31)
386 if (mon < 1 || mon > 12)
388 if (year < 1970 || year > 2020)
391 return mktime(year, mon, mday, hour, min, sec);