How test if Natural's own date (D) format valid

Hello again. :slight_smile: It’s the hard-headed one.

As part of my Date subroutine, I wish to test whether a Natural date has a valid format before trying to use it.

I did not see a specific mask though I hope one exists. I did try “IF #DATE-IN-D IS (D)” but that failed even valid Natural dates.

How is it done ?

Thank you again for your help. I do appreciate it.

What do you mean by “Natural date”? A D-format field? A System variable?

These fields are an internal representation of a date, where 1 is the beginning of time and 732877 is today, 7/21/06. The format is specified via edit mask; the default format is specified by Natural’s DTFORM parameter.

Internally D fields are considered P6 format, so I validate them with values in the range 577813 (1/1/1582) through 986153 (12/31/2699).

Ralph, thanks for your reply. Yes, I am referring to D-format fields.

Near as I can tell the field is 4 bytes long. Now suppose the subroutine is passed spaces, X’40404040’, in the #DATE-IN area, how do I test that the format is correct or not for a D-format field.

I did try “IF #DATE-IN-P = MASK(NNNZ)” but it didn’t appear to work correctly. Do you have a line of code you would use for this test ?

EDIT: I used MASK(NNNNNZ) and that worked!

Thanks for your help.

Your mask will work for valid dates, but to trap all invalid values, try

IF   #P = MASK (NNNNNNZ)          /* Up to 7 digits
 AND #P = 577813 THRU 986153                
  THEN                                      
    WRITE '  Valid date:' #D (EM=MM/DD/YYYY)
  ELSE                                      
    WRITE 'Invalid date:' #P (EM=HHHH)      
END-IF                                      

where #P is the packed field and #D is the D-format redefinition.

I see what you are saying.

Thanks, Ralph.

Don’t forget zero. It is a valid value, too!

The purpose of the IS operator is not to check whether the contents of a variable is in a specific format, but to check, whether the function VAL can transfer the value into a target of that format. As VAL only operates on alpha fields, also the left side of IS must be an alpha field.

See this code where the wrong usage of IS may lead to interesting results:

DEFINE DATA LOCAL
1 ALPHAVAR (A10)
1 REDEFINE ALPHAVAR
  2 NUMVAR (N10)
END-DEFINE
INPUT ALPHAVAR
IF ALPHAVAR IS (N10)
  ADD 0 TO NUMVAR
ELSE
  RESET NUMVAR
END-IF
DISPLAY NUMVAR
END

Watch the result if alphavar contains the value ‘1’!

Thanks, Matthias and Wilfried.