How to check a valid date?

Discussions related to Visual Prolog
danepa
VIP Member
Posts: 57
Joined: 29 Sep 2011 5:54

How to check a valid date?

Unread post by danepa » 22 Nov 2011 4:30

CreateID.pro

Code: Select all

predicates     onPushButtonClick : button::clickResponder. clauses     onPushButtonClick(_Source) = button::defaultAction :-                 Tgllahir = toTerm(tgllahir_ctl:gettext()), %date         Blnlahir = toTerm(blnlahir_ctl:gettext()), %monthdate         Thnlahir = toTerm(thnlahir_ctl:gettext()). %yeardate
How can i check it to be a valid date?

If i click pushbutton, if it is not a valid date, it will back to createID.pro again, but if it is a valid Date, it will go to login.pro.

So, how to check it? thanks...

I use VP 7.2
Daniel

User avatar
Tonton Luc
VIP Member
Posts: 502
Joined: 16 Oct 2001 23:01

Unread post by Tonton Luc » 22 Nov 2011 12:19

Hi,

Why don't you use a Calendar control to select a date ? In this case, the date selected by the user will always correct.
You can find a sample here : http://www.visual-prolog.com/vip/exampl ... lendar.htm

User avatar
abdelrahman
VIP Member
Posts: 65
Joined: 13 Sep 2009 6:23

my Dear Danepa , this was very old premitive class i did long period ago to validate date Input .

Unread post by abdelrahman » 23 Nov 2011 3:00

My Dear Danepa ,
this was very old premitive class i did long period ago to validate date Input .
It may seem fool or stupid but it performs work on personal Editions and check_date every time you need ,
it always keeps date in the format of dd_mm_yyyy .
your calling predicate should be
Validate_date (Input_date,Output_Validated_date).
wish it may be useful to you .
Attachments
validate_date.zip
(1.67 KiB) Downloaded 261 times

Harrison Pratt
VIP Member
Posts: 295
Joined: 5 Nov 2000 0:01

Unread post by Harrison Pratt » 23 Nov 2011 6:51

Check for leap year, old VIP 5.3 style:

Code: Select all

/****************************************************************************** dte_leapYear( INTEGER YearAsYYYY ) -(i)         Suceeds if YYYY is a leap year according to the following:            Rules for Leap Year:             (1) Year Divisible by 4 is a leap year, but             (2) year divisible by 100 is NOT a leap year, but             (3) year divisible by 400 is a leap year.          Software coded without rule (3) will be incorrect for the year 2000.          Software with incorrect coding of rule (3) might make February 2000 have 27 days or even 30 days.          Converting between a serialized date (e.g. number of days since 1980) and day          of month or day of week requires application of Leap Year rule.                   % 2.3.2000      Not rigorously tested yet.                                 Range of valid results for YYYY not checked or known. -----------------------------------------------------------------------------*/ dte_leapYear(YYYY):-    YYYY > 0,    0 =  YYYY mod 4,    0 <> YYYY mod 100,    0 =  YYYY mod 400.

User avatar
George
VIP Member
Posts: 313
Joined: 19 Sep 2011 8:54

Unread post by George » 23 Nov 2011 8:25

I would be suggesting that..

Create a three Edit Text Box and use those in the following manner..

1st Edit Text Box:
Use this one is for date.
Date can be the range from 1 to 31(set the condition to do this check)

date validation:

ex:

Code: Select all

date_validation(Date):-    if Date >=1 and Date <= 31, ! then         succeed().    else         vpiCommonDialogs::note("please enter the valid date")    end if.
2st Edit Text Box:
Use this one is for Month(Month can be max of 1 to 12)

ex:

Code: Select all

month_validation(Month):-    if Month >=1 and Month <= 12, ! then         succeed().    else         vpiCommonDialogs::note("please enter the valid Month")    end if.
3st Edit Text Box:
Use this one for Year. (give any range; according to your code).
ex:

Code: Select all

year_validation(Year):-    if Year >=2000 and Month <= 2011, ! then  %you can get the current year from the built in predicate; you can use that one..         succeed().    else         vpiCommonDialogs::note("please enter the valid Year")    end if.
and get those value from the text box, and perform the validation whether the date is valid or not..
Kind Regards,
George Ananth. S | Prolog Developer
georgeananth.prolog@gmail.com
+91 9791499282

Harrison Pratt
VIP Member
Posts: 295
Joined: 5 Nov 2000 0:01

Unread post by Harrison Pratt » 23 Nov 2011 16:12

A little more prolog style approach in VIP 5x:

Code: Select all

predicates valid_date( unsigned, unsigned, unsigned, unsigned, unsigned ) -(i,i,i,i,i)         month( unsigned, string, string, string, unsigned )         valid_d( unsigned, unsigned, unsigned ) -(i,i,i) clauses valid_date( YMin, YMax, M,D,Y ):-         Y <= YMax,         Y >= YMin,         valid_d( M, D, Y ), !. valid_date(  YMin, YMax, M,D,Y ):-         format( Title, "INVALID MM/DD/YYYY DATE: %02/%02/%04", M,D,Y),         format( Msg, "Invalid Date or out of Year Range % - %", YMin,YMax),         dlg_error(Title,Msg),         fail.                 valid_d( 2,D,YYYY ):- % is February Leap Year                   0 =  YYYY mod 4,                 0 <> YYYY mod 100,                 0 =  YYYY mod 400,                 LeapDays = 29,                 D >= 1,                 D <= LeapDays, !.         valid_d( 2, D, _Y ):-   % is February, non-leap year                 D >= 1,                 D <= 28, !.         valid_d( M,D,_Y ):-     % any other month, 1..12                 month(M,_,_,_,MaxDays),                 D >= 1,                 D <= MaxDays, !.                         month(1,"01","Jan","January",31):- !.         month(2,"02","Feb","February",29):- !.         month(3,"03","Mar","March",31):- !.         month(4,"04","Apr","April",30):- !.         month(5,"05","May","May",31):- !.         month(6,"06","Jun","June",30):- !.         month(7,"07","Jul","July",31):- !.         month(8,"08","Aug","August",31):- !.         month(9,"09","Sep","September",30):- !.         month(10,"10","Oct","October",31):- !.         month(11,"11","Nov","November",30):- !.         month(12,"12","Dec","December",31):- !.


Most of the information is in the month/5 clauses and you have a data structure you can use to reformat dates for display, calculation, etc.

User avatar
George
VIP Member
Posts: 313
Joined: 19 Sep 2011 8:54

Unread post by George » 24 Nov 2011 2:35

Dear Harrison Pratt,

I think for the leap year calculation we required one more condition..
valid_d( 2,D,YYYY ):- % is February Leap Year
0 = YYYY mod 4,
0 <> YYYY mod 100,
0 = YYYY mod 400,
LeapDays = 29,
D >= 1,
D <= LeapDays, !.
valid_d( 2, D, _Y ):- % is February, non-leap year
D >= 1,
D <= 28, !.
As per your above code, 2000 is not considered as a leap year although it is a leap year....

I think, we need to do the following check,

1. 0 = YYYY mod 4,
0 <> YYYY mod 100,
!,
LeapDays = 29.

2. 0 = YYYY mod 4,
0 = YYYY mod 100,
0 = YYYY mod 400,
!,
LeapDays = 29.

3. Days considered as the 28 as u written..

right..??
Kind Regards,
George Ananth. S | Prolog Developer
georgeananth.prolog@gmail.com
+91 9791499282

User avatar
Thomas Linder Puls
VIP Member
Posts: 1641
Joined: 28 Feb 2000 0:01

Unread post by Thomas Linder Puls » 24 Nov 2011 10:52

This will test the validity of a date:

Code: Select all

class predicates     isValidDate: (integer Year, integer Month, integer Date) determ. clauses     isValidDate(Y, M, D) :-         time::newDate(Y, M, D):getDate(Y1, M1, D1),         Y = Y1,         M = M1,         D = D1.
Also remember how to deal with Dialog/Form Validation.
Regards Thomas Linder Puls
PDC

Harrison Pratt
VIP Member
Posts: 295
Joined: 5 Nov 2000 0:01

Unread post by Harrison Pratt » 24 Nov 2011 14:01

Dear George,
I think for the leap year calculation we required one more condition..
Thank you, you are correct! I shouldn't post un-used and un-tested code (see comment in my first post).

Best regards,
Harrison

User avatar
George
VIP Member
Posts: 313
Joined: 19 Sep 2011 8:54

Unread post by George » 25 Nov 2011 11:09

Dear Thomas Linder Puls,
I'm wondering about your date validation.. It's working fine when i tried..

Code i tried is below with the console window.. ,

Code: Select all

clauses     classInfo(className, classVersion). class predicates     isValidDate : (integer, integer, integer) -> string. clauses     isValidDate(Year, Month, Date) = "date is valid":-         ObjTime = time::newDate(Year, Month, Date),         ObjTime:getDate(Y1, M1, D1),         Year = Y1,         Month = M1,         Date = D1,         !.         isValidDate(Year, Month, Date) = "Date is not valid". clauses     run():-         console::init(),         console::write("Please Enter the Year"),         console::nl(),         Year = console::readLine(),         console::write("Please Enter the Month"),                 console::nl(),         Month = console::readLine(),         console::write("Please Enter the Days"),         console::nl(),                 Date = console::readLine(),         Restult = isValidDate(toInteger(Year), toInteger(Month), toInteger(Date)),                 console::write(Restult),         _ = console::readLine(),                        succeed(). % place your own code here
Could you please tell me the "ObjTime:getDate(Y1, M1, D1)" predicate how to return the value for Month, YYYY, DD..

The logic is fine.. giving the perfect solution.. whereas don't understand that getDate/3 predicate..

Kindly clarify..

Thanks in advance..
Kind Regards,
George Ananth. S | Prolog Developer
georgeananth.prolog@gmail.com
+91 9791499282

User avatar
Thomas Linder Puls
VIP Member
Posts: 1641
Joined: 28 Feb 2000 0:01

Unread post by Thomas Linder Puls » 25 Nov 2011 14:04

time::getDate is trivial, it is time::newDate that does the real job.

time::newDate constructs a time object from a year, month and date, getDate returns the year, month and date of a time object.

But time::newDate is very flexible. You can for example say time::newDate(2000, 1, 32), i.e. the 32nd day in january 2000. This will create a date object that represents the 1st february 2000. 32/1-2000 is not a valid date, but it makes sense if you for example want to say one week after the 25th january (i.e. 25+7=32).

Given this flexibility, a valid date is one that returns itself, i.e. same year, month and date.

I will add a date validation predicate to the time class, for next version.
Regards Thomas Linder Puls
PDC

User avatar
George
VIP Member
Posts: 313
Joined: 19 Sep 2011 8:54

Unread post by George » 25 Nov 2011 14:31

Thank you very much Mr. Thomas Linder Puls :!: :!:

I got the perfect idea.. Thanks again..

It's really the awesome feature.. :)
Kind Regards,
George Ananth. S | Prolog Developer
georgeananth.prolog@gmail.com
+91 9791499282

Post Reply