FreeBSD/usr.sbin/bsnmpd/tools/libbsnmptools/bsnmptc.c
/* Read a DateAndTime string eg. 1992-5-26,13:30:15.0,-4:0. */
Read a DateAndTime string eg.
Read a DateAndTime string eg .
static int32_t
parse_dateandtime(struct snmp_value *sv, char *val)
{
	char *endptr;
	uint32_t v;
	uint8_t	date[SNMP_DATETIME_OCTETS];
	v = strtoul(val, &endptr, 10);
	if (v > 0xffff || *endptr != '-')
		goto error;
	date[0] = ((v & 0xff00) >> 8);
	date[1] = (v & 0xff);
	val = endptr + 1;
	v = strtoul(val, &endptr, 10);
	if (v == 0 || v > 12 || *endptr != '-')
		goto error;
	date[2] = v;
	val = endptr + 1;
	v = strtoul(val, &endptr, 10);
	if (v == 0 || v > 31 || *endptr != ',')
		goto error;
	date[3] = v;
	val = endptr + 1;
	v = strtoul(val, &endptr, 10);
	if (v > 23 || *endptr != ':')
		goto error;
	date[4] = v;
	val = endptr + 1;
	v = strtoul(val, &endptr, 10);
	if (v > 59 || *endptr != ':')
		goto error;
	date[5] = v;
	val = endptr + 1;
	v = strtoul(val, &endptr, 10);
	if (v > 60 || *endptr != '.')
		goto error;
	date[6] = v;
	val = endptr + 1;
	v = strtoul(val, &endptr, 10);
	if (v > 9 || *endptr != ',')
		goto error;
	date[7] = v;
	val = endptr + 1;
	if (*val != '-' && *val != '+')
		goto error;
	date[8] = (uint8_t) *val;
	val = endptr + 1;
	v = strtoul(val, &endptr, 10);
	if (v > 13 || *endptr != ':')
		goto error;
	date[9] = v;
	val = endptr + 1;
	v = strtoul(val, &endptr, 10);
	if (v > 59 || *endptr != '\0')
		goto error;
	date[10] = v;
	if ((sv->v.octetstring.octets = malloc(SNMP_DATETIME_OCTETS)) == NULL) {
		warnx("malloc() failed - %s", strerror(errno));
		return (-1);
	}
	sv->v.octetstring.len = SNMP_DATETIME_OCTETS;
	memcpy(sv->v.octetstring.octets, date, SNMP_DATETIME_OCTETS);
	sv->syntax = SNMP_SYNTAX_OCTETSTRING;
	return (1);
  error:
	warnx("Date value %s not supported", val);
	return (-1);
}
static int32_t parse_dateandtime ( struct snmp_value * sv , char * val ) { char * endptr ; uint32_t v ; uint8_t date [ SNMP_DATETIME_OCTETS ] ; v = strtoul ( val , & endptr , 10 ) ; if ( v > 0xffff || * endptr != ' ' ) goto error ; date [ 0 ] = ( ( v & 0xff00 ) >> 8 ) ; date [ 1 ] = ( v & 0xff ) ; val = endptr + 1 ; v = strtoul ( val , & endptr , 10 ) ; if ( v == 0 || v > 12 || * endptr != ' ' ) goto error ; date [ 2 ] = v ; val = endptr + 1 ; v = strtoul ( val , & endptr , 10 ) ; if ( v == 0 || v > 31 || * endptr != ' ' ) goto error ; date [ 3 ] = v ; val = endptr + 1 ; v = strtoul ( val , & endptr , 10 ) ; if ( v > 23 || * endptr != ' ' ) goto error ; date [ 4 ] = v ; val = endptr + 1 ; v = strtoul ( val , & endptr , 10 ) ; if ( v > 59 || * endptr != ' ' ) goto error ; date [ 5 ] = v ; val = endptr + 1 ; v = strtoul ( val , & endptr , 10 ) ; if ( v > 60 || * endptr != ' ' ) goto error ; date [ 6 ] = v ; val = endptr + 1 ; v = strtoul ( val , & endptr , 10 ) ; if ( v > 9 || * endptr != ' ' ) goto error ; date [ 7 ] = v ; val = endptr + 1 ; if ( * val != ' ' && * val != ' ' ) goto error ; date [ 8 ] = ( uint8_t ) * val ; val = endptr + 1 ; v = strtoul ( val , & endptr , 10 ) ; if ( v > 13 || * endptr != ' ' ) goto error ; date [ 9 ] = v ; val = endptr + 1 ; v = strtoul ( val , & endptr , 10 ) ; if ( v > 59 || * endptr != ' \0 ' ) goto error ; date [ 10 ] = v ; if ( ( sv -> v . octetstring . octets = malloc ( SNMP_DATETIME_OCTETS ) ) == NULL ) { warnx ( " " , strerror ( errno ) ) ; return ( -1 ) ; } sv -> v . octetstring . len = SNMP_DATETIME_OCTETS ; memcpy ( sv -> v . octetstring . octets , date , SNMP_DATETIME_OCTETS ) ; sv -> syntax = SNMP_SYNTAX_OCTETSTRING ; return ( 1 ) ; error : warnx ( " " , val ) ; return ( -1 ) ; }
Read a DateAndTime string eg.
Read a DateAndTime string eg .
/* 'YYYY-' */ /* 'MM-' */ /* 'DD,' */ /* 'HH:' */ /* 'MM:' */ /* 'SS.' */ /* '(deci-)s,' */ /* offset - '+/-' */ /* 'HH:' - offset from UTC */ /* 'MM'\0''  offset from UTC */