ex: date format is CCYY/MM/DD(2012/06/28) - I want to check whether the input date is valid.. (If the input date is valid then my output will be "20120628")
predicate should fail if it's not a valid date (ex: 1. 2012/00/29 - month is not valid. 2. 2012/01/37 - date is not valid)
I did this using my own logic for year/month and date..
The following is the one i tried,
Code: Select all
class predicates
getFormattedValidDate : (string, string) determ (i, o).
chkLeapYear : (unsigned) -> integer.
chkValidDayAndMonth : (string, string, integer) determ.
totDaysInMonth : (string, string) determ (i, o).
clauses
getFormattedValidDate("", ""):-
!.
getFormattedValidDate(Date,NewDate):-
fronttoken(Date,CCYY,_),
4 = length(CCYY),
MM = substring(Date,5,2),
DD = substring(Date,8,2),
Out = chkLeapYear(toUnsigned(CCYY)),
chkValidDayAndMonth(MM,DD,Out),
NewDate = format("%s%s%s",CCYY,MM,DD).
%return 1 if it is the leap year.
%And return 0 if it is not the leap year
chkLeapYear(Year) = 1:-
time_api::isLeapYear(Year),
!.
chkLeapYear(_) = 0.
chkValidDayAndMonth("02","29",1) :- !.
chkValidDayAndMonth(MM,DD,_) :-
totDaysInMonth(MM,Days),
DD <= Days.
totDaysInMonth("01","31"):- !.
totDaysInMonth("02","28"):- !.
totDaysInMonth("03","31"):- !.
totDaysInMonth("04","30"):- !.
totDaysInMonth("05","31"):- !.
totDaysInMonth("06","30"):- !.
totDaysInMonth("07","31"):- !.
totDaysInMonth("08","31"):- !.
totDaysInMonth("09","30"):- !.
totDaysInMonth("10","31"):- !.
totDaysInMonth("11","30"):- !.
totDaysInMonth("12","31").
Please advice.