/***********************************************/
/* VX-Rexx Macro to parse dates                */
/* Please do not modify                        */
/***********************************************/
/* Custom mainline for macro */

    call RXFuncAdd "VRLoadFuncs", "VROBJ", "VRLoadFuncs"
    call VRLoadFuncs

    _VREVersion = SubWord( VRVersion( "VRObj" ), 1, 1 )
    if( _VREVersion < 2.02 )then do
        call VRMessage "", "This program requires VX-REXX version 2.0b to run.", "Error!"
        return 32000
    end

    signal on SYNTAX name _VRESyntax
    signal _VREMain

_VRESyntax:
    parse source . . _VRESourceSpec
    call VRMessage "", "Syntax error in" _VRESourceSpec "line" SIGL, "Error!"
    exit 32000

_VREMain:
/*:VRX         Main
*/
Main:
parse upper arg date direction form
if Length( date ) < 8 then
    signal dateError
position = Pos( "/", date, 1 )
if position = 0 then
    signal dateError
if Pos( '/', date, position + 1 ) = 0 then
    signal dateError
if form = 'O' then
    parse var date year '/' month '/' day
if form = 'E' then
    parse var date day '/' month '/' year
if form = 'U' then
    parse var date month '/' day '/' year

/*
 * Increment date by one
 */
if direction = 'UP' then do
    call GetMaxDay
    if day < Maxday then do
        day = day + 1
        signal SetDate
    end
    if day = Maxday then do
        if month <> '12' then do
            month = month + 1
            day = '01'
            signal SetDate
        end
        if month = '12' then do
            year = year + 1
            month = '01'
            day = '01'
            signal SetDate
        end
    end
    signal SetDate
end
/*
 * Decrement date by one
 */
if direction = 'DOWN' then do
    if day > 1 then do
        day = day - 1
        signal SetDate
    end
    if day = 1 then do
        if month <> 1 then do
            month = month - 1
            call GetMaxDay
            day = MaxDay
            signal SetDate
        end
        if month = 1 then do
            year = year - 1
            month = '12'
            day = '31'
            signal SetDate
        end
    end
    signal SetDate
end

GetMaxDay:
/*
 * Figure maximum number of days for given month
 */
maxday = 31
if( month = 4 | month = 6 | month = 9 | month = 11 )then do
    maxday = 30
    return
end
if( Pos( '.', year / 4 ) = 0 & month = 2 )then do
    maxday = 29
    return
end
if( Pos( '.', year / 4 ) <> 0 & month = 2 )then do
    maxday = 28
    return
end
return

SetDate:
/*
 * Return formatted date
 */
if Length( year ) = 1 then
    year = '0'||year
if Length( month ) = 1 then
    month = '0'||month
if Length( day ) = 1 then
    day = '0'||day
if form = 'O' then
    rc = year||'/'||month||'/'||day
if form = 'E' then
    rc = day||'/'||month||'/'||year
if form = 'U' then
    rc = month||'/'||day||'/'||year
call VRMethod 'Application', 'PutVar', 'rc'
exit

dateError:
if form = 'O' then
    call VRMessage "", "Incorrect date format -- yy/mm/dd  ", "Warning"
if form = 'U' then
    call VRMessage "", "Incorrect date format -- mm/dd/yy  ", "Warning"
if form = 'E' then
    call VRMessage "", "Incorrect date format -- dd/mm/yy  ", "Warning"
rc = 'ERROR'
call VRMethod 'Application', 'PutVar', 'rc'
exit
