; CHGDATE
;
; Program for Proteus
;
; (C) 1998-2004 Simone Zanella Productions
;
; Change each date from the format 'dd/mm/yy' to the format 'dd mmm yyyy';
; the date must use at least one of the separators in Seps (which must also include ' ').
; All the tokens are used just once; no library function on dates is exploited.
; See chgdat2.prt for a similar (but faster) algorithm.
; Initialize variables
FUNCTION ONSTART()
IF STREQ(ARGV(3), "..")
CONSOLELN "Syntax: " ARGV(1) " " ARGV(2) " source destination"
CONSOLELN "Purpose: change dates from numeric to alphanumeric"
ABORT 0
FI
_Seps = " ,;:.()[]<>-{}"
_MONTHS = "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"
; EPOCH is used to determine if year is 20xx or 19xx;
; values below EPOCH are considered above 2000,
; values above are considered before 2000.
_EPOCH = 70
RETURN
; Find out the number of tokens in line
NTok = NUMTOKEN(L, Seps)
CurTok = 1
NewL = STRDUP(L)
; For each token...
WHILE LE(CurTok, NTok)
PL = TOKEN(L, CurTok, Seps)
; Is token a date?
IF REXMATCH(PL, "^[0-3][0-9]/[0-1][0-9]/[0-9][0-9]$")
; Faster test, using IMATCH (no check on numbers):
; IF IMATCH(PL, "??/??/??")
DD = LEFT(PL, 2)
MM = SUBSTR(PL, 4, 2)
YY = RIGHT(PL, 2)
; Check that day and month are within the allowed interval
; (does not check that the month can actually have the
; specified number of days)
IF ValidMMDD(DD, MM)
; Extract from MONTHS the string corresponding to the month
MMS = " " TOKEN(MONTHS, MM, " ") " "
; Add due digits for year
IF GT(YY, EPOCH)
YY = "19" YY
ELSE
YY = "20" YY
FI
; Store new date to NPL
NPL = DD MMS YY
; Find where the date is in NewL
P = POSTOKEN(NewL, CurTok, Seps)
; Replace date in NewL
NewL = INSERT(DELETE(NewL, P, 8), P, NPL)
; We have added two tokens: update CurTok and NTok
ADD(@CurTok, 2)
NTok = ADD(CurTok, 2)
FI
FI
INC(@CurTok)
LOOP
PRINTLN NewL
FUNCTION ValidMMDD(dd, mm)
IF AND(GE(dd, 1), LE(dd, 31))
IF AND(GE(mm, 1), LE(mm, 12))
RETURN 1
FI
FI
RETURN 0