	.TITLE ADATIM
	.IDENT /06SE89/

;	File: [22,310]ADATIM.MAC
;       Last edit: 6-SEP-1989 16:09:52 
;	Author: Jim Bostwick 2-Nov-83 (P2 clone)
;	History:
;
;          6-Sep-89.  Philip Hannay.  Updated to use strings, with string
;            length determining output format.
;

.rem |

Procedure Adatim(VAR timstr: packed array [lo..hi:integer] of char);External;

{
*USER*

Pascal-2 procedure to return formatted system date-time string.  The format
of the string is determined by the length of the string variable.  See
PAS$EXT:GENERAL.TYP for more info on the time/date type defs.  The string
variable TIMSTR must be a type0 or type1 string.  The date/time string
will be ASSIGNED to TIMSTR, so any previous contents of TIMSTR will be
overwritten.

The following time formats are output, where the number in the parantheses
is the maximum length of the string variable.  Ie. a DEC_timestamp (CH18)
string or a STR18 string would return a format of DD-MMM-YY#HH:MM:SS.

.lit

HH:MM:SS                (8)
DD-MMM-YY               (9)
DD-MMM-YYYY             (11)
DD-MMM-YY HH:MM:SS      (18)
DD-MMM-YYYY HH:MM:SS    (20)
DD-MMM-YYYY HH:MM:SS.S  (22)

.eli

If an error occurs, the supplied string will remain unchanged.  Thus you
can detect errors if desired, by clearing the string (or intialize it
to some known value) prior to calling the ADATIM routine.  If you cleared
the string, after the ADATIM routine, you can call SLEN to see if the
string is still zero length (ADATIM error occurred).

In most cases, error checking is not needed, as there is little that
can go wrong with this routine.  However, if the string is to be used for
an important function (as a key in an indexed file for example), it would
be wise to check for errors.


*WIZARD*

No action will be done if the string supplied is not a type0 or type1
string, or if it does not conform to one of the recognized string
lengths.  The string supplied will not be changed.

The GTIM$ directive is used to retrieve the current system date and
time.  If the directive fails, no action will be done, and the supplied
string will not be changed.  In all cases, the $DSW may be examined
after the ADATIM call for the GTIM$ directive status, as no other directives
are subsequently called.

}

|

;
; Assemble with PASMAC.MAC as prefix file.
;

	.MCALL GTIM$S
	
	PROC ADATIM

	PARAM TIMSTR, ADDRESS	; Pointer to string
	PARAM TLO, INTEGER	; low conformant param
	PARAM THI, INTEGER	; hi conformant param

	VAR RAW,8.*INTEGER	; LOCAL GTIM$ OUTPUT

	SAVE <R0,R1,R2,R3>

	BEGIN
	MOV TIMSTR(SP),R0	; GET ADDRESS OF OUTPUT
	MOV THI(SP),R3		; STORE THI IN R3 AS WE USE IT A LOT
	CMP TLO(SP),#1		; SEE IF TYPE1 STRING
	BEQ 12$			; BRANCH IF TYPE1
        TST TLO(SP)		; SEE IF TYPE0 STRING
	BNE 4$			; BRANCH IF NOT TYPE0, CANNOT RETURN ANYTHING
	INC R0			; ADJUST ADDRESS FOR TYPE0 STRING
12$:	CMP R3,#8.		; VALIDATE STRING LENGTH AS ONE OF THOSE
	BEQ 3$			;    THAT WE DEAL WITH - 
        CMP R3,#9.		;    THAT IS LENGTH OF 8,9,11,18,20 OR 22
	BEQ 3$			;
	CMP R3,#11.		;
	BEQ 3$			;
	CMP R3,#18.		;
	BEQ 3$			;
	CMP R3,#20.		;
	BEQ 3$			;
	CMP R3,#22.		;
	BEQ 3$			;
	BR 4$			; SKIP IT IF NOT A RECOGNIZED LENGTH
3$:	MOV SP,R1		; GET ADDRES OF RAW IN R1
	ADD #RAW,R1
	GTIM$S R1		; GET CURRENT DATE/TIME, PUT INTO RAW
	CMP R3,#8.		; REQUESTED TIME ONLY?
	BNE 10$			; BRANCH IF NO, DO THE DATE FORMATTING
	ADD #6,R1		; SKIP OVER THE DATE WORDS IN RAW
	BR 5$			; BRANCH TO TIME FORMTTING
10$:	CMP #9.,4(R1)		; ONE-DIGIT DAY??
	BLO 1$			; BR IF NOT
	MOVB #'0,(R0)+		; STUFF LEADING ZERO IN DAYS
1$:	CALL $DAT		; SYSLIB DATE FORMATTER
	BISB #^O40,-4(R0)	; MAKE LAST CHARS OF MONTH LOWER CASE
	BISB #^O40,-5(R0)	; ....BOTH LAST CHARS
	CMP R3,#9.		; 4 DIGIT YEAR NEEDED?
	BEQ 6$			; BRANCH IF NO, STICK WITH 2 DIGIT YEAR
	CMP R3,#18.		; 4 DIGIT YEAR NEEDED?
	BEQ 6$			; BRANCH IF NO, STICK WITH 2 DIGIT YEAR
	MOVB -2(R0),(R0)+	; MOVE 2 DIGIT YEAR OVER TWO BYTES
	MOVB -2(R0),(R0)+	; MOVE 2 DIGIT YEAR OVER TWO BYTES
	CMP -6(R1),#50.		; PUT IN CENTURY - MORE THAN YEAR 50 IS 1900
	BLOS 7$			; BRANCH IF CENTURY 2000
	MOVB #'1,-4(R0)		; PUT IN "19"
	MOVB #'9,-3(R0)		;
	BR 6$			; AND ON TO TIME
7$:	MOVB #'2,-4(R0)		; PUT IN "20"
	MOVB #'0,-3(R0)		;
6$:	CMP R3,#9.		; DATE ONLY REQUESTED?
	BEQ 8$			; BRANCH IF YES, WE ARE DONE
	CMP R3,#11.		; DATE ONLY REQUESTED?
	BEQ 8$			; BRANCH IF YES, WE ARE DONE
	MOVB #^O40,(R0)+	; INSERT SPACE CHAR IN OUTPUT STRING
5$:	MOV #3,R2		; SET UP TO RETURN HH:MM:SS
	CMP R3,#22.		; TENTHS OF SECONDS REQUESTED?
	BNE 9$			; BRANCH IF NOT
	INC R2			; SET UP TO RETURN HH:MM:SS.S (R2=4)
9$:	CALL $TIM		; SYSLIB TIME FORMATTER
8$:	TST TLO(SP)		; TYPE0 STRING?
	BNE 4$			; BRANCH IF NO, NOTHING ELSE TO DO
	MOVB R3,@TIMSTR(SP)	; PUT IN LENGTH FOR TYPE0 STRING
4$:
	ENDPR
	.END

