Lasso Soft Inc. > Home

[valid_date]

Linkvalid_date
AuthorJolle Carlestam
CategoryDate
Version9.x
LicensePublic Domain
Posted13 Oct 2014
Updated13 Oct 2014
More by this author...

Description

Replacement of Lasso 9 valid_date that adds an optional param -strict. If 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 in ISO or US date format.

To use, place the file in the LassoStartup folder and restart Lasso 9.

NOTE: This method has a more modern version as a Gist.
https://gist.github.com/jolle-c/cef289d6ac3db1f9f6c0

Please use the Gist version instead
 

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)

Source Code

Click the "Download" button below to retrieve a copy of this tag, including the complete documentation and sample usage shown on this page. Place the downloaded ".inc" file in your LassoStartup folder, restart Lasso, and you can begin using this tag immediately.

month
Deals with a bug in the present date type that doesn't return correct values for date -> month(-days)
Written by Jolle Carlestam 2010-11-02
**/
define date -> month( -long::boolean = false, -short::boolean = false, -days::boolean = false ) => {

	#long ? return .format( '%B' )
	#short ? return .format( '%b' )
	if(#days)
		local(tempdate = date(.format('%Q')))
		#tempdate -> day = 1
		#tempdate -> add(-month = 1)
		#tempdate -> subtract(-day = 1)
		return #tempdate -> day
	/if

	return ..get(ucal_month)+1 
}



/**!
define 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.
Written by Jolle Carlestam 2010-11-02
**/
define valid_date(date, -format::string = '', -strict::boolean = false) => {

	// Empty input
	#date == null ? return(false)
	string(#date)->trim & == '' ? return(false)

	local(_format = #format -> ascopy)
	// Parse date
	local(parse) = (#format != '' ? date(#date, -format=#format) | date(#date))
	
	// Invalid dates
	(#parse->type == '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(#date) -> split(' ')) -> first) // get rid of the time part

		select(true)
			case(#_format == '%D') // US date format
				#date_array = #date_array -> split('/')
				(#date_array -> size != 3 ? return false)
				local(day = #date_array -> second)
				local(month = #date_array -> first)
				local(year = #date_array -> last)
			case(#_format == '%Q' || #_format == '') // 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)
				local(month = #date_array -> second)
				local(year = #date_array -> first)
			case
				return false
		/select

		(integer(#year) < date -> min(-year) || integer(#year) > date -> max(-year) ? return false)
		(integer(#month) < 1 || integer(#month) > 12 ? return false)
		(integer(#day) < 1 || 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)

		!#date -> isa('date') ? #date = (#format != '' ? date(#date, -format=#format) | date(#date))
		string(#date) == #parse->format(regexp(-find='%-?_?', -replace='%')->replaceall(#format)) ? return(true);
		string(#date) == #parse->format(regexp(-find='%-?_?', -replace='%_')->replaceall(#format)) ? return(true);
		string(#date) == #parse->format(regexp(-find='%-?_?', -replace='%-')->replaceall(#format)) ? return(true);
		return(false);

	/if;
	
	return(true);
}


?>

Comments

No comments

Please log in to comment

Subscribe to the LassoTalk mail list

LassoSoft Inc. > Home

 

 

©LassoSoft Inc 2015 | Web Development by Treefrog Inc | PrivacyLegal terms and Shipping | Contact LassoSoft