Exemple #1
0
/* idea derived from mini_httpd */
time_t get_timet(char * str)
{
    time_t t;
    struct tm tm;
    char wday[10], mon[10];
    int year,mday,hour,min,sec,w_day,mth;
    char * tmp = str;    
    memset(&tm,0,sizeof(struct tm));    
    while ( *tmp == ' ' )
        tmp++;        
    /*  An example of the field is:
          If-Modified-Since: Sat, 29 Oct 1994 19:43:31 GMT    
    
        HTTP-date = rfc1123-date | rfc850-date | asctime-date
        
        add more format later
    */
    
    /* wkday, DD mth year HH:MM:SS GMT (rfc1123-date) */ 
    if (sscanf(tmp,"%400[a-zA-Z], %d %400[a-zA-Z] %d %d:%d:%d GMT",wday,&mday,mon,&year,&hour,&min,&sec) == 7 &&
        (w_day = get_wday(wday)) != -1 && (mth = get_mth(mon)) != -1 ) 
    {
        tm.tm_wday = w_day;
        tm.tm_mday = mday;
        tm.tm_mon = mth;
        tm.tm_year = year;
        tm.tm_hour = hour;
        tm.tm_min = min;
        tm.tm_sec = sec;
        // printf("%s\n",str);
        // printf("%d %d %d %d %d:%d:%d\n",year,mth,mday,w_day,hour,min,sec);
    }
    
    /* weekday, DD-mth-YY HH:MM:SS GMT (rfc850-date)*/ 
    else if (sscanf( tmp, "%[a-zA-Z], %d-%400[a-zA-Z]-%d %d:%d:%d GMT",wday,&mday,mon,&year,&hour,&min,&sec) == 7 &&
        (w_day = get_wday(wday)) != -1 && (mth = get_mth(mon)) != -1 )
    {
        w_day = get_wday(wday);
        mth = get_mth(mon);
        
        tm.tm_wday = w_day;
        tm.tm_mday = mday;
        tm.tm_mon = mth;
        tm.tm_year = year;
        tm.tm_hour = hour;
        tm.tm_min = min;
        tm.tm_sec = sec;
        // printf("%s\n",str);
        // printf("%d %d %d %d %d:%d:%d\n",year,mth,mday,w_day,hour,min,sec);
    }
    else return (time_t)-1;
    
    if ( tm.tm_year > 1900 )
        tm.tm_year -= 1900;
    else if ( tm.tm_year < 70 )
        tm.tm_year += 100;    
        
    t = timegm(&tm);    
    return t;
}
static time_t
decode_broken_date (struct _date_token *tokens, int *tzone)
{
	gboolean got_wday, got_month, got_tzone;
	int hour, min, sec, offset, n;
	struct _date_token *token;
	struct tm tm;
	time_t time;

	memset ((void *) &tm, 0, sizeof (struct tm));
	got_wday = got_month = got_tzone = FALSE;
	offset = 0;

	token = tokens;
	while (token) {
		if (is_weekday (token) && !got_wday) {
			if ((n = get_wday (token->start, token->len)) != -1) {
				d(printf ("weekday; "));
				got_wday = TRUE;
				tm.tm_wday = n;
				goto next_token;
			}
		}

		if (is_month (token) && !got_month) {
			if ((n = get_month (token->start, token->len)) != -1) {
				d(printf ("month; "));
				got_month = TRUE;
				tm.tm_mon = n;
				goto next_token;
			}
		}

		if (is_time (token) && !tm.tm_hour && !tm.tm_min && !tm.tm_sec) {
			if (get_time (token->start, token->len, &hour, &min, &sec)) {
				d(printf ("time; "));
				tm.tm_hour = hour;
				tm.tm_min = min;
				tm.tm_sec = sec;
				goto next_token;
			}
		}

		if (is_tzone (token) && !got_tzone) {
			struct _date_token *t = token;

			if ((n = get_tzone (&t)) != -1) {
				d(printf ("tzone; "));
				got_tzone = TRUE;
				offset = n;
				goto next_token;
			}
		}

		if (is_numeric (token)) {
			if (token->len == 4 && !tm.tm_year) {
				if ((n = get_year (token->start, token->len)) != -1) {
					d(printf ("year; "));
					tm.tm_year = n - 1900;
					goto next_token;
				}
			} else {
				if (!got_month && !got_wday && token->next && is_numeric (token->next)) {
					d(printf ("mon; "));
					n = decode_int (token->start, token->len);
					got_month = TRUE;
					tm.tm_mon = n - 1;
					goto next_token;
				} else if (!tm.tm_mday && (n = get_mday (token->start, token->len)) != -1) {
					d(printf ("mday; "));
					tm.tm_mday = n;
					goto next_token;
				} else if (!tm.tm_year) {
					d(printf ("2-digit year; "));
					n = get_year (token->start, token->len);
					tm.tm_year = n - 1900;
					goto next_token;
				}
			}
		}

		d(printf ("???; "));

	next_token:

		token = token->next;
	}

	d(printf ("\n"));

	time = e_mktime_utc (&tm);

	/* time is now GMT of the time we want, but not offset by the timezone ... */

	/* this should convert the time to the GMT equiv time */
	time -= ((offset / 100) * 60 * 60) + (offset % 100) * 60;

	if (tzone)
		*tzone = offset;

	return time;
}