check if an item is a date
Rob Cozens
rcozens at pon.net
Thu Mar 17 10:59:27 EST 2005
This is nothing new, Xavier,
>But surprise!
>
>put (2485694 is a date)
>
>It gets worse... 1 is also a date!
>
>So either the << is a date>> function is useless as a date checker and we
>all must roll our own AGAIN
Virtually any integer represents a date in seconds format in xTalks; so if
you need need to validate an input string as a valid date (as per system
date format), you need more:
From Serendipity Library <http://wecode.org/serendipity/>:
function validDate theString -- 5 April 04:RCC
put stripBlanks(theString,false) into theString
get systemDateFormat()
set the itemDelimiter to char -1 of it -- theSeparator
if the number of items of theString <> 3 then return false
delete the last char of it
delete char offset("mm",it) of it -- remove double characters, if any
delete char offset("dd",it) of it
delete char offset("yy",it) of it
repeat with x = 1 to 3
switch char x of it
case "m"
put item x of theString into theMonth
if not validDigits(theMonth) then return false
if theMonth < 1 or theMonth > 12 then return false
break
case "d"
put item x of theString into theDay
if not validDigits(theDay) then return false
if theDay < 1 or theDay > 31 then return false
break
case "y"
put item x of theString into theYear
if not validDigits(theYear) then return false
put length(theYear) into charCount
if charCount <> 2 and charCount <> 4 then return false
if charCount is 2 then add 2000 to theYear
if theYear < 1 then return false
break
end switch
end repeat
if theDay < 29 then return
true&return&theYear&comma&theMonth&comma&theDay&",0,0,0,0"
if theMonth is 2 then
if theDay > 29 or not leapYear(theYear) then return false
else return true&return&theYear&comma&theMonth&comma&theDay&",0,0,0,0"
else
if offset("0",theMonth) = 1 then delete char 1 of theMonth
set the itemDelimiter to comma
if theMonth is among the items of "1,3,5,7,8,10,12" then return
true&return&theYear&comma&theMonth&comma&theDay&",0,0,0,0"
if theDay is 31 then return false
else return true&return&theYear&comma&theMonth&comma&theDay&",0,0,0,0"
end if
end validDate
Note this uses the systemDateFormat function I posted yesterday, plus other
Library handlers.
Rob Cozens CCW
Serendipity Software Company
"And I, which was two fooles, do so grow three;
Who are a little wise, the best fooles bee."
from "The Triple Foole" by John Donne (1572-1631)
More information about the use-livecode
mailing list