;<FOONEX>DATIME.MAC;5 18-Mar-81 17:43:14, Edit by MMCM
; Added SUMEX ERJMP/ERCAL code
;<134-TENEX>DATIME.MAC;4    18-Feb-80 16:27:41    EDIT BY PETERS
; Added bug fixes from ISI
;<134-TENEX>DATIME.MAC;3    24-Jun-79 19:54:13    EDIT BY PETERS
;<134-TENEX>DATIME.MAC;2    14-SEP-76 20:58:30    EDIT BY GEOFF
; added some lower caseing.
;<134-TENEX>DATIME.MAC;13    27-JAN-75 12:45:38    EDIT BY CLEMENTS
; LATEST EPISODE IN THE SAGA OF WHEN WE HAVE DAYLIGHT TIME.
; NOTE THAT THE CODE NOW AGREES WITH THE LAW AS OF THIS DATE, FOR ALL
; POSSIBLE TIME INPUTS. THAT IS, THIS CODE KNOWS ALL THE OLD
; GLITCHES AND THE FUTURE ONES, AND CAN THUS BE INSTALLED AT ALL SITES.
; FURTHER CHANGE IS NEEDED IF CONGRESS DECIDES TO KEEP THE START
; OF SUMMER DST IN FEBRUARY. LAW NOW SAYS WE GO BACK TO APRIL IN '76.
; (ACTUALLY IT DOESN'T KNOW ABOUT WAR TIME" IN WW II, BUT
; OTHERWISE, IT'S RIGHT....)
;<133-TENEX>DATIME.MAC;12    26-OCT-74 12:48:46    EDIT BY CLEMENTS
; FIRST PART OF FIX FOR RETURN OF STANDARD TIME. FEBRUARY VS APRIL 
;  STILL NEEDS TO BE FIXED.
;<TENEX-132>DATIME.MAC;11    12-MAR-74 13:01:19	EDIT BY CLEMENTS
;FIX FOR FEB 29 IN NON-LEAP YEAR RESULTING FROM DAYLIGHT SAVINGS TIME
;<TENEX-132>DATIME.MAC;10    11-DEC-73 15:55:38	EDIT BY CLEMENTS
; FIXES TO DAYLIGHT SAVINGS TIME: 1) MOVE TIME OF SWITCH
;  BACK FROM 4 AM TO 2 AM, AS LAW READS. 2) INCLUDE FORCED
;  DAYLIGHT SAVINGS TIME FROM 6 JAN 74 THRU SUMMER OF 75.
;<TENEX-132>DATIME.MAC;9    16-MAY-73 19:07:29	EDIT BY CLEMENTS
; DEFAULT -1 TO ODTIM NOW IMPLIES TIMEZONE (B13)
;<TENEX-130>DATIME.MAC;8    28-DEC-72 15:49:59	EDIT BY CLEMENTS
; CHANGED HANDLING OF MIDNITE AND NOON TO BE ALGEBRAIC
;<TENEX-130>DATIME.MAC;7     7-DEC-72 14:06:16	EDIT BY TOMLINSON
; FIXED IDTNCS B0 _ B1 BUG
;<TENEX-130>DATIME.MAC;6    30-NOV-72 14:35:45	EDIT BY TOMLINSON
;<TENEX-130>DATIME.MAC;5     2-NOV-72 13:36:10	EDIT BY TOMLINSON
;<TENEXDLM>DATIME.MAC;4    26-OCT-72 11:10:01	EDIT BY TOMLINSON
;22 NOV 71, 1847:

;DATIME.MAC
;TIME AND DATE JSYS ROUTINES

; JRB  BBN  SEPT 1970


;JSYS'S HEREIN
; ODTIM
; IDTIM
; ODTNC
; IDTNC
; ODCNV
; IDCNV


;ASSEMBLE WITH STENEX.MAC


	SEARCH	STENEX,PROLOG
	TITLE DATIME

	USE	SWAPPC

	INTERN TIMZON	;CELL TO PATCH TO RUN IN ANOTHER TIME ZONE
	INTERN .ODTIM,.IDTIM,.ODTNC,.IDTNC,.ODCNV,.IDCNV	;JSYS'S
	;INTERN ODTIMS,IDTIMS,ODTNCS,IDTNCS	;SUBR ENTRIES TO JSYS'S

	EXTERN LSTERR,ITRAP,MENTR,MRETN,MRTNE1

	MLON

EXTERN BIN1	;RST'S ROUTINE TO INPUT BYTE TO 2, SOURCE IN CALLER 1
EXTERN BOUTA	;OUTPUT LIKEWISE
EXTERN NOUTXX	;NUMBER OUTPUT LIKEWISE

;PARAMETERS

TIMZON:	TMZONE	;NUMBER OF HOURS LOCAL TIME LAGS GREENWICH
DWFUDG==2	;CONSTANT TO NORMALIZE DAY OF WEEK
		; = DAY OF WEEK OF 17 NOV 1858


;ACCUMULATORS
Z=0
A=1
B=2
C=3
D=4
E=5
F=6
G=7
AA=10
BB=11
CC=12
DD=13
EE=14
FF=15
GG=16
P=17

OPDEF CALL [PUSHJ P,]
OPDEF RET [POPJ P,]

;HALF-WORD BIT SYMBOLS
B0==1B18
B1==1B19
B2==1B20
B3==1B21
B4==1B22
B5==1B23
B6==1B24
B7==1B25
B8==1B26
B9==1B27
B10==1B28
B11==1B29
B12==1B30
B13==1B31
B14==1B32
B15==1B33
B16==1B34
B17==1B35

REPEAT 0, <

ODTIM

	OUTPUT DATE AND TIME

	CONVERTS INTERNAL DATE AND TIME TO TEXT

TAKES:	1: TENEX DESTINATION DESIGNATOR
	2: LEFT HALF: INTERNAL DATE
	   RIGHT HALF: INTERNAL TIME
	   OR ENTIRE WORD -1 TO USE CURRENT DATE AND TIME
	3: FORMAT FLAGS, DESCRIBED BELOW

RETURNS: +1 ALWAYS: UPDATED STRING POINTER (IF PERTINENT) IN 1.

	GENERATES ILLEGAL INSTRUCTION PSEUDO-INTERRUPT ON
	ERROR CONDITIONS GIVEN BELOW.

ODTIM FORMAT FLAGS (AC 3):

    B0: OFF, OUTPUT DATE
	ON, OMIT DATE AND IGNORE B1-B8
    B1: ON, OUTPUT DAY OF WEEK
	OFF, OMIT DAY OF WEEK AND IGNORE B4
    B2: ON, USE FULL TEXT FOR WEEKDAY
	OFF, USE 3-LETTER ABBREVIATION FOR WEEKDAY
    B3: ON, OUTPUT MONTH AS NUMBER AND IGNORE B4
	OFF, OUTPUT MONTH AS TEXT
    B4: ON, USE FULL TEXT FOR MONTH
	OFF, USE 3-LETTER ABBREVIATION FOR MONTH
    B5: ON, OUTPUT 4-DIGIT YEAR
	OFF, OUTPUT 2-DIGIT YEAR IF BETWEEN 1900 AND 1999
    B6: ON, OUTPUT DAY OF MONTH AFTER MONTH
	OFF, OUTPUT DAY OF MONTH BEFORE MONTH
    B7 AND B8: DATE PUNCTUATION CONTROL:
	BOTH OFF, PUNCTUATE WITH DASHES, AS 13-APR-70
	B7 ON, PUNCTUATE WITH SPACES, AS 13 APR 70.
  	       A COMMA IS ALSO USED IF B6 IS ON, AS APRIL 13, 1970
	B8 ON, PUNCTUATE WITH SLASHES, AS 5/13/70
	BOTH B7 AND B8 ON, UNSPECIFIED
    B9: OFF, OUTPUT TIME
	ON, OMIT TIME AND IGNORE B10-B13
   B10: OFF, INCLUDE SECONDS, PRECEDED BY A COLON
	ON, OMIT SECONDS
   B11: OFF, USE 24-HOUR TIME
	ON, USE 12-HOUR TIME AND AM-PM
   B12: OFF, COLON BETWEEN HOURS AND MINUTES
	ON, NO COLON
   B13: OFF, NO TIME ZONE GARBAGE
	ON, ADD "-" AND TIME ZONE TO TIME, EG "-EDT" OR "-PST"
   B17: OFF, COLUMNATE: USE LEADING SPACES AND ZEROES SO THAT
	TTME AND DATE PRINTOUT IS CONSTANT-WIDTH REGARDLESS OF THE
	PARTICULAR DATE AND TIME (NOTE: FULL MONTH AND WEEKDAY TEXTS
	ARE NOT COLUMNATED).
	ON, DON'T COLUMNATE.  
	NOTE: COLUMNATION WILL GENERALLY BE DESIRED IF THE PRINTOUT
	IS PART OF A TABLE, BUT NO COLUMNATION PRODUCES A
	SLIGHTLY BETTER LOOKING PRINTOUT WHEN PART OF A SENTENCE.

FOR CONVENIENCE, -1 IN 3 IS TAKEN AS EQUIVALENT TO 336021000000,
WHICH PRODUCES, FOR EXAMPLE, FRIDAY, APRIL 13, 1970 1:06:03-EST .

EXAMPLES:
    CONTENTS OF 3	    TYPICAL TEXT
	0			 3-APR-70  1:06:03
	202201000000		FRI 3 APR 70 1:06
	336321000000		FRIDAY, APRIL 3, 1970 1:06AM-EST
	041041000000		3/4/70 106:03
	041040000000		 3/04/70  106:03

ODTIM ERROR MNEMONICS:

STIMEX:	SYSTEM HAS NO DATE AND TIME
TIMEX1:	ILLEGAL TIME (GREATER THAN 24 HOURS)
ODCNV ERRORS (LIST THEM ALL HERE)
ODTNC ERRORS
FILE ERRORS
DESXN ERRORS

>




;NOTE: JSYS'S USING STRING I/O CANNOT BE NESTED BECAUSE STRING
;POINTER WOULD ALWAYS REFER TO MONITOR ADDR SPACE EVEN IF OUTER JSYS
;CALLED FROM USER SPACE.
;HENCE SUBROUTINE ENTRIES ARE USED FOR ODTNC AND IDTNC WHEN USED
;IN ODTIM AND IDTIM, AND SUBROUTINE ENTRIES ARE ALSO PROVIDED
;TO ODTIM AND IDTIM FOR USE ELSEWHERE IN THE MONITOR.

;ODTIM JSYS

.ODTIM:	JSYS MENTR	;BECOME SLOW, ETC.
	CALL ODTIMS	;ODTIM SUBROUTINE
	JRST MRETN	;RETURN TO CALLER

;ODTIM SUBROUTINE, FOR ODTIM JSYS
; AND POSSIBLE OTHER FUTURE USES IN MONITOR INVOLVING STRING
; IN USER SPACE.
;TAKES: CALLER'S A: DESTINATION
;	ACTUAL B: DATE & TIME OR -1
;	ACTUAL C: FORMAT OPTION FLAGS
;RETURNS +1: WITH CALLERS A UPDATED (IF STRING POINTER)
;CLOBBERS MOST OR ALL OF AC'S B-DD.

ODTIMS:	PUSH P,C		;SAVE FORMAT OPTION FLAGS
	SETZ D,		;DEFUALT ZONE & DAYLIGHT STUFF
	ODCNV		;CONVERT INTERNAL TO LOCAL DATE & TIME
	POP P,F		;GET FORMAT OPTION FLAGS BACK INTO F
	CALL ODTNCS	;SUBR ENTRY TO ODCNV JSYS (BELOW)
			;CONVERTS LOCAL DATE & TIME TO STRING
	RET		;RETURN

REPEAT 0, <

IDTIM

	INPUT DATE AND TIME

	CONVERTS TEXT TO INTERNAL DATE AND TIME

TAKES: 1: TENEX SOURCE DESIGNATOR
       2: FORMAT OPTION FLAGS, DESCRIBED BELOW, 0 FOR NORMAL CASE

RETURNS: +1: FAIL, ERROR NUMBER IN 2,
		   UPDATED STRING PTR (IF PERTINENT) IN 1
	 +2: SUCCESS, 1: UPDATED STRING PTR (IF PERTINENT)
		      2: LH: DATE IN SYSTEM INTERNAL FORMAT (GREENWICH)
			 RH: TIME IN SYSTEM INTERNAL FORMAT
			     (GREENWICH MEAN TIME)

IDTIM DOES NOT ADMIT OF DOING CONVERSIONS FOR OTHER THAN THE
LOCAL TIME ZONE OR FOR DAYLIGHT SAVINGS TIME IF APPROPRIATE, EXCEPT IF
THE INPUT STRING CONTAINS A TIME ZONE CODE.  HOWEVER, THE CALLER MAY
ADD OR SUBTRACT HOURS TO ADJUST TO OTHER TIME ZONES; HE MAY
DETERMINE THE LOCAL TIME ZONE USING "ODCNV" (BELOW).

IDTIM DOES NOT ADMIT OF INPUTTING EITHER THE DATE OR THE TIME ALONE.
SEE IDTNC, BELOW, FOR THIS.

IDTIM FORMAT OPTIONS (AC 2):

    B1: ON, DISALLOW NUMERIC MONTH IN INPUT AND IGNORE B2-B3
    B2: OFF, IF MONTH IS NUMERIC, AS IN "1/2/70", CONSIDER
	     THE FIRST NUMBER TO BE THE MONTH
	ON, CONSIDER THE SECOND NUMBER TO BE THE MONTH. IE "1/2/70"
	    MEANS 1 FEB NOT JAN 2, AS IS CUSTOMARY IN ENGLAND.
    B3: OFF, IF AN INVALID DATE CAN BE INTERPRETED SUCESSFULLY BY
             ASSUMING MONTH AND DAY ARE IN OPPOSITE ORDER THAN
             THAT SPECIFIED BY B2, INTERPRET IT THUS.
	     THIS WOULD APPLY, FOR EXAMPLE, TO "31/1/70" WITH B2 OFF.
	ON, GIVE AN ERROR RETURN FOR SUCH DATES.
    B7 AND B8 GOVERN INCLUSION OF SECONDS (PRECEDED BY COLON)
	      IN TIME.
              BOTH OFF, SECONDS ARE OPTIONAL.
              B7 ON, SECONDS ILLEGAL.
              B8 ON, SECONDS MANDATORY.
    B9 AND B10 GOVERN USE OF COLON BETWEEN HOURS AND MINUTES.
	       BOTH OFF, OPTIONAL
	       B9 ON, ILLEGAL.
	       B10 ON, MANDATORY.
    B11 AND B12: WHEN BITS 7 THRU 10 ARE ALL OFF, THESE
		 BITS DETERMINE WHETHER A TIME WITH ONE COLON IS
		 INTERPRETED AS HOURS AND MINUTES OR HOURS, MINUTES,
		 AND SECONDS WITH NO COLON BETWEEN THE FORMER.
		 BOTH OFF, TAKE AS HH:MOVEM IF FIRST FIELD IS SMALL ENOUGH,
			   ELSE AS HHMM:SS.
		 B11 ON, ALWAYS TAKE AS HHMM:SS.
		 B12 ON, ALWAYS TAKE AS HH:MOVEM (GIVING AN ERROR IF
			 FIRST FIELD IS TOO BIG). THIS DIFFERS FROM
			 B7 ON IN THAT SECONDS CAN BE INCLUDED IF 
			 PRECEDED BY A SECOND COLON.
   B14: ON TO DISALLOW 24-HOUR TIME AND MAKE AM-PM MANDATORY
   B15: ON TO DISALLOW AM-PM
   B16: ON TO DISALLOW TIME ZONE


IDTIM ERROR MNEMONICS

IDTNC ERRORS
IDCNV ERRORS
FILE ERRORS
DESXN ERRORS

>

;IDTIM JSYS

.IDTIM:	JSYS MENTR
	CALL IDTIMS	;IDTIM SUBROUTINE
	 JRST [	UMOVEM B,B	;ERROR. RETURN CODE IN B
		jrst mrtne1]	; Error return +1.
	UMOVEM B,B	;RETURN INTERNAL DATE & TIME IN B
	AOS (P)		;RETURN +2
	JRST MRETN

;IDTIM SUBROUTINE, FOR IDTIM JSYS AND POSSIBLE OTHER USES 
; (SEE NOTES WITH ODTIM)
;TAKES: CALLERS A: SOURCE
;	ACTUAL B: FORMAT OPTION FLAGS
;RETURNS +1: ERROR CODE IN ACTUAL B, CALLERS A UPDATED
;	 +2: DATE & TIME IN ACTUAL B, CALLERS A UPDATED
;CLOBBERS C-DD.

IDTIMS:	CALL IDTNCS	;SUBR ENTRY TO IDCNV JSYS (BELOW)
			;INPUTS DATE & TIME AND RETURNS AS LOCAL NUMBERS
	 RET		;ERROR. # IN B.
	IDCNV		;CONVERT LOCAL TO INTERNAL
	RET		;ERROR, # IN B.
	AOS (P)
	RET

;ODTNC
;	OUTPUT DATE AND/OR TIME WITHOUT CONVERSION FROM INTERNAL

;A SUBSET OF ODTIM, USED INTERNALLY BY ODTIM.

;TAKES:	1: TENEX DESTINATION DESIGNATOR
;	2: LH: REAL YEAR
;	   RH: MONTH (JANUARY=0)
;	3: LH: DAY OF MONTH
;	   RH: DAY OF WEEK IF PRINTOUT THEREOF REQUESTED
;	4: LH: NEEDED ONLY IF TIME ZONE PRINTOUT REQUESTED:
;	       B1: ON FOR DAYLIGHT SAVINGS
;	       B12-17: TIME ZONE
;	   RH: LOCAL TIME (SECONDS SINCE MIDNITE)
;	5: FORMAT OPTION FLAGS, SAME AS FOR ODTIM (ABOVE).
;	   NOTE: THE ONLY TIME ZONES WHICH CAN BE PRINTED (FLAG B13)
;	   ARE THOSE IN THE USA AND GREENWICH.

;RETURNS +1 WITH UPDATED STRING POINTER (IF PERTINENT) IN 1.
;TRAPS ON ERRORS.

;ERROR MNEMONICS

;DATEX1: ILLEGAL YEAR
;DATEX2: MONTH TOO LARGE
;DATEX3: DAY OF MONTH TOO LARGE
;DATEX4: DAY OF WEEK TOO LARGE
;ZONEX1: ILLEGAL TIME ZONE VALUE (NOT BETWEEN -12 AND 12)
;ODTNX1: UNPRINTABLE TIME ZONE
;FILE ERRORS
;DESXN ERRORS


;ODTNC JSYS

.ODTNC:	JSYS MENTR
	UMOVE F,E
	CALL ODTNCS	;CALL ODTNC SUBROUTINE (NEXT IN LISTING)
	JRST MRETN

;ODTNCS
;ODTNC SUBROUTINE FOR ODTNC AND ODTIM JSYS'S

;CALL: ALL AC'S ARE ACTUAL EXCEPT THOSE NOTED AS "CALLER" WHICH ARE
;	USER IF LAST JSYS WAS FROM USER SPACE.
;TAKES:	CALLER A: DESTINATION
;	B: YEAR,,MONTH
;	C: MONTHDAY,,WEEKDAY
;	D: TIME STUFF
;	F: FORMAT OPTION FLAGS
;RETURNS +1: ON SUCCESS WITH CALLER A UPDATED,
;	B-E,G,AA,BB,DD CLOBBERED
;TRAPS ON ERRORS

;INTERNAL AC USE
;	D,E,AA:	ARGS FROM B,C,D
;	BB: LOCAL TIME FROM RH D, LH 0.

ODTNCS:	MOVE DD,P		;SAVE PD LEVEL TO RESTORE BEFORE ERROR RETURN
	CAMN F,[-1]
	HRLZI F,336021	; DEFAULT FORMAT FOR -1
	MOVE AA,D		;TIME STUFF TO AA
	MOVEI BB,(D)	;TIME ONLY, WITH LH 0, TO BB
	CALL CKYMDT	;CHECK MOST ARGUMENTS. THIS SUBR IS WITH
			;"IDCNV" BELOW. USES B,C,AA.
	 JRST ODTNCE	;BAD ARGUMENT. ERROR CODE IN B.
	MOVE D,B		;LOCAL YEAR,,LOCAL MONTH TO D.
	MOVE E,C		;LOCAL DAY OF MONTH,,DAY OF WEEK TO E.
	MOVEI G,^D10	;SET G FOR USE IN C FOR MOST NOUTS: DECIMAL,
	TLNN F,B17	;COLUMNATION SUPPRESSED?
	HRLI G,B2+2	;NO, 2 COLUMNS, LEADING SPACE.
	TLNE F,B0	;ARE WE GOING TO PRINT DATE?
	JRST OTT1		;NO, DATE SUPPRESSION REQUESTED.

;ODTNCS...
;DATE
	TLNN F,B1
	JRST ODB2
;DAY OF WEEK
	HRRZ B,E
	CAIL B,7
	JRST [	MOVEI B,DATEX4	;"DAY OF WEEK TOO LARGE"
		JRST ODTNCE]
	HRROI B,[ASCIZ /Mon/
		ASCIZ /Tue/
		ASCIZ /Wed/
		ASCIZ /Thu/
		ASCIZ /Fri/
		ASCIZ /Sat/
		ASCIZ /Sun/](E)
	TLNE F,B2	;ON FOR VERBOSE
	HRRO B,[[ASCIZ /Monday,/]
		[ASCIZ /Tuesday,/]
		[ASCIZ /Wednesday,/]
		[ASCIZ /Thursday,/]
		[ASCIZ /Friday,/]
		[ASCIZ /Saturday,/]
		[ASCIZ /Sunday,/] ] (E)
	CALL SOUTA
	MOVEI B," "	;ALWAYS A SPACE AFTER WEEKDAY
	CALL BOUTA

;ODTNCS...
;DAY # IF BEFORE MONTH
ODB2:	TLNE F,B6
	JRST ODB3
	HLRZ B,E
	ADDI B,1	;REAL DAY IS ARG +1.
	MOVE C,G
	CALL NOUTXX
;SEPERATOR
	CALL ODTMSP	;DASH, SPACE, OR SLASH, DEPENDING ON FLAGS.
;MONTH
ODB3:	TLNN F,B3
	JRST ODB3A
;MONTH AS NUMBER
	HRRZ B,D
	ADDI B,1
	MOVE C,G		;NOUT FORMAT
	TLNN F,B6	;IF DAY # WAS FIRST,
	TLO C,B3	;USE LEADING 0 NOT SPACE FOR MONTH
	CALL NOUTXX	;MONTH
	JRST ODB3B
;MONTH AS CONCISE OR VERBOSE TEXT
ODB3A:	HRROI B,[ASCIZ /Jan/
		ASCIZ /Feb/
		ASCIZ /Mar/
		ASCIZ /Apr/
		ASCIZ /May/
		ASCIZ /Jun/
		ASCIZ /Jul/
		ASCIZ /Aug/
		ASCIZ /Sep/
		ASCIZ /Oct/
		ASCIZ /Nov/
		ASCIZ /Dec/ ] (D)
	TLNE F,B4	;ON FOR VERBOSE
	HRRO B,[[ASCIZ /January/]
		[ASCIZ /February/]
		[ASCIZ /March/]
		[ASCIZ /April/]
		[ASCIZ /May/]
		[ASCIZ /June/]
		[ASCIZ /July/]
		[ASCIZ /August/]
		[ASCIZ /September/]
		[ASCIZ /October/]
		[ASCIZ /November/]
		[ASCIZ /December/] ] (D)
	CALL SOUTA
;SEPERATOR
ODB3B:	CALL ODTMSP	;DASH, SLASH, OR SPACE PER FLAGS

;ODTNCS...
;DAY OF MONTH IF AFTER MONTH
	TLNN F,B6
	JRST ODB5
	HLRZ B,E
	ADDI B,1
	MOVE C,G
	TLNE F,B3	;IF MONTH WAS NUMBER,
	TLO C,B3	;USE LEADING 0 NOT SPACE
	CALL NOUTXX
;SEPERATOR: FIRST A COMMA IF MONTH WAS VERBOSE TEXT
	TLNN F,B3
	TLNN F,B4
	JRST .+3
	MOVEI B,","
	CALL BOUTA
	CALL ODTMSP	;DASH, SLASH, OR SPACE PER FLAGS
;YEAR
ODB5:	HLRZ B,D
	TLNE F,B5
	JRST ODB5A		;VERBOSE YEAR REQUESTED
	IDIVI B,^D100	;SEPERATE CENTURY AND YEAR WITHIN CENTURY
	EXCH B,C	;CONCISE YEAR: LAST 2 DIGITS ONLY
	CAIE C,^D19
	HLRZ B,D	;MAKE YEAR ALWAYS VERBOSE IF NOT 19XX
ODB5A:	MOVE C,[1B2+1B3+2B17+^D10]	;NOUT FORMAT: 2 COLS, LEADING 0S.
	CAIL B,^D100
	HRLI C,B2+B3+4	;BUT USE 4 COLS IF VERBOSE
	CALL NOUTXX
;SPACE BETWEEN DATE AND TIME,
;NOT PRINTED IF EITHER IS SUPPRESSED.
	TLNE F,B9
	JRST OTT1
	MOVEI B," "
	CALL BOUTA

;ODTNCS...
OTT1:	TLNE F,B9
	JRST ODT8		;TIME SUPPRESSED
;TIME
	MOVEI D,(BB)	;MASK RH: LOCAL TIME IN MIUTES SINCE MIDNITE
	IDIVI D,^D3600
	MOVE B,D
	TLNN F,B11	;PRINTING 12-HOUR TIME?
	JRST OTT2		;NO
	CAIGE B,1	;12-HOUR PRINTOUT IS ALWAYS BETWEEN 1:00:00
	ADDI B,^D12	;...AND 12:59:59. SO ADD 12 IF BEFORE 1:00:00
	CAILE B,^D12
	SUBI B,^D12	;SUBTRACT 12 HOURS IF AFTER 12:59:59
OTT2:	MOVE C,G		;NOUT FORMAT SET UP AT ENTRY TO ODTIM.
	TLNN F,B11		;PRINTING AM/PM?
	TLO C,B2+B3+2		;NO. SO USE LEADING 0 ON HOURS TOO
	CALL NOUTXX	;PRINT HOURS
	MOVEI B,":"
	TLNN F,B12	;COLON REQUESTED BETWEEN HOURS AND MINUTES?
	CALL BOUTA	;YES
	MOVE D,E
	IDIVI D,^D60
	MOVE B,D
	TLO C,B2+B3+2	;ALWAYS LEADING 0 ON MINUTE AND SECONDS
	CALL NOUTXX	;PRINT MINUTES
	TLNE F,B10
	JRST OTT4		;SECONDS SUPPRESSED
	MOVEI B,":"
	CALL BOUTA	;SECONDS ARE ALWAYS PRECEDED BY COLON
	MOVE B,E
	CALL NOUTXX	;SECONDS

;ODTNCS...
OTT4:	TLNN F,B11	;12-HOUR TIME REQUESTED?
	JRST OTT5		;NO
	HRROI B,[ASCIZ /AM/]	;YES, PRINT AM, PM, N, OR M.
	CAIL BB,^D<12*3600>
	HRROI B,[ASCIZ /PM/]
	CAIN BB,^D<12*3600>
	HRROI B,[ASCIZ /N/]	;"N" FOR NOON
	CAIN BB,0
	HRROI B,[ASCIZ /M/]	;"M" FOR MIDNITE
	CALL SOUTA
OTT5:	TLNN F,B13	;PRINTING TIME ZONE REQUESTED?
	JRST OTT6		;NO
	LDB E,[POINT 6,AA,17]	;TIME ZONE
	JUMPE E,[HRROI B,[ASCIZ /-GMT/]
		TLNE AA,B1	;B1 OF ARG FROM D ON FOR DAYLIGHT
		HRROI B,[ASCIZ /-GDT/]	;"GREENWICH DAYLIGHT TIME" !?
		JRST OTT5A]
	CAIN E,^D10		;IS IT HST?
	 JRST [	HRROI B,[ASCIZ /-HST/]
		TLNE AA,B1
		HRROI B,[ASCIZ /-HDT/]
		JRST OTT5A ]
	CAIG E,^D8	;IS TIME ZONE IN RANGE FOR WHICH WE HAVE TEXTS?
	CAIGE E,5
	JRST [	MOVEI B,ODTNX1	;"UNPRINTABLE TIME ZONE"
		JRST ODTNCE]
	HRROI B,[ASCIZ /-EST/
		ASCIZ /-CST/
		ASCIZ /-MST/
		ASCIZ /-PST/ ] -5 (E)	;PICK UP STANDARD TIME ZONE TEXT
	TLNE AA,B1	;DAYLIGHT SAVINGS IN EFFECT FOR THIS DATE?
	HRROI B,[ASCIZ /-EDT/
		ASCIZ /-CDT/
		ASCIZ /-MDT/
		ASCIZ /-PDT/] -5 (E)	;GET DAYLIGHT TEXT
OTT5A:	CALL SOUTA
OTT6:	TLNE F,B11	;12-HOUR TIME REQUESTED?
	TLNE F,B17	;COLUMNATION SUPPRESSED?
	JRST OTT9		;NO OR YES
	;IF N FOR NOON OR M FOR MIDNITE PRINTED WITH COLUMNATION,
	;PRINT SPACE AFTER TIME ZONE, BECAUSE SPACE BEFORE TIME ZONE
	;IS A FORMAT THAT CAN'T BE TYPED INTO "IDTIM".
	MOVEI B," "
	CAIE BB,^D<12*3600>
	CAIN BB,0
	CALL BOUTA
OTT9:

;ODTNCS...
;EXIT
ODT8:	RET

ODTNCC:	MOVE B,C		;HERE WHEN ERROR CODE IS IN C, AS AFTER "NOUT"
;ODTNC ERROR RETURN.  ERROR CODE IN B.
ODTNCE:	MOVEM B,LSTERR	;SAVE ERROR CODE IN PSB FOR "GETER"
	MOVE P,DD		;CLEAR PD
	JRST ITRAP


;SUBROUTINE TO OUTPUT APPROPRIATE SEPERATOR PER FLAGS 7 AND 8.
ODTMSP:	MOVEI B,"-"
	TLNE F,B7
	MOVEI B," "
	TLNE F,B8
	MOVEI B,"/"
	JRST BOUTA

;SOUTA
;MONITOR TO CALLER'S ADDRESS SPACE STRING OUTPUT SUBROUTINE
;FOR ODTNCS
;B POINTS TO SOURCE STRING; LH -1 => 440700
;CALLERS A IS DESTINATION. BOTH ARE UPDATED.
SOUTA:	PUSH P,C
	HLRZ C,B
	CAIN C,-1
	HRLI B,<POINT 7,0,-1>B53
	MOVE C,B
SOUTA1:	ILDB B,C
	JUMPE B,[MOVE B,C
		POP P,C
		RET]
	CALL BOUTA
	JRST SOUTA1

REPEAT 0, <

IDTNC

	INPUT DATE AND/OR TIME WITHOUT CONVERSION TO INTERNAL

THIS JSYS IS A SUBSET OF IDTIM AND WILL ONLY BE OF INTEREST TO THOSE
WHO WISH TO INPUT THE DATE AND TIME WITHOUT CONVERTING TO
INTERNAL, OR WHO WISH TO INPUT THE TWO QUANTITIES SEPERATELY, EG
BECAUSE OF INTERVENING TYPEOUT.  (IDTIM CANNOT iNPUT EITHER
ALONE BECAUSE IT IS IMPOSSIBLE TO CONVERT ONE TO INTERNAL WITHOUT
THE OTHER.)  IDTNC RETURNS VALUES SUITABLE FOR USE AS ARGUMENTS TO
IDCNV.  IDTNC IS USED INTERNALLY BY IDTIM.

TAKES: 1: TENEX SOURCE DESIGNATOR
       2: FORMAT OPTION FLAGS, DESCRIBED BELOW

RETURNS: +1: FAILURE, ERROR CODE IN 1,
		      UPDATED STRING POINTER (IF PERTINENT) IN 4
	 +2: SUCCESS, VALUES IN ACCUMULATORS:
		1: UPDATED STRING POINTER IF PERTINENT
	        IF DATE INPUT WAS NOT SUPPRESSED:
		   2: LH: REAL YEAR
		      RH: MONTH (JANUARY=0)
		   3: LH: DAY OF MONTH (FIRST DAY=0)
		      RH: DAY OF WEEK (MONDAY=0)
		IF TIME INPUT WAS NOT SUPPRESSED:
		   4: RH: TIME AS SECONDS SINCE MIDNIGHT
		      LH: B0: OFF UNLESS A TIME ZONE WAS INPUT
		          B1: ON IF A DAYLIGHT SAVING TIME ZONE
			      (EG EDT) WAS INPUT.
		          B2: OFF UNLESS A TIME ZONE WAS INPUT
		          B12-17: TIME ZONE, IF ONE WAS INPUT, ELSE
			          LOCAL TIME ZONE.
			          (SEE DESCRIPTION OF TIME ZONeS UNDER 
			          IDCNV)
			  (REDUNDANT BITS B0 AND B2 ARE FOR
			  COMPATIBILITY WITH ODCNV)

IDTNC FORMAT OPTION FLAGS (AC 2):

    ALL OF THOSE DESCRIBED ABOVE UNDER IDTIM APPLY, AND ALSO:

    B0 ON, DON'T INPUT DATE, IGNORE B1-B3
    B6 ON, DON'T INPUt TIME, IGNORE B8-B16

IDTNC ERROR MNEMONICS:

DILFX1: ILLEGAL FORMAT FOR DATE
TILFX1: ILLEGAL FORMAT FOR TIME
NOTE: IDTNC DOES NOT DETECT CERTAIN ERRORS IN DATE INPUT, SUCH AS
DAY 31 OF A 30-DAY MONTH.  SUCH ERRORS ARE DETECTED IN IDCNV.
>

;IDTNC JSYS

.IDTNC:	JSYS MENTR
	CALL IDTNCS	;IDTNC SUBROUTINE, NEXT
	 JRST [	UMOVEM B,B	;ERROR, RETURN CODE IN CALLER'S B.
		jrst mrtne1]	; Handle error, return +1
	UMOVEM B,B	;RETURN RESULTS
	UMOVEM C,C
	UMOVEM D,D
	AOS (P)		;SKIP
	JRST MRETN


;IDTNCS
;IDTNC SUBROUTINE, FOR IDTIM AND IDTNC JSYS'S

;CALL SAME AS JSYS EXCEPT ALL AC'S ACTUAL EXCEPT A IS CALLER
;	(IE CALLER OF JSYS THAT CALLS SUBROUTINE)
;CLOBBERS B-DD.

;IDTNC INTERNAL AC USE
;	A: SOURCE DESIGNATOR
;	B,C,D,E: TEMPS
;	F: FLAGS
;	G: LAST CHARACTER READ
;	AA, BB, CC: WHAT WILL BE RETURNED IN B, C, D
;	DD: SAVED P

;IDTNCS
IDTNCS:	MOVE DD,P		;SAVE P: USED WHEN MAKING AN ERROR RETURN
	MOVE F,B		;FLAGS REMAIN IN F THROUGHOUT
	MOVEI G," "	;G CONTAINS CHARACTER: PRESET TO SPACE SO
			;"GNSC" SUBR WILL READ FIRST CHARACTER
	TLNE F,B0	;ON TO SUPPRESS DATE INPUT
	JRST ITN1		;DATE INPUT SUPPRESSED

;INPUT DATE

	CALL MONTH	;TRY INPUTTING ALPHA MONTH
	JRST IDN2		;NOT A LETTER, GO TRY NUMBER
;PROCESS REST OF DATE WHICH BEGINS WITH ALPHABETIC MONTH
	CALL DNIN		;INPUT NUMBER FOR DAY OF MONTH -1 INTO B
	HRL BB,B	;RETURN DAY OF MONTH IN LH C
	CALL GNSC		;NON-SPACE CHARACTER
	CAIN G,","	;COMMA ALLOWED IN THIS CONTEXT ONLY
	JRST [	CALL IDCHAR	;PASS THE COMMA. "YEAR" PASSES SPACES.
		JRST .+2]
	CALL IDSEP	;SEPERATING DASH OR SLASH AND SPACES
	CALL YEAR		;INPUT YEAR
	JRST IDN7
;DATE MUST BEGIN WITH A NUMBER
IDN2:	CALL DNIN		;READ NUMBER: VALUE-1 TO B
	HRL BB,B	;STORE VALUE IN DAY OF MONTH
	CALL IDSEP	;SEPERATING DASH OR SLASH AND SPACES
	CALL MONTH	;ALPHANUMERIC MONTH NEXT?
	JRST IDN3		;NO, GO TRY NUMBER
	CALL YEAR		;INPUT YEAR
	JRST IDN7

;IDTNCS...
;SECOND FIELD OF DATE MUST ALSO BE A NUMBER
IDN3:	TLNE F,B1	;NUMERIC MONTH ALLOWED?
	JRST IDILF		;NO, "ILLEGAL FORMAT" ERROR
	CALL DNIN		;GET SECOND NUMBER -1 INTO B.
	HLRZ C,BB	;FIRST NUMBER TO C
	TLNE F,B2	;IS FIRST NUMBER SUPPOSED TO BE MONTH?
	EXCH C,B	;NO, SWAP. MONTH TO C, DAY TO B.
	CAIG C,^D11	;IS "MONTH" TOO LARGE
	JRST IDN4		;OK
	CAIG B,^D11	;IS OTHER NUMBER ALSO TO LARGE TO BE MONTH?
	TLNE F,B3	;WILL CALLER LET US SWAP THEM?
	JRST IDILF		;YES OR NO, ERROR.
	EXCH B,C
IDN4:	HRR AA,C	;RETURN MONTH IN RH B
	HRL BB,B	;RETURN DAY IN RH C
	CALL IDSEP	;SEPERATING SPACES, DASH OR SLASH, SPACES.
	CALL YEAR		;GET YEAR
IDN7:	TLNE F,B6	;ON TO SUPPRESS TIME INPUT
	JRST IDTNC8	;TIME INPUT SUPPRESSED
;SEPERATOR BETWEEN DATE AND TIME: ONE TAB, ANY NUMBER OF SPACES.
	CALL GNSC
	CAIN G,11	;ASCII TAB
	CALL IDCHAR

;IDTNCS...
;INPUT TIME
ITN1:	TLNE F,B6
	JRST IDTNC8	;BOTH TIME AND DATE SUPPRESSED, JSYS IS NOP.
	SETZ E,		;ZERO FOR SECONDS IF OMITTED
	CALL IDNIN	;PASS SPACES, READ NUMBER
	 JRST ITILF	;NO NUMBER, "ILLEGAL FORMAT FOR TIME" ERROR
	PUSH P,B
	;THIS NUMBER MAY BE HOURS OR HOURS AND MINUTES.
	CAILE B,^D2400	;BUT CAN NEVER BE GREATER THAN 2400.
	JRST ITILF
	;MAKE SURE MINUTES PART OK (THIS ALWAYS SUCCEEDS IF ITS
	;JUST HOURS)
	IDIVI B,^D100
	CAIL C,^D60
	JRST ITILF
	;DON'T CHECK HOURS PART YET - ITS MAX VALUE DEPENDS ON
	;MANY THINGS INCLUDING FOLLOWING INPUT, AND THIS FIELD
	;(ONLY) CAN BE CHECKED AFTER CASES CONVERGE AND VALUES ARE
	;COMBINED.
	POP P,C		;FIRST VALUE IN C
	CAIE G,":"	;COLON AFTER NUMBER?
	JRST ITN4		;NO, ONLY ONE NUMBER IN INPUT.
;COLON SEEN IN TIME, THERE WILL BE AT LEAST TWO NUMBERS
	TLNE F,B7	;SECONDS ILLEGAL?
	TLNN F,B9	;COLON ILLEGAL BETWEEN HRS AND MINS ALSO?
	JRST .+2
	JRST ITILF		;BOTH TRUE, NO COLONS ALLOWED
	CALL TNIN		;PASS SPACES, READ NUMBER <60.
	MOVE D,B		;VALUE OF SECOND NUMBER IN D
	CAIE G,":"
	JRST ITN5		;TIME CONSISTS OF TWO NUMBERS
;SECOND COLON SEEN, THERE WILL BE THREE NUMBERS
	TLNN F,B7	;SECONDS ILLEGAL?
	TLNE F,B9	;COLON ALLOWED BETWEEN HOURS AND MINS?
	JRST ITILF		;TWO COLONS NOT ALLOWED
	CALL TNIN		;PASS SPACES, READ THIRD NUMBER
	MOVE E,B		;THIRD VALUE IN E
	JRST ITN7		;GO COMBINE VALUES

;ONLY ONE NUMBER IN TIME
ITN4:	TLNN F,B10	;COLON MANDATORY BETWEEN HOURS AND MINS?
	TLNE F,B8	;SEcONDS MANDATORY?
	JRST ITILF
	JRST ITN5B		;GO SEPERATE HOURS AND MINUTES. 
			;SECONDS ALREADY ZEROED.

;IDTNCS...
;TWO NUMBERS IN TIME
ITN5:	TLNE F,B8	;SECONDS REQUIRED?
	TLNN F,B10	;COLON REQUIRED BETWEEN HOURS AND MINUTES?
	JRST .+2
	JRST ITILF		;YES AND YES, TWO COLONS REQUIRED.
	TLNN F,B9	;COLON ILLEGAL?
	TLNE F,B8	;SECONDS REQUIRED?
	JRST ITN5A		;YES OR YES, TAKE IT AS HHMM:SS
	TLNN F,B7	;SECONDS ILLEGAL?
	TLNE F,B10+B12	;COLON REQ'D OR "TAKE AS HH:MM IF B7-10 OFF"?
	JRST ITN7		;YES, YES, OR YES, TAKE AS HH:MM
	TLNE F,B11	;"TAKE AS HHMM:SS IF B7-B10 OFF"?
	JRST ITN5A		;YES. THIS TEST MUST BE AFTER B7-10!
;B7-B12 ALL OFF, TAKE AS HH:MM IF POSSIBLE, ELSE HHMM:SS.
;WE MUST SEE WHETHER AM-PM IS PRESENT TO KNOW MAXIMUM
;HOUR VALUE IN ORDER TO DECIDE WHETHER WE CAN TAKE IT AS
;HH:MM.
	TLNE F,B14
	JRST ITN58		;24-HOUR TIME ILLEGAL, 12 HOURS.
	TLNE F,B15
	JRST ITN59		;AM-PM ILLEGAL, 24 HOURS.
	CAIG G,"Z"
	CAIGE G,"A"
	JRST ITN59		;NO LETTER, NO AM-PM.
ITN58:	CAILE C,^D12	;AM-PM WILL BE PRESENT, MAX TIME 12:59.
	JRST ITN5A		;HOURS TOO BIG, HHMM:SS.
	;JRST ITN7		;HH:MM. FALLING THRU WILL GET TO ITN7.
ITN59:	CAIN C,^D24	;NO AM-PM, MAX TIME 24:00
	JUMPE D,ITN7	;HOURS=24, HH:MM IF MINS =0.
	CAIGE C,^D24
	JRST ITN7		;FIRST NUMBER SMALL ENOUGH FOR HH:MM.
;TWO NUMBERS IN TIME, TO BE TAKEN AS HHMM:SS
ITN5A:	MOVE E,D		;SECONDS
ITN5B:	IDIVI C,^D100	;SEPERATE HOURS AND MINUTES.
;COMBINE HOURS, MINUTES, SECONDS INTO C.
ITN7:	IMULI C,^D60
	ADD C,D
	IMULI C,^D60
	ADD C,E
	CAILE C,^D<24*3600>	;MAX VALUE IN ANY CASE
	JRST ITILF
	TLNE F,B14	;24-HOUR TIME DISALLOWED?
	JRST ITN8		;YES, AM-PM MANDATORY.
	TLNE F,B15	;AM-PM ALLOWED?
	JRST ITN9		;NO
	CAIG G,"Z"	;AM-PM OPTIONAL. LETTER NEXT?
	CAIGE G,"A"	;..
	JRST ITN9		;NO

;IDTNCS...
;INPUT AM-PM AND MAKE APPROPRIATE CHECKS AND ADJUSTMENTS TO YIELD
;24-HOUR TIME.
;THE TIMES PRECEDING AM-PM MUST BE BETWEEN 1:00:00 AND 12:59:59.
ITN8:	CAIG C,^D<<12*60+59>*60+59>
	CAIA C,^D3600	;NO, ALSO ALLOW 0:00:00 TO 0:59:59
	JRST ITILF
	CALL AMPM		;READ STRING, SEARCH TABLE, RETURN +1 - +4.
	JRST [	CAILE C,^D<12*3600>	;+1: AM
		SUBI C,^D<12*3600>	;12AM=NOON, AFTER 12=MORNING
		JRST .+4]
	JRST [	CAIG C,^D<12*3600>	;+2: PM
		ADDI C,^D<12*3600>	;BEFORE 12=AFTERNOON, 12PM=MIDNITE
		JRST .+3]
	JRST [	CAIE C,^D<12*3600>	;+3: NOON
		JRST ITILF		;ONLY 12 NOON ALLOWED
		JRST .+2]
	JRST [	CAIE C,^D<12*3600>	;+4: MIDNITE
		JRST ITILF
		JRST .-2]		;GO ADD 12 HOURS
	;ABOVE MANIPULATIONS CAN MAKE MIDNITE COME OUT 24:00:00
	;AS OPPOSED TO 0:00:00.  THE FOLLOWING FIXES THAT,
	;AND ALSO CHANGES 24:00:00 (WHICH IS OK TO INPUT WITHOUT
	;AM-PM) TO 0.
ITN9:	REPEAT 0,<	;BUT 2400 IS END OF DAY, NOT START
	CAIL C,^D<24*3600>
	SUBI C,^D<24*3600>
>
	HRR CC,C	;RETURN LOCAL TIME IN RH D.
	SETZ C,		;IF NO TIME ZONE, RETURN LH D ZERO
			;EXCEPT FOR LOCAL TIME ZONE (BELOW).
	TLNE F,B16	;TIME ZONE ALLOWED IN INPUT?
	JRST ITN10
	CAIE G,"-"	;"-" PRECEDES TIME ZONE
	JRST ITN10		;NO TIME ZONE
	CALL IDCHAR	;NEXT CHARACTER
	CAIG G,"Z"	;LETTER NEXT?
	CAIGE G,"A"	;..
	JRST ITN10		;NO, NO TIME ZONE (EXTRA CHAR READ)
	CALL ZONE		;INPUT STRING, SEARCH TIME ZONES TABLE, 
			;RETURN THE RIGHT THING IN C (INCL B2 ON).
			;(NOTE THAT "DAYL" OR "STD" CAN BE INPUT,
			;WHICH LEAVES B2 OFF.)
ITN10:	MOVE B,TIMZON	;LOCAL TIME ZONE
	TRNN C,B2	;UNLESS A TIME ZONE WAS INPUT,
	DPB B,[POINT 6,C,35]	;RETURN LOCAL TIME ZONE IN C B12-17
	HRL CC,C	;RETURN THIS STUFF IN LH D.

;IDTNCS...
;NOW DONE DECODING DATE AND TIME.
;CHECK TERMINATOR: IT SHOULD BE NON-ALPHANUMERIC
IDTNC8:	CAIG G,"Z"
	CAIGE G,"A"
	JRST [	CAIG G,"9"
		CAIGE G,"0"
		JRST .+2
		JRST .+1]
	JRST [	TLNN F,B6
		JRST ITILF
		JRST IDILF]
;GIVE GOOD RETURN
	MOVE B,AA		;PUT RESULTS IN PROPER AC'S
	MOVE C,BB
	MOVE D,CC
	AOS (P)		;SKIP
	RET		;RETURN

;ERROR RETURN STUFF
IDILF:	SKIPA B,[DILFX1]	;"ILLEGAL FORMAT FOR DATE"
ITILF:	MOVEI B,TILFX1	;"ILLEGAL FORMAT FOR TIME"
;COME HERE WITH ERROR CODE IN B
	MOVE P,DD		;RESTORE P (CAN GET HERE FROM SUBRS WITH AN
			;INDEFINITE NUMBER OF EXTRANEOUS WORDS IN PD).
	MOVEM B,LSTERR	;SAVE ERR CODE IN PSB FOR "GETER", ETC
	RET		;RETURN +1 WITH ERROR CODE IN ACTUAL B

;SUBROUTINES FOR IDTNCS

;INPUT ALPHANUMERIC MONTH
;ACCEPTS ANY "UNIQUE SUBSTRING" OF A MONTH NAME.
;IGNORES PRECEDING SPACES AND (ON R2) PASSES FOLLOWING SEPERATING CHARS.
;R1: NEXT NON-SPACE CHAR NOT A LETTER.
;R2: SUCCESS, MONTH # IN RH AA.
;CLOBBERSS B,C,D,E.

MONTH:	CALL GNSC		;GET NON-SPACE CHARACTER
	CAIG G,"Z"
	CAIGE G,"A"
	RET		;NO LETTER: R1
	CALL IDSTR	;READ STRING. BUFFERS IT IN PD. PUTS OLD
			;P VALUE IN E.
	MOVEI C,MONTHS	;TABLE ADDRESS
	CALL FSYM		;SEARCH TABLE
	 JRST IDILF	;NOT FOUND, ILLEGAL DATE FORMAT
	HRR AA,C	;RETURN MONTH
	MOVE P,E		;DEALLOCATE STRING SPACE IN PD
	AOS (P)		;SKIP
	JRST IDSEP		;RETURN VIA SUBR TO PASS SEPERATING CHARS

	;MACRO FOR TABLES OF MONTHS, AM-PM'S, TIME ZONES
	DEFINE TM (TEXT,VALUE)
	<XWD VALUE,[ASCIZ /TEXT/]>

;MONTH TEXTS TABLE, IN FSYSM FORMAT
MONTHS:	^D12		;WORD 0 OF TABLES IS NUMBER OF ENTRIES
	TM APRIL,3
	TM AUGUST,7
	TM DECEMBER,^D11
	TM FEBRUARY,1
	TM JANUARY,0
	TM JULY,6
	TM JUNE,5
	TM MARCH,2
	TM MAY,4
	TM NOVEMBER,^D10
	TM OCTOBER,^D9
	TM SEPTEMBER,^D8

;IDTNCS SUBROUTINES...

;SUBROUTINE TO INPUT YEAR
;RETURNS YEAR IN LH AA

YEAR:	CALL IDNIN	;INPUT NUMBER
	 JRST IDILF	;NONE, ILLEGAL DATE FORMAT ERROR
	CAIGE B,^D100
	ADDI B,^D1900
	CAIL B,^D1858
	CAILE B,^D2576
	 JRST IDILF	;OUT OF RANGE, "ILLEGAL FORMAT"
	HRL AA,B
	RET


;AM-PM LOOKUP ROUTINE. FIRST CHARACTER MUST BE IN G.
;ACCEPTS ONE OF MORE LETTERS OF --     AND RETURNS + --
;	AM		1
;	PM		2
;	NOON		3
;	MIDNIGHT	4
;CLOBBERS B,D,E
;ONLY CALLED IN ONE PLACE; CODED AS A SUBR TO FACILITATE CHANGE SUCH
;AS SUBSTITUTION OF A SIMPLER SYMBOL TABLE SEARCH.

AMPM:	PUSH P,C
	CALL IDSTR
	MOVEI C,AMPMS
	CALL FSYM
	 JRST ITILF
	MOVE P,E
	ADDM C,-1(P)	;INCREMENT RETURN BY VALUE FROM TABLE
	POP P,C
	RET

AMPMS:	4
	TM AM,0
	TM MIDNIGHT,3
	TM NOON,2
	TM PM,1

;IDTNCS SUBROUTINES...

;TIME ZONE LOOKUP SUBROUTINE. FIRST CHAR MUST BE IN G.
;ACCEPTS ANY UNIQUE SUBSTRING OF ANY OF THE US STANDARD OR
;DAYLIGHT TIME ZONE ABBREVIATIONS, ALSO "STANDARD" AND "DAYLIGHT".
;RETURNS IN RH C WHAT IDTNC SHOULD RETURN IN LH D,
;EXCEPT THAT LOCAL TIME ZONE ISN'T FILLED IN FOR "DAY" OR "STD".
;CLOBBERS B,D,E.
;LIKE "AMPM", THIS SUBR IS ONLY USED IN ONE PLACE.

ZONE:	CALL IDSTR
	MOVEI C,ZONES
	CALL FSYM
	 JRST ITILF
	MOVE P,E
	RET

ZONES:	^D17
	TM CDT,B0+B1+B2+6
	TM CST,B0+B2+6
	TM DAYLIGHT,B0+B1
	TM EDT,B0+B1+B2+5
	TM EST,B0+B2+5
	TM GDT,B0+B1+B2+0	;"GREENWICH DAYLIGHT TIME"
	TM GMT,B0+B2+0
	TM GST,B0+B2+0		;GREENWICH STANDARD TIME
	TM MDT,B0+B1+B2+7
	TM MST,B0+B2+7
	TM PDT,B0+B1+B2+^D8
	TM PST,B0+B2+^D8
	TM HDT,B0+B1+B2+^D10
	TM HST,B0+B2+^D10
	TM ST,B0		;"ST" OK FOR "STANDARD"
	TM STANDARD,B0
	TM STD,B0		;"STD" OK FOR "STANDARD"


;IDTNCS SUBROUTINES...

;SUBROUTINE FOR "MONTH", "AMPM", AND "ZONE" TO READ A STRING OF LETTERS.
;FIRST CHARACTER MUST BE IN G. STORES IN ASCIZ FORM IN PUSHDOWN,
;RETURNING PREVIOUS P VALUE IN E AND STRING POINTER IN B.
;CLOBBERS C,D.

IDSTR:	MOVE E,P		;SAVE PUSHDOWN LEVEL
	SUB E,[XWD 1,1]	;...BEFORE THIS ROUTINE WAS CALLED
	HRRI C,1(P)
	HRLI C,<POINT 7,0,-1>B53	;FORM BYTE PTR INTO PUSHDOWN
	MOVEI D,^D14	;MAX LENGTH 3 WORDS
	PUSH P,[0]		;ALLOCATE 3 WORD OF BUFFER SPACE, ALSO
	PUSH P,[0]		;ZEROS REST OF LAST WORD
	PUSH P,[0]		;(NEEDED FOR SEARCH ROUTINE "FSYM")
IDSTR2:	IDPB G,C	;STORE CHARACTER
	CALL IDCHAR
	CAIG G,"Z"
	CAIGE G,"A"
	JRST IDSTR9	;NON-LETTER ENDS STRING
	SOJG D,IDSTR2	;GO STORE CHARACTER
	;STOP ANYWAY AT 14 CHARS. THE APPROPRIATE "ILLEGAL FORMAT"
	;ERROR WILL HAPPEN IN CALLING ROUTINE BECAUSE NOTHING
	;14 CHARS LONG IS IN TABLES.
IDSTR9:	HRRI B,2(E)	;RETURN BYTE PTR TO STRING IN B
	HRLI B,<POINT 7,0,-1>B53
	MOVE C,1(E)	;FETCH RETURN ADDR FROM PD
	JRST (C)		;RETURN

;IDTNCS SUBROUTINES...

;TABLE SEARCH FOR "MONTH", "AMPM", "ZONE"
;TAKES IN B: POINTER TO LEFT ADJUSTED ASCIZ STRING
;	  C: POINTER TO TABLE OF THE FORM:
;		WORD 0: N=NUMBER OF ENTRIES
;		WORDS 1-N: LH: V=VALUE
;			   RH: POINTER TO ASCIZ STRING
;		ENTRIES MUST BE IN ALPHABETICAL ORDER
;RETURNS +1: NO MATCH OR AMBIGUOUS
;	 +2: UNIQUE SUBSET OR EXACT MATCH, VALUE "V" IN RH C.
;AC USE
;   A	POINTS AT LAST ENTRY IN TABLE
;   B   POINTER WHICH IS INDEXED THRU INPUT TEXT
;   C   POINTER INTO TABLE
;   D   WORD OF INPUT TEXT
;   E   POINTER WHICH IS INDEXED THROUGH THE TEXT OF A TABLE ENTRY
;   F   WORD OF TEXT FROM TABLE ENTRY
;   G   "DELTA" - THE BINARY SEARCH INCREMENT

 IFN E-D-1,<PRINTX FSYM WON'T WORK CAUSE E IS NOT D+1
>

;IDTNCS SUBROUTINE FSYM...

FSYM:	PUSH P,A		;SAVE AC'S
	PUSH P,B
	PUSH P,C
	PUSH P,D
	PUSH P,E
	PUSH P,F
	PUSH P,G
	MOVE A,C		;TABLE LOCATION
;INIT DELTA TO HIGHEST POWER OF 2 IN TABLE LENGTH
	MOVE D,(A)		;TABLE LENGTH
	JFFO D,.+2
	JRST NOMAT		;0 LENGTH: NO MATCH
	MOVEI G,1
	MOVN E,E
	LSH G,43(E)	;SHIFT BY 35 - # OF 0 BITS TO GET POWER

	MOVEI C,(A)	;INIT POINTER THAT RUNS OVER TABLE
	ADD A,(A)	;LOCATION OF LAST USED ENTRY IN TABLE

;IDTNCS SUBROUTINE FSYM...

; BINARY SEARCH. STOPS AT = ENTRY OR SMALLEST > ENTRY.

FSRC1:	ADDI C,(G)	;ADD DELTA TO TABLE POINTER
FSRC1A:	LSH G,-1	;HALVE DELTA FOR NEXT TIME AROUND
	CAILE C,(A)
	JRST FSRC4		;POINTS BEYOND END OF TABLE, GO BACK UP.
;COMPARE THE INPUT TEXT TO A TEXT IN THE TABLE
	MOVE B,-5(P)	;GET VALUE THAT CAME IN B AS PTR TO INPUT TEXT
	MOVE E,(C)		;POINTER INTO TABLE TEXT FROM TABLE WORD
FSRC2:	MOVE D,(B)		;GET AN INPUT WORD
	LSH D,-1	;POSITION SO DATA ISN'T IN SIGN BIT
	MOVEI B,1(B)	;INDEX INPUT POINTER
	MOVE F,(E)		;GET A WORD OF TABLE TEXT
	LSH F,-1
	CAMGE F,D
	JRST FSRC3		;TABLE ENTRY LESS THAN INPUT
	CAME F,D
	JRST FSRC4		;TABLE ENTRY GREATER THAN INPUT 
	TRNE D,177	;THESE WORDS EQUAL, AT END OF INPUT?
	AOJA E,FSRC2	;NO, INDEX TABLE TEXT PTR, CONTINUE COMPARE.

;MATCH FOUND.
;CODE FOR EXITS, SEARCH STUFF CONTINUES AFTER THIS.
UPAR:	AOS -7(P)
	HLRZ D,(C)	;VALUE FIELD FROM TABLE ENTRY WHICH MATCHED
	MOVEM D,-4(P)	;RETURN SAME IN C
APAR:
NOMAT:	POP P,G		;RESTORE AC'S
	POP P,F
	POP P,E
	POP P,D
	POP P,C
	POP P,B
	POP P,A
	RET		;RETURN

;THE TEXT OF THIS TABLE ENTRY IS LESS THAN INPUT STRING
FSRC3:	JUMPN G,FSRC1	;DELTA><0, GO MOVE DOWN AND CONTINUE SEARCH
	AOJA C,NEM1	;DONE SEARCH. NEXT ENTRY IN TABLE IS THE
	;SMALLEST LARGER ENTRY. IF THERE IS NO NEXT ENTRY, THEN
	;THERE IS NO MATCH. "SBST" SUBR IS CODED TO HANDLE THIS CASE.

;THIS TABLE ENTRY GREATER THAN INPUT, OR POINTER IS OF END OF TABLE
FSRC4:	SUBI C,(G)	;MOVE UP IN TABLE
	JUMPN G,FSRC1A	;UNLESS DELTA=0, CONTINUE SEARCH.

;IDTNCS SUBROUTINE FSYM...

;WE GET TO "NEM1" WHEN THE SEARCH COMPLETES WITHOUT FINDING AN EXACT
;MATCH.  C POINTS TO SMALLEST TABLE ENTRY GREATER THAN INPUT.
;THIS ENTRY MAY OR MAY NOT BE A SUBSET MATCH; IF IT IS, THEN IT IS 
;AMBIGUOUS IF AND ONLY IF NEXT ENTRY IS ALSO A SUBSET MATCH.
;NOTE ALSO THAT WE CAN TEST NEXT ENTRY FIRST, AND IF IT IS SUBSET,
;THEN WE KNOW INPUT IS AMBIGUOUS WITHOUT TESTING THIS ENTRY.

;TEST NEXT ENTRY
NEM1:	ADDI C,1	;POINT C AT NEXT ENTRY
	CALL SBST		;SUBSET TEST SUBR COMPARES ENTRY C TO INPUT
	SOJA C,NEM2	;R1: NOT A SUBSET (INCLUDES NO NEXT ENTRY)
	SOJA C,APAR	;R2: IS A SUBSET, SO INPUT IS AMBIG. GIVE R2.

;NOT AMBIGUOUS, SO TEST THIS ENTRY
NEM2:	CALL SBST
	JRST NOMAT		;INPUT NOT SUBSET THIS ENTRY, NO MATCH
	JRST UPAR

;IDTNCS SUBROUTINE FSYM...

;SUBSET TEST SUBROUTINE FOR "FSYM".
;COMPARES INPUT STRING AND STRING FOR TABLE ENTRY C POINTS TO,
; SKIPS IF FORMER IS INITIAL SUBSTRING OF LATTER.
;ON R2, RETURNS IN E A BYTE POINTER TO REST OF TABLE ENTRY STRING
;MUST BE CALLED ONLY WHEN INPUT STRING IS LESS THAN TABLE STRING
;SEE "FSYM"'S COMMENTS ON AC USE. CLOBBERS B,D,E,F,G.

SBST:	CAILE C,(A)	;C BEYOND END OF TABLE?
	RET		;YES, NO ENTRY, INPUT ISN'T SUBSET, RETURN.
;FIND FIRST WORD OF STRINGS IN WHICH THEY DIFFER
	MOVE B,-6(P)	;POINTER TO INPUT TEXT
	MOVE E,(C)		;POINTER TO TABLE ENTRY'S TEXT
SBST1:	MOVE D,(B)		;WORD OF INPUT
	LSH D,-1	;POSITION FOR COMPARE
	MOVEI B,1(B)	;INDEX INPUT POINTER
	MOVE F,(E)		;WORD OF TABLE ENTRY
	LSH F,-1	;POSITION
	CAMG F,D
	AOJA E,SBST1	;IF ITS = IT MUST NOT BE END.
	TRNE D,177	;IS DIFFERENCE IN LAST WORD OF INPUT?
	RET		;NO, INPUT CAN'T BE SUBSTRING OF TABLE ENTRY.
;MASK OFF TABLE TEXT TO LENGTH OF INPUT
	HRLZI G,-4
	TDNE D,	[-1	;LOOP TO SEE HOW MANY BYTES OF D ARE 0
		1777777777
		7777777
		37777
		177 ] (G)	;YES, (G).
	AOBJN G,.-1
	ANDCM F,@.-2	;THIS CLEARS F WHERE THERE ARE BITS IN TABLE
;CONVERT WORD PTR IN E TO BYTE POINTER AS REQUIRED ON R2.
	HLL E,	[POINT 7,0,-1
		POINT 7,0,6
		POINT 7,0,13
		POINT 7,0,20
		POINT 7,0,27 ] (G)
;NOW IF MASKED PART OF TABLE WORD = INPUT WORD, INPUT IS SUBSET.
	CAMN F,D
	AOS (P)		;SKIP
	RET

;IDTNCS SUBROUTINES...

;NUMBER INPUT CONVERSION: CAN'T USE SYSTEM'S NIN  BECAUSE STRING
;POINTER MUST ADDRESS CALLER'S, NOT CURRENT PC'S, ADDRESS SPACE,
; AND IN ANY CASE NOT UNTIL BKJFN
; WORKS FOR ALL DEVICES OR ANOTHER MEANS OF RETURNING TERMINATOR
; IS ESTABLISHED.
;R1: NO DIGIT OR OVERFLOW (18 BITS)
;R2: VALUE IN B
IDNIN:	CALL GNSC		;GET NON-SPACE CHARACTER
	CAIG G,"9"
	CAIGE G,"0"
	RET
	MOVEI B,-60(G)
NIN1:	CALL IDCHAR
	CAIG G,"9"
	CAIGE G,"0"
	JRST [	AOS (P)
		RET]
	IMULI B,^D10
	ADDI B,-60(G)
	TLNN B,-1
	JRST NIN1
	RET		;OVERFLOW


;NIN FOR MONTH OR DAY OF MONTH
;ERROR IF NOT IN RANGE 1 TO 31.
;RETURNS VALUE -1 IN B.

DNIN:	CALL IDNIN
	 JRST IDILF
	SOJL B,IDILF
	CAIL B,^D31
	JRST IDILF
	RET


;NIN FOR MINUTES OR SECONDS: ERROR IF >= 60

TNIN:	CALL IDCHAR	;PASS PRECEDING SEPARATOR
	CALL IDNIN
	 JRST ITILF
	CAIL B,^D60
	JRST ITILF
	RET

;IDTNCS SUBROUTINES...

;SEPERATOR BETWEEN DATE FIELDS:
;ANY NUMBER OF SPACES, DASH OR SLASH, ANY NUMBER OF SPACES.
IDSEP:	CALL GNSC
	CAIE G,"-"
	CAIN G,"/"
	CALL IDCHAR
	JRST GNSC


;GET NON-SPACE CHARACTER INTO G

GNSC:	CAIE G," "
	RET
	CALL IDCHAR
	JRST GNSC


;GET NEXT INPUT CHARACTER INTO G

IDCHAR:	PUSH P,B
	CALL BIN1		;INPUT TO B FROM SOURCE IN CALLER'S A.
	CAIL B,141
	CAILE B,172
	JRST .+2
	SUBI B,40	;CONVERT LOWER CASE TO UPPER
	MOVE G,B
	POP P,B
	RET


;ODCNV

; OUTPUT DATE AND TIME CONVERT
; BREAKS DOWN INTERNAL DATE AND TIME TO LOCAL WEEKDAY, DAY, MONTH,
;	AND YEAR, BUT DOES NOT CONVERT TO TEXT.

;TAKES: 2: LEFT HALF: INTERNAL DATE
;	   RIGHT HALF: INTERNAL TIME
;	   OR ENTIRE WORD -1 FOR CURRENT DATE AND TIME
;	4: (0 FOR NORMAL CASE)
;	   B0: OFF, USE DAYLIGHT SAVINGS OR NOT AS APPROPRIATE FOR
;		    GIVEN DATE
;	       ON, USE DAYLIGHT SAVINGS IF AND ONLY IF B1 IS ON.
;	   B2: OFF, USE LOCAL TIME ZONE
;	       ON, USE TIME ZONE IN B12-B17

;RETURNS +1: 2: LEFT HALF: REAL YEAR (IE SINCE THE YEAR 0)
;		RIGHT HALF: MONTH (0=JANUARY)
;	     3: LEFT HALF: DAY OF MONTH (FIRST DAY = 0)
;		RIGHT HALF: DAY OF WEEK (MONDAY = 0)
;	     4: B0: ON
;		B1: ON IF DAYLIGHT SAVING TIME WAS USED
;		B2: ON
;		B12-17: TIME ZONE USED: NUMBER OF HOURS WEST OF
;		        GREENWICH, EG EST = 5.
;		RH: LOCAL TIME IN SECONDS SINCE MIDNITE

;ODCNV ERROR MNEMONICS (TRAP)
;DATEX6: SYSTEM HAS NO DATE AND TIME (AND -1 WAS GIVEN)
;TIMEX1: ILLEGAL TIME (GREATER THAN 24 HOURS)
;ZONEX1: ILLEGAL TIME ZONE

;INTERNAL AC USE
;	B: YEAR,,MONTH
;	C: SYSTEM INTERNAL TIME, THEN LOCAL TIME
;	D: DAY OF YEAR
;	AA: TIME STUFF FROM, THEN TO, D.
;	BB: SYSTEM INTERNAL, LATER LOCAL, DATE (MUST BE A FULL WORD
;	    QUANTITY BECAUSE TIME ZONE CAN TAKE IT OVER 18 BITS.)

;ODCNV JSYS

.ODCNV:	JSYS MENTR
	MOVE AA,D		;TIME ZONE AND DAYLIGHT GARBAGE
;GET INTERNAL (GREENWICH) DATE & TIME TO CONVERT INTO BB AND C
	CAME B,[-1]
	 JRST ODC2		;DATE & TIME GIVEN
	PUSH P,A		;SAVE DESTINATION
	GTAD		;GET TIME AND DATE FROM SYSTEM INTO A
	CAMN A,[-1]
	JRST [	POP P,A
		MOVEI B,DATEX6	;SYSTEM HAS NO DATE AND TIME
		JRST ODCER]
	MOVE B,A
	POP P,A
ODC2:	HRRZ C,B	;TIME IN C
	HLRZ BB,B	;DATE IN BB

;CHECK FOR REASONABLE ARGUMENT: ANY 18 BITS OK FOR DATE;
; TIME MUST BE .LE. 2400.
	CAILE C,^D<24*3600>
	JRST [	MOVEI B,TIMEX1	;"ILLEGAL TIME"
		JRST ODCER]

;CONVERT TIME TO LOCAL STD AND ADJUST INTERNAL FORMAT DATE TO LOCAL.
;THIS MUST BE DONE FIRST BECAUSE IT CAN CHANGE YEAR
	CALL CNVZON	;GET TIME ZONE AS SECONDS EARLIER THAN GMT
	 JRST ODCER	;BAD ZONE, GIVE ERR RET
	SUB C,E		;ADJUST TIME FOR TIME ZONE
	CALL FIXDT	;IF TIME NOW OUT OF RANGE 0 TO 23:59:59,
			;ADD OR SUBTRACT 24 HOURS AND ADJUST DATE
			;CORRESPONDINGLY.

;ODCNV...
;CONVERT SYSTEM DATE (DAY 1=18 NOV 1858) IN LH C
; TO YEAR IN LH A AND DAY OF YEAR IN D.
;ALLOW FOR LEAP YEARS, WHICH RUN IN CYCLES OF:
; 4 YEARS, EG 1961-64, LAST YEAR NORMALLY LEAP.
; "CENTURIES", EG 1801-1900, LAST YEAR NORMALLY NON-LEAP.
; "QUADRACENTURIES", EG 1501-1900, FIRST "CENTURY" THEREOF
;     1 DAY LONGER THAN OTHERS CAUSE ITS LAST YEAR IS LEAP.
;NOTE: DATE IS TREATED AS 18-BIT MAGNITUDE --
; DATES BEFORE 18 NOV 1858 CANNOT BE REPRESENTED.

	MOVE D,BB		;INTERNAL DATE
;SCALE SO DAY 0 IS 1/1/1501,
;THE BEGINNING OF A QUADRACENTURY.
;DERIVATION OF NUMBER ADDED:
; 365*358	YEARS 1501 TO 1859
; 89-2		LEAP YEARS IN THAT INTERVAL
; -12-31-1	DAYS 1 JAN 1859 TO 18 NOV 1858
; -1	BASE DAY IS 0 NOT 1
	ADDI D,^D<365*358+89-2-12-31-1-1>
;DO THREE DIVIDES, 1 FOR EACH CYCLE,
;ACCUMULATING # YEARS IN D.
	IDIVI D,^D<400*365+97>	;# DAYS / QUADRACENTURY
	IMULI D,^D400	;CONVERT # QUADRACENTURIES TO YEARS
	;DAY OF QUADRACENTURY IS IN E. SCALE TO QUARTER-DAYS AND
	;DIVIDE BY N+1/4 DAYS SO THAT FIRST CENTURY OF QUADRACENTURY
	;GETS EXTRA DAY.
	LSH E,2
	IDIVI E,<^D<100*365+24>>B33+1	;# QUARTER-DAYS / CENTURY
	IMULI E,^D100	;CONVERT # CENTURIES TO YEARS
	ADD D,E		;ADD TO # YEARS FOR QUADRACENTURIES
	LSH F,-2	;SCALE BACK TO DAYS
	;DAY OF CENTURY IS IN F
	IDIVI F,^D365*4+1	;# DAYS IN A 4-YEAR CYCLE
	LSH F,2		;CONVERT # 4-YEAR CYCLES TO YEARS
	ADD D,F		;ADD TO YEARS SO FAR
	MOVE F,G		;DAY WITHIN 4-YEAR CYCLE
	IDIVI F,^D365	;DIVIDE BY NUMBER OF DAYS IN REGULAR YEAR
	CAIE F,4	;IF WE GOT 5TH YEAR, IT IS REALLY...
	JRST .+3
	MOVEI G,^D365	;366TH DAY OF...
	SUBI F,1	;4TH (LEAP) YEAR OF 4-YEAR CYCLE.
	ADD D,F		;ADD YEAR OF 4-YEAR CYCLE TO YEARS SO FAR
	;EXCEPTION FOR THE LAST YEAR OF MOST CENTURIES IS HANDLED BY
	;THE FACT THAT DAY 365 OF YEAR 99 CAN GET HERE ONLY FOR
	;CENTURY 0 OF A QUADRACENTURY.  (OTHERWISE ABOVE DIVIDE
	;YIELDS DAY 0 OF NEXT CENTURY.)
;NOW HAVE CORRECT YEAR. SCALE IT TO YEARS SINCE 0AD AND RETURN IT.
	ADDI D,^D1501
	HRL B,D		;RETURN YEAR IN LH B
	MOVE D,G		;DAY OF YEAR TO D

;ODCNV...
;FOR NON-LEAP YEARS INCREMENT DAY OF YEAR IF AFTER FEB 28
	CALL NLEAP	;SKIP IF NOT LEAP YEAR (USES LH B)
	JRST ODLEAP
	CAIL D,^D<31+28>
	ADDI D,1
;ADJUST FOR DAYLIGHT SAVING
ODLEAP:	CALL ODAYL	;SKIP IF DAYL SAVINGS APPLIES (USES C,D,AA,BB)
	JRST ODCSTD	;NOT DAYLIGHT SAVINGS
	ADDI C,^D3600	;ADD AN HOUR
	CAIGE C,^D<24*3600>;DID IT RUN ACCROSS MIDNITE?
	JRST ODC7
	ADDI D,1	;NEXT DAY OF YEAR
	ADDI BB,1	;NEXT LOCAL DATE (USED FOR DAY OF WEEK)
	SUBI C,^D<24*3600>	;24 HOURS EARLIER
	CAIN D,^D<31+28>	;DID THIS EXTRA HOUR MAKE IT FEB 29?
	CALL NLEAP		;YES. IS IT A NON-LEAP YEAR?
	SKIPA			;NO. OK.
	ADDI D,1		;YES. STEP IT TO MARCH 1
	CAIL D,^D365	;DID IT RUN OVER END OF YEAR?
	JRST [	SETZ D,		;YES, SOME NUT IS LOOKING FOR A BUG
				;OR IS IN SOUTHERN HEMISPHERE.
		ADD B,[1B17]	;NEXT YEAR
		JRST .+1]
ODC7:
ODCSTD:
;NOW DONE WITH TIME AND YEAR. YEAR ALREADY IN LH B.
	HRR AA,C	;SAVE TIME FOR RETURN IN C.
;MONTH AND DAY OF MONTH
	HRRI B,0	;GENERATE MONTH NUMBER IN RH B
	CAML D,DAYS+1(B)  ;FIND MONTH WITH MORE DAYS DAYS BEFORE IT
	AOJA B,.-1
	SUB D,DAYS(B)	;SUBTRACT DAYS IN PRECEDING MONTH
	HRL C,D		;RETURN DAY OF MONTH
;DAY OF WEEK
	MOVE E,BB		;LOCAL DATE IN SYSTEM FORMAT
	ADDI E,DWFUDG
	IDIVI E,7
	HRR C,F		;RETURN IT
;EXIT
	UMOVEM B,B
	UMOVEM C,C
	UMOVEM AA,D
	JRST MRETN

;ERROR RETURN, ERROR CODE MUST BE IN B
ODCER:	MOVEM B,LSTERR	;SAVE ERROR CODE IN PSB
	JRST ITRAP		;GIVE ILLEGAL INSTRUCTION PSI

;IDCNV

; INPUT DATE CONVERT
; TAKES LOCAL YEAR, MONTH, DAY, AND TIME (AS NUMBERS) AND
; 	GENERATES INTERNAL DATE AND TIME

;THIS JSYS IS A SUBSET OF IDTIM AND WILL ONLY BE USED BY THOSE WHO
;WISH TO MANIPULATE HOURS, DAY OF WEEK, ETC AS SEPERATE NUMBERS,
;OR WHO WISH TO INPUT DATE AND/OR TIME IN A FORMAT NOT ACCEPTED BY
;IDTIM, THEN CONVERT TO STANDARD INTERNAL FORMAT.

;TAKES: 2: LH: REAL YEAR
;	   RH: MONTH (JANUARY = 0)
;	3: LH: DAY OF MONTH
;	4: LH (0 FOR NORMAL CASE):
;	      B0: OFF, USE DAYLIGHT SAVING IF APPROPRIATE FOR GIVEN DATE
;	          ON, SEE B1
;	      B1: ON TO USE DAYLIGHT SAVING IF B0 ON
;	      B2: OFF, USE LOCAL TIME ZONE
;	          ON, USE TIME ZONE IN B12-17
;	      B12-B17: IF B2 ON, TIME ZONE TO USE, AS NUMBER OF HOURS
;		       WEST OF GREENWICH.*
;	   RH: LOCAL TIME IN SECONDS SINCE MIDNITE

;RETURNS +1: FAILURE, ERROR NUMBER IN A (SEE BELOW)
;	 +2: SUCCESS:
;	     2: LH: INTERNAL DATE (DAYS SINCE 18 NOV 1858, GREENWICH)
;		RH: INTERNAL TIME (SECONDS  SINCE MIDNITE AT GREENWICH)
;	     4: B0: ON
;		B1: ON IF DAYLIGHT SAVINGS WAS APPLIED
;		B2: ON
;		B12-17: TIME ZONE USED

;IDCNV ERROR MNEMONICS:
;TIMEX1
;DATEX1: ILLEGAL YEAR (OUT OF RANGE 1858 TO 2576)
;DATEX2: MONTH TOO LARGE
;DATEX3: DAY OF MONTH TOO LARGE
;DATEX5: ILLEGAL DATE (OUT OF RANGE 17 NOV 1858 TO 7 AUG 2576 GMT)
;ZONEX1: ILLEGAL TIME ZONE

;INTERNAL AC USE
;	B-C: ARGUMENTS; C: LOCAL TIME.
;	D: DAY OF YEAR
;	AA: ARGUMENT FROM D, THEN RESULT TO, D
;	BB: LOCAL DATE

;* TIME ZONE IS NUMBER OF HOURS WEST OF GREENWICH AND CAN RANGE FROM
;  -12 TO 12 (DECIMAL).  THE ZONES -12 AND +12 REPRESENT THE SAME
;  TIME DIFFERENCE BUT ON OPPOSITE SIDES OF THE INTERNATIONAL DATE
;  LINE.

;IDCNV

.IDCNV:	JSYS MENTR
	MOVE AA,D		;TIME ETC FROM D IN AA THRUOUT IDCNV
;CHECK ARGUMENTS, GET DAY OF YEAR IN D
	CALL CKYMDT	;CHECK YEAR, MONTH, DAY OF MONTH, TIME, AND
			;COMPUTE DAY OF YEAR WHILE SO DOING.
			;USES B,C,AA.
	 JRST IDCE		;BAD ARG, ERROR CODE IN B.
;CONVERT YEAR TO DAYS SINCE 1/1/1601 IN BB
	HLRZ BB,B	;YEAR
	CAIG BB,^D99
	ADDI BB,^D1900
	SUBI BB,^D1601
	MOVE F,BB
	IMULI BB,<^D365>B33+1	;365  1/4  DAYS PER YEAR
	LSH BB,-2
	IDIVI F,^D100
	SUB BB,F	;MINUS ONE DAY PER CENTURY
	LSH F,-2
	ADD BB,F	;BUT ADD BACK ONE DAY PER 4 CENTURIES
;COMBINE DATE, SCALE TO DAYS SINCE 11/17/1858
	ADD BB,D	;ADD DAY IN YEAR TO DAYS IN PRECEDING YEARS
	SUBI BB,^D<365*258+64-2-12-31-1-1>
			;SCALE SO DAY 1 IS 18 NOV 1858
			;THERE'S AN EXPLAINATION OF NUMBER ADDED
			;IN ODCNV
;FOR DATES AFTER FEB 28 IN NON-LEAP YEARS REMOVE THE DAY ALLOWED
;IN DATE FOR FEB 29.
	CAIL D,^D31+^D28	;BEFORE FEB 29?
	CALL NLEAP	;LEAP YEAR?
	JRST IDC6		;YES OR YES
	CAIN D,^D31+^D28	;FEB 29?
	JRST [	MOVEI B,DATEX3	;YES, "ILLEGAL DAY OF MONTH"
		JRST IDCE]
	SUBI BB,1	;NOT LEAP YEAR, REMOVE FEB 29
	;THE DAY FOR 2/29 IS INTENTIONALLY LEFT IN THE QUANTITY IN D,
	;BECAUSE THE SUBROUTINE "IDAYL", CALLED BELOW, REQUIRES IT.
IDC6:

;IDCNV...
	MOVEI C,(AA)	;TIME WITH LH 0 TO C
;ADJUST FOR DAYLIGHT SAVING IF IN EFFECT
	CALL IDAYL	;SKIP IF IN EFFECT (USES C,D,AA,BB)
	JRST .+2
	SUBI C,^D3600	;REAL TIME IS AN HOUR EARLIER
			;DON'T BOTHER TO CORRECT UNDERFLOW YET.
;CONVERT TIME TO GREENWICH AND ADJUST DATE IF NECESSARY
	CALL CNVZON	;GET TIME ZONE IN E AS SECS BEHIND GREENWICH
	 JRST IDCE		;BAD ZONE, GIVE ERROR RETURN
	ADD C,E		;ADD TIME ZONE LAG TO TIME
	CALL FIXDT	;IF TIME NOW NEGATIVE OR TOO BIG,
			;FIX IT BY ADDING OR SUBTRACTING 24 HOURS AND
			;ADJUSTING DATE CORRESPONDINGLY.
	TLNE BB,-1	;CHECK FOR DATE OUT OF 18-BIT RANGE
	JRST [	MOVEI B,DATEX5	;"DATE OUT OF LEGAL RANGE"
		JRST IDCE]
;EXITS
	HRL C,BB		;RETURN DATE IN LH B,
	UMOVEM C,B	;TIME IN RH C.
	UMOVEM AA,C	;TIME ZONE STUFF IN LH C
	AOS (P)		;RETURN TO CALLING LOCATION +2
	JRST MRETN

;ERRORS COME HERE, ERROR CODE IN B
IDCE:	UMOVEM B,A	;RETURN ERROR CODE IN A
	MOVEM B,LSTERR	;STORE ERROR CODE IN PSB ALSO
	jrst mrtne1	; Handle error

;SUBROUTINES FOR IDCNV, ODCNV, ETC


;SUBROUTINE TO CHECK VALUES GIVEN FOR YEAR, MONTH, DAY OF MONTH,
; AND TIME, FOR IDCNV AND ODTNC.
;TAKES: B:    YEAR,,MONTH
;	LH C: DAY OF MONTH
;	RH AA: TIME
;IF BAD, R1 WITH ERROR CODE IN B
;IF OK, R2 WITH DAY OF YEAR IN D (ASSUMING ITS A LEAP YEAR AT THIS
;          POINT), E CLOBBERED.

CKYMDT:	HLRZ E,B	;YEAR
	CAIG E,^D99
	ADDI E,^D1900
	CAIL E,^D1858
	CAILE E,^D2576
	JRST [	MOVEI B,DATEX1	;"ILLEGAL YEAR"
		RET]
	HRRZ E,B	;MONTH
	CAIL E,^D12
	JRST [	MOVEI B,DATEX2	;"MONTH TOO LARGE"
		RET]
	HLRZ D,C	;DAY OF MONTH
	ADD D,DAYS(E)	;ADD DAYS IN PRECEDING MONTHS (ALLOWS A DAY FOR
			;FEB 29 - ADJUSTED LATER IN IDCNV FOR NON-LEAP
			;YEARS)
	CAML D,DAYS+1(E);RESULT MUST NOT BE GREATER THAN NUMBER OF DAYS
			;IN MONTHS INCLUDING THIS ONE
	 JRST [	MOVEI B,DATEX3	;"DAY OF MONTH TOO LARGE"
		RET]
	HRRZ E,AA
	CAILE E,^D<24*3600>
	 JRST [	MOVEI B,TIMEX1	;"ILLEGAL TIME"
		RET]
	AOS (P)		;SKIP
	RET


;TABLE OF NUMBER OF DAYS IN MONTHS PRECEDING MONTH USED AS TABLE INDEX
;USED BY IDCNV, ODCNV.
DAYS:	EXP 0,^D31,^D60,^D91,^D121,^D152
	EXP ^D182,^D213,^D244,^D274,^D305,^D335
	^D366

;SUBROUTINE TO SKIP IF YEAR IN LH B IS NOT A LEAP YEAR
;DESTROYS F.  USED BY IDVNC, ODCNV.
NLEAP:	PUSH P,E
	HLRZ E,B
	CAIG E,^D99
	ADDI E,^D1900
	IDIVI E,^D4
	JUMPN F,IFL9	;NOT DIVISIBLE BY 4, NOT LEAP YEAR
	IDIVI E,^D25
	JUMPN F,IFL8	;DIVISIBLE BY 4, NOT 100, IS LEAP YEAR
	IDIVI E,^D4
	JUMPE F,IFL8	;DIVIBLE BY 100, NOT 400, NOT LEAP
IFL9:	AOS -1(P)	;DIVISIBLE BY 400, IS LEAP YEAR
IFL8:	POP P,E
	RET

;SUBROUTINE TO DETERMINE WHETHER DAYLIGHT SAVING TIME SHOULD BE
; APPLIED TO DATE BEING OUTPUT CONVERTED.
;CHANGE THIS ROUTINE FOR LOCAL VARIATIONS IN DAYLIGHT SAVINGS DATES.

;NOTE:**** THE COMMENTS IN THIS ROUTINE SAY "NLSAPR" AND "NLSOCT" FOR
; NEXT-TO-LAST SUNDAY. THE CODE IN FACT COMPUTES "LAST-SUNDAY" WHICH
; IS CORRECT. THE COMMENTS ARE REMNANTS FROM EARLIER CODE.

;TAKES: C: LOCALIZED SYSTEM FORMAT TIME
;	D: DAY OF YEAR (ALLOWING A DAY FOR FEB 29 ALWAYS)
;	AA: WHAT CAME IN LH D.
;	BB: LOCALIZED SYSTEM FORMAT DATE
;RETURNS IN AA: WHAT TO RETURN IN LH D: B1 ON FOR DAYL SAVING.
;CLOBBERS E,F,G.

;THIS VERSION PUTS DAYLIGHT SAVING INTO EFFECT AT 2AM ON THE
;LAST SUNDAY IN APRIL AND ENDS IT AT 1AM STANDARD TIME ON THE
;LAST SUNDAY IN OCTOBER.
; IT ALSO INCLUDES FORCED DAYLIGHT TIME FROM 6 JAN 74 THRU SUMMER 75.

ODAYL:	TLOE AA,B0	;THIS BIT ON IF CALLER SPECIFIED WHETHER OR
			;NOT TO USE DAYLIGHT SAVING IN B1.
	JRST ODAY9		;CALLER SPECIFIED, GO USE CURRENT B1 VALUE.
	TLZ AA,B1		;CLEAR DAYL BIT
	JSP E,DAYLFS		;CHECK DAYLIGHT FOR FUEL SHORTAGE
				; WON'T RETURN HERE IF FORCED SETTING.
	CALL NLSS		;COMPUTE DATE OF LAST SUNDAY IN
				;APRIL (NLSAPR) INTO E AND DITTO OF OCT
				;(NLSOCT) INTO F.
	CAMN BB,F		;DATE = NLSOCT?
	CAIGE C,^D<1*3600>	;YES, 1AM OR AFTER STD TIME?
	JRST .+2
	RET			;YES AND YES, STD TIME APPLIES.
ODAY6:	CAMN BB,E	;DATE = NLSAPR?
	CAIL C,^D<2*3600>	;YES, BEFORE 2AM?
	JRST .+2
	RET			;YES AND YES, STD TIME.
	;AT THIS POINT WE WOULD HAVE RETURNED IF IT WAS NLSAPR OR
	;NLSOCT AND TIME OF DAY WAS SUCH THAT STANDARD TIME APPLIED.
	;HENCE IF ITS BETWEEN NLSAPR AND NLSOCT INCLUSIVE, 
	;DAYLIGHT SAVING APPLIES.
	CAML BB,E		;DATE BEFORE NLSAPR?
	CAMLE BB,F		;OR AFTER NLSOCT?
	RET			;YES, STANDARD TIME APPLIES.
ODAY8:	TLO AA,B1		;SAY USE DAYLIGHT SAVINGS
ODAY9:	TLNE AA,B1
	AOS (P)			;SKIP IF DAYLIGHT SAVINGS SHOULD BE USED
	RET

;SUBROUTINE TO SKIP IF DAYLIGHT SAVING TIME SHOULD
;BE APPLIED TO DATE BEING INPUT CONVERTED.
;CHANGE THIS FOR LOCAL VARIATIONS.

;SAME AS ODAYL (PRECEDING) EXCEPT THAT TIME FOR SWITCHING BACK TO
;STANDARD TIME IS 2AM RATHER THAN 1AM BECAUSE TIME IN HAND IS
;DAYLIGHT IF BEFORE 2AM ON NLSOCT.

IDAYL:	TLOE AA,B0
	JRST ODAY9
	TLZ AA,B1
	JSP E,DAYLFS		;CHECK FOR FUEL-SHORTAGE DAYLIGHT LAW
				; DON'T RETURN HERE IF FORCED.
	CALL NLSS
	CAMN BB,F		;IS DATE NLSOCT?
	CAIGE C,^D<2*3600>	;YES, 2AM OR AFTER?
	JRST .+2
	RET			;YES AND YES, STANDARD TIME APPLIES.
	JRST ODAY6		;REST IS SAME AS OUTPUT CASE

;SUBROUTINE TO CHECK FOR THE FORCED DAYLIGHT TIME DECLARED BY CONGRESS
; FOR THE FUEL SHORTAGE FROM 6 JAN 74 THRU THE SUMMER OF 74.
; ALSO, FOR THE MODIFICATION WHEREIN WE GO BACK TO DAYLIGHT
; TIME IN LATE FEBRUARY OF 75 RATHER THAN LATE APRIL. AS THE LAW
; STANDS TODAY, WE REVERT TO APRIL-OCTOBER IN 1976, AND THIS CODE
; DOES THAT, TOO, BUT WATCH YOUR LOCAL CONGRESSMAN....

DSTFS1==122105		;DAYLIGHT SAVINGS TIME, FUEL SHORTAGE, START
			; DATE, WHICH IS 6 JAN 74 ( A SUNDAY ).
DSTFS2==122521		;DAYLIGHT SAVINGS TIME, FUEL SHORTAGE, END
			; DATE, WHICH IS 1 OCT 74, AND THE NORMAL
			; ALGORITHM TAKES OVER FOR REST OF THAT MONTH.
DSTFS3==122742		;23 FEB 75, WHEN WE GO BACK TO DAYLIGHT TIME
			; EARLY FOR SUMMER 75.
DSTFS4==123045		;1 MAY 75, WHEN THE NORMAL ALGORITHM TAKES OVER
			; AGAIN UNTIL THE LAW CHANGES AGAIN.

DAYLFS:	CAIN BB,DSTFS1		;THE DAY THIS PERIOD STARTS?
	CAIL C,^D<2*3600>	;YES. BEFORE 2 AM STANDARD TIME?
	SKIPA			;NOT MORNING OF THAT DAY.
	JRST ODAY9		;YES. FIRST HOURS OF THAT DAY. STD TIME.
	CAIGE BB,DSTFS1		;ANY TIME IN THIS FUDGED PERIOD?
	JRST 0(E)		;BEFORE 6 JAN 74. NORMAL ALGORITHM.
	CAIGE BB,DSTFS2		; ..
	JRST ODAY8		;YES. FORCE DAYLIGHT TIME.
	CAIN BB,DSTFS3		;23 FEB 75, FIRST DAY OF DST IN 75?
	CAIL C,^D<2*3600>	;YES. BEFORE 2 AM?
	SKIPA			;NO.
	JRST ODAY9		;YES. STILL STANDARD TIME.
	CAIL BB,DSTFS3		;IN THE PERIOD 23 FEB 75 0200 THRU
	CAIL BB,DSTFS4		;1 MAY 75?
	JRST 0(E)		;NO. USE NORMAL ALGORITHM
	JRST ODAY8		;YES. THIS IS FORCED DAYLIGHT TIME.

;COMPUTE DATES OF LAST SUNDAYS IN APRIL, OCTOBER.
;FOR ODAYL, IDAYL.

NLSS:
;DATE OF LAST SUNDAY IN APRIL THIS YEAR (NLSAPR) TO E
	MOVE E,BB		;DATE TODAY
	SUB E,D		;-DAY OF YEAR TODAY = BEG OF YEAR
	ADDI E,^D<31+29+31+30>-1	;DAY OF YEAR OF APRIL 30
	;HAVE DATE OF APR 30 THIS YEAR =LAST POSSIBLE DATE OF
	; LAST SUNDAY IN APRIL.
	;IF APR 30 ISN'T SUNDAY, FIND PRECEDING SUNDAY.
	ADDI E,DWFUDG+^D701	;+1 TO MAKE SUNDAY, NOT MONDAY, =0.
			;700 IS ARBITRARY MULTIPLE OF 7 TO MAKE SURE
			;QUANTITY IS POSITIVE DURING DIVISION EVEN
			;IN 1858, WHEN NLSAPR AND NLSOCT ARE NEGATIVE.
	IDIVI E,7	;DIVIDE INTO WEEKS AND DAY OF WEEKS
	IMULI E,7	;CONVERT BACK, DISCARDING DAY OF WEEK
	SUBI E,DWFUDG+^D701	;UNFUDGE AND WE'VE GOT IT!
;DATE OF LAST SUNDAY IN OCTOBER (NLSOCT) THIS YEAR TO F.
	MOVE F,BB
	SUB F,D
	ADDI F,^D<274+31>-1	;OCT 31
	ADDI F,DWFUDG+^D701
	IDIVI F,7
	IMULI F,7
	SUBI F,DWFUDG+^D701
	RET

;TIME ZONE SUBR FOR IDCNV, ODCNV
;GETS TIME ZONE GIVEN BY CALLER, OR DEFAULTS TO LOCAL AND RETURNS
; THAT TO CALLER OF IDCNV/ODCNV.
;TAKES: AA: WHAT CAME IN LH D. UPDATES SAME.
;RETURNS TIME ZONE, CONVERTED TO MINUTES, IN E.
;SKIP EXCEPT ON ILLEGAL TIME ZONE (ERROR CODE IN B).

CNVZON:	MOVE E,TIMZON	;LOCAL TIME ZONE
	TLOE AA,B2	;SKIP IF CALLER DIDN'T SPEciFY ZONE
	LDB E,[POINT 6,AA,17]	;GET CALLER-SPECIFIED ZONE
	DPB E,[POINT 6,AA,17]	;RETURN ZONE USED TO CALLER
	TRNE E,1B30
	ORCMI E,77	;EXTEND SIGN
	CAML E,[-^D12]
	CAILE E,^D12
	JRST [	MOVEI B,ZONEX1	;"ILLEGAL TIME ZONE"
		RET]
	IMULI E,^D3600	;CONVERT HOURS TO SECONDS
	AOS (P)
	RET


;SUBROUTINE TO FIX TIME AND ADJUST DATE APPROPRIATELY IF TIME IS
; NEGATIVE OR 24 HOURS OR MORE.
;CALLED IN IDCNV, ODCNV AFTER ADJUSTING FOR TIME ZONE.
;TAKES AND RETURNS:	C: TIME
;			BB: DATE

FIXDT:	JUMPGE C,.+3	;IS TIME NEGATIVE?
	SUBI BB,1	;YES, ITS YESTERDAY, SUBTRACT A DAY FROM DATE,
	ADDI C,^D<24*3600>	;AND ADD A DAY TO TIME.
	CAIGE C,^D<24*3600>	;IS TIME MIDNITE OR AFTER?
	JRST .+3		;(MIDNITE TODAY IS 0)
	ADDI BB,1	;YES, ITS TOMMORROW, ADD A DAY TO DATE,
	SUBI C,^D<24*3600>	;AND SUBTRACT A DAY FROM TIME.
	RET




	END


