C   ---------------------------------------------------------------------------
C   TIMECNV.FOR - The function of this routine is to convert the absolute
C	time, delta time, or interger value character string passed into 
C	a positive 64 bit binary value.  If the format is incorrect then the
C	status returned is even.
C
C	Note:  The delta times will be converted to positive values.
C
C   Calling Procedure:
C
C	status = TIMECNV( string, binary_value )
C
C   Entry Conditions:
C
C	string - must contain the absolute time, the delta time or an integer
C		 character string (integer = 0 through 2147483647) or a
C		 recognized keyword name.
C
C   Exit Conditions:
C
C	status = 3 if string is absolute time format.
C	       = 4 if invalid absolute time format.
C	       = 5 if string is delta time format.
C	       = 6 if invalid delta time format.
C	       = 7 if integer value.
C	       = 8 if invalid integer value.
C
C	binary_value - contains the binary value of the value converted.
C		Both absolute times and delta times will be positive.
C		This variable must be 2 longword long.
C
C   ---------------------------------------------------------------------------
C
	INTEGER*4 FUNCTION TIMECNV( STRING, BIN_VAL )
C
	IMPLICIT INTEGER*4 (A-Z)
C
	EXTERNAL	OTS$_INPCONERR
C
	PARAMETER	DASH='-'
	PARAMETER	COLON=':'
	PARAMETER	BLANK=' '
	PARAMETER	PERIOD='.'
C
	CHARACTER*(*)	STRING
	CHARACTER	WORK*(32)
C
	INTEGER*4	TIMEKEY
C
	INTEGER*4	BIN_VAL(2)
	INTEGER*4	ZERO(2) /0,0/
C
C   ---------------------------------------------------------------------------
C
C   Initialize the return code to integer value.
C
	TIMECNV = 7
C
C   Determine if it is absolute, delta, or integer format.
C	2 dashes is absolute.
C	1 dash or (no dash and a colon) or (no dash and a period) is delta.
C	no dash and no colon and no period is and integer.
C
	I = INDEX( STRING, DASH )
	IF ( I .NE. 0 ) THEN	
		TIMECNV = 5
		J = INDEX( STRING(I+1:), DASH )
		IF ( J .NE. 0 ) THEN
			TIMECNV = 3
		ENDIF
	ENDIF
C 
	IF ( TIMECNV .EQ. 7 ) THEN
		IF ( (INDEX( STRING, COLON ) + 
	1	      INDEX( STRING, PERIOD )) .NE. 0 ) TIMECNV = 5
	ENDIF
C
C   ---------------------------------------------------------------------------
C
C   Convert the time based on the type of time it is.
C
	IF ( TIMECNV .EQ. 3 ) THEN
C
C	   Absolute time; remove the colon from between the year and hour
C 		if present.
C
		B1 = INDEX( STRING, BLANK )
		IF ( B1 .EQ. 0 ) THEN
			B1 = INDEX( STRING, COLON )
			IF ( B1 .NE. 0 ) THEN
				STRING(B1:B1) = BLANK
			ENDIF
		ENDIF
C
C	   If trailing fields of the time of day are missing supply 0's.
C
		WORK = STRING
		IF ( B1 .NE. 0 ) THEN		! no time; allow full subs.
		    L = LEN( STRING ) + 1
		    C1 = INDEX( WORK, COLON )
		    IF ( C1 .EQ. 0 ) THEN
			WORK(L:) = ':0:0.0'
		    ELSEIF ( INDEX( WORK(C1+1:), COLON ) .EQ. 0 ) THEN
			WORK(L:) = ':0.0'
		    ELSEIF ( INDEX( WORK, PERIOD ) .EQ. 0 ) THEN
			WORK(L:) = '.0'
		    ENDIF
		ENDIF
C
C	   Convert to absolute binary time.
C
		STATUS = SYS$BINTIM( WORK, BIN_VAL )
		IF ( .NOT. STATUS ) THEN
C
C		    Specify return status of invalid absolute time format.
C
			TIMECNV = 4
		ENDIF
C
C   ---------------------------------------------------------------------------
C
	ELSEIF ( TIMECNV .EQ. 5 ) THEN
C
C	   Delta time; remove the dash from between the days and the hour.
C
		I = INDEX( STRING, DASH )
		IF ( I .NE. 0 ) THEN
			STRING(I:I) = BLANK
		ENDIF
C
C	   Convert to delta binary time.
C
		STATUS = SYS$BINTIM( STRING, BIN_VAL )
		IF ( .NOT. STATUS ) THEN
C
C		    Specify return status of invalid delta time format.
C
			TIMECNV = 6
		ENDIF
		STATUS = LIB$SUBX( ZERO, BIN_VAL, BIN_VAL, 2 )
C
C   ---------------------------------------------------------------------------
C
	ELSEIF ( TIMEKEY(string, bin_val)) THEN
C
C Keyword was found, treat as an absolute time
C
		timecnv = 3
	ELSE
C
C	   Convert the integer value to binary.
C
		STATUS = OTS$CVT_TI_L( STRING, BIN_VAL(1), %VAL(4), %VAL(3) )
		IF ( STATUS .EQ. %LOC(OTS$_INPCONERR) .OR.
	1	      BIN_VAL(1) .LT. 0 ) THEN
C
C		   Specify return status of invalid integer value format.
C
			TIMECNV = 8
		ENDIF
		BIN_VAL(2) = 0
	ENDIF
C
C   ---------------------------------------------------------------------------
C
C   Return
C
	RETURN
	END
