Announcement

Collapse
No announcement yet.

Checking *any* date for validity?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Michael Mattias
    replied
    You'll have to remove or change one line, but the DATEUNIT.BAS file supplied with the PB/DOS 3.5 compiler contains a function cleverly named "VaidDate" you can use.

    Leave a comment:


  • Tom Tallardy
    replied
    Robert -

    pretty "simple" approach would be

    Function pickaname (Dt$)
    'presuming Dt$="dd mmm yyyy" input format

    function=%true
    Dt$=trim$(Dt$)

    'optionally check format
    if mid$(Dt$,3,1)<>" " then function=%false:exit function
    if mid$(Dt$,7,1)<>" " then function=%false:exit function

    day%=val(left$(Dt$,2))
    if day%=0 then function=%false:exit function

    febdays%=28
    year%=val(mid$(Dt$,8))
    if year% mod 4=0 then febdays%=29
    if year% mod 400=0 then febdays%=29

    month$=mid$(Dt$,3,3)
    select case ucase$(month$)
    case "FEB"
    if day%>febdays% then function=%false
    case "JUN","APR","SEP","NOV"
    if day%>30 then function=%false
    case "JAN","MAR","MAY","JUL","AUG","OCT","DEC"
    if day%>31 then function=%false
    end select

    end function

    ------------------

    Leave a comment:


  • Michael Mattias
    replied
    WinAPI function GetdateFormat handles SYSTEMTIME structures, which handle dates back to year 1600. If it errors out with ERROR_INVALID_PARAMETER the date is no good (assuming you test the function with good dates and it works ok there).

    Oops, this is PB/DOS forum. WinAPI calls ain't gonna work. My bad.

    However, I think there might be some BIOS interupt you can use...
    Or maybe you could just..

    Code:
    FUNCTION isValidDate ( something) 
     LOCAL de AS INTEGER 
    
     ON LOCAL ERROR GOTO DateErrorTrap
     d$  =  DATE$  ' save current date 
     DATE$ "appropriate string representation of 'something'"
     DATE$  d$   ' restore 
    
     FUNCTION = (DE = 0)
     EXIT FUNCTION
    
    DateErrorTrap:
     DE = 1  
     RESUME NEXT
    
    END FUNCTION

    [This message has been edited by Michael Mattias (edited July 24, 2007).]

    Leave a comment:


  • Robert E. Carneal
    replied
    are you concerned strick about leap years?

    Yes, but I am also concerned and trying to trap any invalid date.
    31 Jun 1922
    00 Dec 1955
    99 Jul 1982
    04 Fib 1944
    31 Sep 1734
    20 Sip 1982
    07 10
    Are all invalid dates.
    All dates will be in format of dd Mmm YYYY.

    I am aware the calendar changed, but I am going to stick with our present calendar system. Simplifies things.

    Basically, what I am trying to do is come up with a formula in the least number of steps that checks to see if the date is valid.

    Thanks.

    Robert

    ------------------

    Leave a comment:


  • Tom Tallardy
    replied
    Robert -

    not entirely clear on what you are trying to accomplish - however leap years are fairly simple:

    if yr mod 4 = 0 it's a leap year
    except
    if yr mod 100=0 it's not a leap year
    except
    if yr mod 400=0 then it is a leap year

    which I think works for everything as long as you are sticking to the current Gregorian calendar "stuff"

    are you concerned strick about leap years?
    the nbr of days in each month does not change except for leap year Febs...


    ------------------

    Leave a comment:


  • Robert E. Carneal
    started a topic Checking *any* date for validity?

    Checking *any* date for validity?

    Checking a fairly recent date for validity (that is, to make certain the date says "30 Jun 1922" and not "31 Jun 1922" or "30 Jum 1922") is fairly easy for years after 1900.

    Before 1900, is there an easy way? For example, how would I verify that Feb 29, 1703 is a leap year? (Its not) Or that Apr 31, 1705 does exist? Right now, I add 2000 to the year to make it after 1900 and then check for validity. Is that the best way?

    Thanks.

    Robert



    ------------------
Working...
X