Beispiel #1
0
static int parsetime(const char **t)
{
	unsigned h = 0, m = 0, s = 0;

	if (!isdigit((int)(unsigned char) **t))
		return (-1);
	h = parsedig(t);
	if (h > 23)
		return (-1);
	if (**t != ':')
		return (-1);
	++*t;
	if (!isdigit((int) (unsigned char) **t))
		return (-1);
	m = parsedig(t);
	if (**t == ':')
	{
		++*t;
		if (!isdigit((int) (unsigned char) **t))
			return (-1);
		s = parsedig(t);
	}
	if (m > 59 || s > 59)
		return (-1);
	return (h * 60 * 60 + m * 60 + s);
}
Beispiel #2
0
time_t rfc822_parsedt(const char *rfcdt)
{
unsigned day=0, mon=0, year;
int secs;
int offset;
time_t t;
unsigned y;

	/* Ignore day of the week.  Tolerate "Tue, 25 Feb 1997 ... "
	** without the comma.  Tolerate "Feb 25 1997 ...".
	*/

	while (!day || !mon)
	{
		if (!*rfcdt)	return (0);
		if (isalpha((int)(unsigned char)*rfcdt))
		{
			if (mon)	return (0);
			mon=parsekey(&rfcdt, mnames);
			if (!mon)
				while (*rfcdt && isalpha((int)(unsigned char)*rfcdt))
					++rfcdt;
			continue;
		}

		if (isdigit((int)(unsigned char)*rfcdt))
		{
			if (day)	return (0);
			day=parsedig(&rfcdt);
			if (!day)	return (0);
			continue;
		}
		++rfcdt;
	}

	while (*rfcdt && isspace((int)(unsigned char)*rfcdt))
		++rfcdt;
	if (!isdigit((int)(unsigned char)*rfcdt))	return (0);
	year=parsedig(&rfcdt);
	if (year < 70)	year += 2000;
	if (year < 100)	year += 1900;

	while (*rfcdt && isspace((int)(unsigned char)*rfcdt))
		++rfcdt;

	if (day == 0 || mon == 0 || mon > 12 || day > mdays(mon,year))
		return (0);

	secs=parsetime(&rfcdt);
	if (secs < 0)	return (0);

	offset=0;

	/* RFC822 sez no parenthesis, but I've seen (EST) */

	while ( *rfcdt )
	{
		if (isalnum((int)(unsigned char)*rfcdt) || *rfcdt == '+' || *rfcdt == '-')
			break;
		++rfcdt;
	}

	if (isalpha((int)(unsigned char)*rfcdt))
	{
	int	n=parsekey(&rfcdt, zonenames);

		if (n > 0)	offset= zoneoffset[n-1];
	}
	else
	{
	int	sign=1;
	unsigned n;

		switch (*rfcdt)	{
		case '-':
			sign= -1;
		case '+':
			++rfcdt;
		}

		if (isdigit((int)(unsigned char)*rfcdt))
		{
			n=parsedig(&rfcdt);
			if (n > 2359 || (n % 100) > 59)	n=0;
			offset = sign * ( (n % 100) * 60 + n / 100 * 60 * 60);
		}
	}

	if (year < 1970)	return (0);

	t=0;
	for (y=1970; y<year; y++)
	{
		if ( leap(y) )
		{
			if (year-y >= 4)
			{
				y += 3;
				t += ( 365*3+366 ) * 24 * 60 * 60;
				continue;
			}
			t += 24 * 60 * 60;
		}
		t += 365 * 24 * 60 * 60;
	}

	for (y=1; y < mon; y++)
		t += mdays(y, year) * 24 * 60 * 60;

	return ( t + (day-1) * 24 * 60 * 60 + secs - offset );
}