[valid_date]

Description

Link: [valid_date]
Author: Jolle Carlestam
Category: Date
Version: 9.x
License: Public Domain
Posted: Nov. 03, 2010
Updated: Nov. 03, 2010
More by this author...

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.

Parameters

none


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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?LassoScript

/**!
define date->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

none

Email:


Password:



Newest

Most Popular

Support tagSwap.net