	section	libDate,CODE
	IDNT	ALL

**	libDate
**		for producing an ascii date string suitable for inclusion in a library
**
**		Copyright ©1990 DigiSoft
**
**	Permission is granted to purchasers of Macro68 to modify this source
**	file.  Permission is further granted to distribute the source code that
**	they have modified subject to the following conditions:
**	1.  That the original, unaltered, source is included with the
**	    distribution.
**	2.  That no charge is made for the distribution, other than the cost of
**	    media.                    (Distribution via Fred Fish is permitted)
**	3.  That my copyright notice remains as part of this and any other
**	    file adapted from this file.
**                                               Paul Coward
**                                                         DigiSoft
**
**	Tabstops : 8
**	Assemble with Macro68


**	 3-07-90	** written


* NOTES:
*
*	This program was written to provide an ascii representation of the date in
* an abrievated form suitable for use in a library ID string.
*	Date format is:  DD MMM YYYY
*
*	Usage is intended to be from Macro68 :
*
*idString:
*	db	'digisoftSupport '
*	db	(VERSION+$30),'.'
*	db	(REVISIONTENS+$30)
*	db	(REVISIONUNITS+$30)
*	db	' ('
*	doscmd	'LibDate >ram:tempdate'		;write date to file on pass 1
*	incbin	'ram:tempdate'			;include date in assembly
*	execute 'delete ram:tempdate'		;delete the date file on pass 2
*	db	')',Cr,Lf,0



		OBJFILE		"ram:libDate"
	;	ERRORFILE	"ram:libDate.bugs"

	;	PALL
	;	HALL
		EXEOBJ
	;	LIST
	;	LISTSYMS
	;	ADDSYM
	;	LISTFILE	"ram:libDate.lst"
	;	CREFFILE	"ram:libDate.cref"
	;	DEBUG
	;	REALTIME
		STRICT


	ifnd	EXEC_TYPES_I
	if1
	PRINTX	Resident maclib not in use.
	endc
_AbsExecBase		equ	4
_LVOAlert		equ	-108	;exec
_LVOCloseLibrary	equ	-414	;exec
_LVOForbid		equ	-132	;exec
_LVOOpenLibrary		equ	-552	;exec
_LVOPermit		equ	-138	;exec
ds_SIZEOF		equ	3*4
AT_Recovery		equ	$00000000
AG_OpenLib		equ	$00030000
	endc


_LVOdsAllocCPremember	equ	-30
_LVOdsAllocScratch	equ	-42
_LVOdsClearRemember	equ	-66
_LVOdsDosDateStamp	equ	-774
_LVOdsDosWrite		equ	-864
_LVOdsFreeScratch	equ	-174
_LVOdsParse		equ	-216
_LVOdsStrLen		equ	-288
_LVOdsXlateBinaryDo	equ	-372


	ifnd	csd_Output
csd_Output		equ	20*4
csd_DSSBase		equ	16
AO_DSSLib		equ	$00008099	;alert number id support library can't be opened
Tab			equ	$09
Lf			equ	$0a
	endc

		setso	84
csd_DateStamp	so.b	ds_SIZEOF
csd_PrintBuffer	so.b	20
csd_SIZEOF	soval


*************************
*	COLD ENTRY	*
*************************


START:		moveq		#0,d3
		movea.l		(_AbsExecBase).w,a6	;establish exec for calls

	; --- open the support library

		moveq		#2,d0			;version #
		lea	 	(supportLibName,pc),a1	;point to liberary name
		jsr		(_LVOOpenLibrary,a6)	;exec
		tst.l		d0
		bne.b		1$
		move.l		#AT_Recovery!AG_OpenLib!AO_DSSLib,d7
		jmp		(_LVOAlert,a6)

	; --- initialize the scratch area

1$		movea.l		d0,a6
		moveq		#csd_SIZEOF,d0
		moveq		#33,d1
		moveq		#0,d2
		jsr		(_LVOdsAllocScratch,a6)
		bne		EXIT

	; --- get the current date from DOS

		lea		(csd_DateStamp,a5),a0
		move.l		a0,d1
		jsr		(_LVOdsDosDateStamp,a6)

	; --- now convert it to human readable form

; Register usage
;	D0 - days since new year 1978
;	D1 - minutes
;	D2 - ticks
;	A0 - pointer to memory to write date into

		movem.l		(a0)+,d0-d2

Xlate_Date:	addi.l		#(365*2)+1,d0	;back date to New Year 1976
		divu.w		#1461,d0	;number of 4 year groups
		move.w		d0,d3
		clr.w		d0		;clear the low word
		swap		d0
		divu.w		#365,d0		;number of years past leap year group
		tst.w		d0		;zero if leap year
		bne.b		.notLeapYear
		move.w		#29,(February)	;set february for leap year
.notLeapYear:	lsl.w		#2,d3		;* 4 for four year groups
		add.w		d0,d3
		add.w		#1976,d3	;got year
		swap		d0		;remaining days in low word
		moveq		#0,d6		;initialize the month counter
		lea		(DaysPerMonth,pc),a1
..findMonth:	cmp.w		(a1),d0
		bmi.b		SetDate		;jan
		sub.w		(a1)+,d0
		addq.b		#1,d6
		bra.b		..		;findMonth

SetDate:	andi.l		#$0000003f,d0
		cmpi.b		#9+1,d0
		bge.b		PutDay
		move.b		#' ',(a0)+
PutDay:		jsr		(_LVOdsXlateBinaryDo,a6)	;insert day
		move.b		#' ',(a0)+
		lea		(Calander,pc),a1
		lsl.l		#2,d6		;* 4
		moveq		#3-1,d5			;-1 for dbra
.copyMonth:	move.b		(0,a1,d6.w),(a0)+	;insert month
		addq.w		#1,d6
		dbra		d5,.copyMonth
		move.b		#' ',(a0)+
		exg		d3,d0			;insert year
		andi.l		#$0000ffff,d0
		jsr		(_LVOdsXlateBinaryDo,a6)
		clr.b		(a0)			;null terminate

	; --- now output it

		lea		(csd_PrintBuffer,a5),a0
		jsr		(_LVOdsStrLen,a6)
		move.l		d0,d3
		move.l		a0,d2
		move.l		(csd_Output,a5),d1
		jsr		(_LVOdsDosWrite,a6)

EXIT:		jsr		(_LVOdsFreeScratch,a6)
		movea.l		a6,a1
		movea.l		(4).w,a6
		jsr		(_LVOCloseLibrary,a6)
		move.l		d6,d0			;return the result
		rts					;exit to DOS


***************************************
***************************************
***************************************


DaysPerMonth:	dw	31
February:	dw	28
		dw	31,30,31,30,31,31,30,31,30,31


Calander:
	db	'Jan',0
	db	'Feb',0
	db	'Mar',0
	db	'Apr',0
	db	'May',0
	db	'Jun',0
	db	'Jul',0
	db	'Aug',0
	db	'Sep',0
	db	'Oct',0
	db	'Nov',0
	db	'Dec',0


supportLibName:
	cstr	'digisoftSupport.library'


*******************************************************************************

	END
