Ejemplo n.º 1
0
bool MaskedDateReader :: Read( const string & ds, ALib::Date & date ) {

    ALib::STRPOS s1 = ds.find( mSep[0] );
    ALib::STRPOS s2 = ds.find_last_of( mSep[1] );
    if ( s1 == std::string::npos  ||
            s2 == std::string::npos  ||
            s1 >= s2 ) {
        return false;
    }

    string s[3];
    s[0] = ds.substr( 0, s1 );
    s[1] = ds.substr( s1 + 1, (s2 - s1) - 1 );
    s[2] = ds.substr( s2 + 1 );

    int day = -1, month = -1, year = -1;

    for ( unsigned int i = 0; i < 3; i++ ) {
        switch( mDMY[i] ) {
        case 'd':
            MakeDay( s[i], day );
            break;
        case 'm':
            MakeMonth( s[i], month );
            break;
        case 'y':
            MakeYear( s[i], year );
            break;
        }
    }

    if ( ALib::Date::Validate( year, month, day ) != ALib::Date::DATEOK ) {
        return false;
    }

    date = ALib::Date( year, month, day );
    return true;

}
Ejemplo n.º 2
0
time_t
Ns_ParseHttpTime(char *str)
{
    char      *s;
    struct tm  tm;
    time_t     t;

    if (str == NULL) {
        return 0;
    }

    /*
     * Find the comma after day-of-week
     *
     * Thursday, 10-Jun-93 01:29:59 GMT
     *         ^
     *         +-- s
     *
     * Thu, 10 Jan 1993 01:29:59 GMT
     *    ^
     *    +-- s
     */

    s = strchr(str, ',');
    if (s != NULL) {

	/*
	 * Advance S to the first non-space after the comma
	 * which should be the first digit of the day.
	 */
	
        s++;
        while (*s && *s == ' ') {
            s++;
	}

	/*
	 * Figure out which format it is in. If there is a hyphen, then
	 * it must be the first format.
	 */
	
        if (strchr(s, '-') != NULL) {
            if (strlen(s) < 18) {
                return 0;
            }

	    /*
	     * The format is:
	     *
	     * Thursday, 10-Jun-93 01:29:59 GMT
	     *           ^
	     *           +--s
	     */
	    
            tm.tm_mday = MakeNum(s);
            tm.tm_mon = MakeMonth(s + 3);
            tm.tm_year = MakeNum(s + 7);
            tm.tm_hour = MakeNum(s + 10);
            tm.tm_min = MakeNum(s + 13);
            tm.tm_sec = MakeNum(s + 16);
        } else {
            if ((int) strlen(s) < 20) {
                return 0;
            }

	    /*
	     * The format is:
	     *
	     * Thu, 10 Jan 1993 01:29:59 GMT
	     *      ^
	     *      +--s
	     */
	    
            tm.tm_mday = MakeNum(s);
            tm.tm_mon = MakeMonth(s + 3);
            tm.tm_year = (100 * MakeNum(s + 7) - 1900) + MakeNum(s + 9);
            tm.tm_hour = MakeNum(s + 12);
            tm.tm_min = MakeNum(s + 15);
            tm.tm_sec = MakeNum(s + 18);
        }
    } else {

	/*
	 * No commas, so it must be the third, fixed field, format:
	 *
	 * Wed Jun  9 01:29:59 1993 GMT
	 *
	 * Advance s to the first letter of the month.
	 */
	 
        s = str;
        while (*s && *s == ' ') {
            s++;
	}
        if ((int) strlen(s) < 24) {
            return 0;
        }
        tm.tm_mday = MakeNum(s + 8);
        tm.tm_mon = MakeMonth(s + 4);
        tm.tm_year = MakeNum(s + 22);
        tm.tm_hour = MakeNum(s + 11);
        tm.tm_min = MakeNum(s + 14);
        tm.tm_sec = MakeNum(s + 17);
    }

    /*
     * If there are any impossible values, then return an error.
     */
    
    if (tm.tm_sec < 0 || tm.tm_sec > 59 ||
        tm.tm_min < 0 || tm.tm_min > 59 ||
        tm.tm_hour < 0 || tm.tm_hour > 23 ||
        tm.tm_mday < 1 || tm.tm_mday > 31 ||
        tm.tm_mon < 0 || tm.tm_mon > 11 ||
        tm.tm_year < 70 || tm.tm_year > 120) {
        return 0;
    }
    tm.tm_isdst = 0;
#ifdef HAVE_TIMEGM
    t = timegm(&tm);
#else
    t = mktime(&tm) - timezone;
#endif
    return t;
}