Skip to content

Instantly share code, notes, and snippets.

@jolle-c
Last active August 29, 2015 14:07
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jolle-c/cef289d6ac3db1f9f6c0 to your computer and use it in GitHub Desktop.
Replacement of valid_date that adds an optional param -strict
[
/**!
valid_date
Replacement of valid_date that adds an optional param -strict. When set to true it will fail on dates that doesn't exist in the real world. Like 2010-02-31.
When used with strict assumes that date inputs are either as ISO or US date format.
2014-10-13 JC Added to Gist
2014-10-13 JC Adjusted code, added additional accepted format strings
2012-05-08 JC Slight modification of syntax including aligning format section with the latest from Lassosoft repo
2010-11-02 JC First version
Sample Usage:
valid_date('2010-02-28') //' true'
valid_date(date('2010-02-28'), -format = '%Q') // -> true
valid_date('02/14/2010') // -> true
valid_date('02/31/2010', -format = '%D') // -> true (should be false)
valid_date('02/14/2010', -format = '%D') // -> true
valid_date('2010-13-28') // -> true (should be false)
valid_date('6000-12-28') // -> true
valid_date('02/31/2010', -format = '%D', -strict) // -> false
valid_date('2010-02-28', -strict) // -> true
valid_date('02/14/2010', -format = '%D', -strict) // -> true
valid_date('02/37/2010', -format = '%D', -strict) // -> false
valid_date('2010-13-28', -strict) // -> false
valid_date('6000-12-28', -strict) // -> true
valid_date('02/31/2010', -format = '%D', -strict) // -> false (would be true without -strict)
valid_date('2010-13-28', -strict) // -> false (would be true without -strict)
**/
define valid_date(indate, -format::string = '', -strict::boolean = false) => {
// Empty input
#indate == null ? return(false)
string(#indate)->trim & == '' ? return(false)
local(
_format = #format -> ascopy,
// Parse date
parse = (#format != '' ? date(#indate, -format = #format) | date(#indate))
)
// Invalid dates
(#parse -> isa(::null)) ? return(false)
(string(#parse)->size == 0) ? return(false)
if(#strict) => {
// strict will check that a date actually exist in the real world
// find out input format, we can't convert input to a date type since that will change the input values if needed and we need to avoid that
local(date_array = (string(#indate) -> split(' ')) -> first) // get rid of the time part
match(#_format) => {
case('%D', 'MM/dd/yyyy') // US date format
#date_array = #date_array -> split('/')
(#date_array -> size != 3 ? return false)
local(
day = #date_array -> second,
month = #date_array -> first,
year = #date_array -> last
)
case('%Q', 'yMMdd', 'yyyyMMdd', 'yyyy-MM-dd', '') // assume ISO format since that is standard in Lasso 9
#date_array = #date_array -> split('-')
(#date_array -> size != 3 ? return false)
local(
day = #date_array -> last,
month = #date_array -> second,
year = #date_array -> first
)
case
return false
}
(integer(#year) < date -> min(-year) or integer(#year) > date -> max(-year) ? return false)
(integer(#month) < 1 or integer(#month) > 12 ? return false)
(integer(#day) < 1 or date(#year + '-' + #month + '-01') -> month(-days) < integer(#day) ? return false)
else(#format != '')
// Strict check of format against original date (allowing for leading zeroes or spaces)
string(#indate) == #parse->format(regexp(-find='%-?_?', -replace='%')->replaceall(-input=#format)) ? return(true)
string(#indate) == #parse->format(regexp(-find='%-?_?', -replace='%_')->replaceall(-input=#format)) ? return(true)
string(#indate) == #parse->format(regexp(-find='%-?_?', -replace='%-')->replaceall(-input=#format)) ? return(true)
return(false)
}
return(true)
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment