oldgaa_error_code PRIVATE
oldgaa_evaluate_day_cond(oldgaa_conditions_ptr condition,
                         UNUSED(oldgaa_options_ptr    options))

{
    int            retval, j=0;
    oldgaa_error_code oldgaa_status = OLDGAA_NO;

    char *day = NULL, *str1 = NULL, *str2 = NULL, cond[MAX_COND_LENGTH] = {NUL};
    char *current_day = NULL;
    char *value;

    strcpy(cond, condition->value);

    /* get current day */
    current_day = get_day();
    day = oldgaa_strcopy(current_day, day);
    free(current_day);

    /* get first day delimiter */
    str1 = oldgaa_strcopy(get_value(&j, cond, '-'), str1);

    /* get second day delimiter */
    value = get_value(&j, cond, NUL);
    str2 = oldgaa_strcopy(value, str2);
    free(value);

    retval = check_day(str1, str2, day);

    if(retval == -1) return OLDGAA_MAYBE; /* unsupported day format */

    if(retval == 1) oldgaa_status = OLDGAA_YES;

    return oldgaa_status;
}
Exemple #2
0
/*	time_t parse_date(char *)
	Originally by libcurl, Copyright (C) 1998 - 2004, Daniel Stenberg, <*****@*****.**>, et al. */
static inline time_t parse_date(const char *date)
{
    time_t t = 0;
    int tz_offset = -1, year = -1, month = -1, monthday = -1, weekday = -1,
        hours = -1, minutes = -1, seconds = -1;
    struct tm tm;
    enum assume_next dignext = DATE_MDAY;
    const char *indate = date;

    int part = 0; /* max 6 parts */

    while (*date && (part < 6)) {
        int found = 0;

        while (*date && !HTTP_IS_CTYPE(alnum, *date)) {
            date++;
        }

        if (HTTP_IS_CTYPE(alpha, *date)) {
            /* a name coming up */
            char buf[32] = "";
            size_t len;
            sscanf(date, "%31[A-Za-z]", buf);
            len = strlen(buf);

            if (weekday == -1) {
                weekday = check_day(buf, len);
                if (weekday != -1) {
                    found = 1;
                }
            }

            if (!found && (month == -1)) {
                month = check_month(buf);
                if (month != -1) {
                    found = 1;
                }
            }

            if (!found && (tz_offset == -1)) {
                /* this just must be a time zone string */
                tz_offset = check_tzone(buf);
                if (tz_offset != -1) {
                    found = 1;
                }
            }

            if (!found) {
                return -1; /* bad string */
            }
            date += len;
        }
        else if (HTTP_IS_CTYPE(digit, *date)) {
            /* a digit */
            int val;
            char *end;
            if ((seconds == -1) &&
                    (3 == sscanf(date, "%02d:%02d:%02d", &hours, &minutes, &seconds))) {
                /* time stamp! */
                date += 8;
                found = 1;
            }
            else {
                val = (int) strtol(date, &end, 10);

                if ((tz_offset == -1) && ((end - date) == 4) && (val < 1300) &&
                        (indate < date) && ((date[-1] == '+' || date[-1] == '-'))) {
                    /* four digits and a value less than 1300 and it is preceeded with
                    a plus or minus. This is a time zone indication. */
                    found = 1;
                    tz_offset = (val / 100 * 60 + val % 100) * 60;

                    /* the + and - prefix indicates the local time compared to GMT,
                    this we need ther reversed math to get what we want */
                    tz_offset = date[-1] == '+' ? -tz_offset : tz_offset;
                }

                if (((end - date) == 8) && (year == -1) && (month == -1) && (monthday == -1)) {
                    /* 8 digits, no year, month or day yet. This is YYYYMMDD */
                    found = 1;
                    year = val / 10000;
                    month = (val % 10000) / 100 - 1; /* month is 0 - 11 */
                    monthday = val % 100;
                }

                if (!found && (dignext == DATE_MDAY) && (monthday == -1)) {
                    if ((val > 0) && (val < 32)) {
                        monthday = val;
                        found = 1;
                    }
                    dignext = DATE_YEAR;
                }

                if (!found && (dignext == DATE_YEAR) && (year == -1)) {
                    year = val;
                    found = 1;
                    if (year < 1900) {
                        year += year > 70 ? 1900 : 2000;
                    }
                    if(monthday == -1) {
                        dignext = DATE_MDAY;
                    }
                }

                if (!found) {
                    return -1;
                }

                date = end;
            }
        }

        part++;
    }

    if (-1 == seconds) {
        seconds = minutes = hours = 0; /* no time, make it zero */
    }

    if ((-1 == monthday) || (-1 == month) || (-1 == year)) {
        /* lacks vital info, fail */
        return -1;
    }

    if (sizeof(time_t) < 5) {
        /* 32 bit time_t can only hold dates to the beginning of 2038 */
        if (year > 2037) {
            return 0x7fffffff;
        }
    }

    tm.tm_sec = seconds;
    tm.tm_min = minutes;
    tm.tm_hour = hours;
    tm.tm_mday = monthday;
    tm.tm_mon = month;
    tm.tm_year = year - 1900;
    tm.tm_wday = 0;
    tm.tm_yday = 0;
    tm.tm_isdst = 0;

    t = mktime(&tm);

    /* time zone adjust */
    if (t != -1) {
        struct tm *gmt, keeptime2;
        long delta;
        time_t t2;

        if((gmt = php_gmtime_r(&t, &keeptime2))) {
            tm = *gmt; /* MSVC quirks */
        } else {
            return -1; /* illegal date/time */
        }

        t2 = mktime(&tm);

        /* Add the time zone diff (between the given timezone and GMT) and the
        diff between the local time zone and GMT. */
        delta = (tz_offset != -1 ? tz_offset : 0) + (t - t2);

        if((delta > 0) && (t + delta < t)) {
            return -1; /* time_t overflow */
        }

        t += delta;
    }

    return t;
}