[mv_clcdat]

Description

Link: [mv_clcdat]
Author: Marc Vos
Category: Date
Version: 8.x
License: Public Domain
Posted: Aug. 03, 2009
Updated: Aug. 03, 2009
More by this author...
This tag is for handling dates entered by users, showing dates to users and do some date calculations, like leapyear, last day of month, difference in days, hours, minutes. etc. Here are some examples of what it can do for you (left of the colon is the fed date, right side is the result):

Action = FS (From Screen = Entered by a user)
31082005 : 20050831
08312005 : 20053108
9082005 : 20050809
9.8.2005 : 20050809
9/8/2005 : 20050809
090805 : 20050809
90805 : 20050809
9.8.05 : 20050809
9/8/05 : 20050809
9-8-2005 : 20050809
9-8-05 : 20050809
zero : 0
null : 0


Action = TS (To Screen = coming from a database)
20051223 : 23-12-2005
2005-12-23 : 23-12-2005
2005.12.23 : 23-12-2005
2005/12/23 : 23-12-2005
zero :
null :


Action = EX (EXport - just strips non-numeric characters)
20051223 : 20051223
2005-12-23 : 20051223
2005.12.23 : 20051223
2005/12/23 : 20051223
51223 : 20051223
5/12/23 : 20051223
zero : 0
null : 0


Action = LY (LeapYear)
1996 : 29
19971200 : 28
19981223 : 28
1999-12-23 : 28
2000.12.23 : 29
2001/12/23 : 28
21223 : 28
3/12/23 : 28
4-12-23 : 29
zero : 0
null : 0


Action = LD (Last Day [of month])
20040223 : 20040229
2004-03-23 : 20040331
2004.04.23 : 20040430
2004/05/23 : 20040531
40623 : 20040630
4/07/23 : 20040731
zero : 0
null : 0


Action = ** (System date)
Today : 20090803


Action = DF (Difference in days, hours, minutes, seconds)
2004-02-12 08:00:00 - 2004-02-25 21:30:00 : array: (13), (13), (30), (00)
2004-02-25 21:30:00 - 2004-02-12 08:00:00 : array: (-13), (-13), (-30), (-00)
2000-01-01 21:30:00 - 2000-01-01 08:00:00 : array: (-0), (-13), (-30), (-00)
2000-01-01 21:30:00 - 2000-01-01 22:11:00 : array: (0), (00), (41), (00)

Parameters

-date1 string, optional Date to perform actions on
-date2 string, optional A second date to calculate the difference with
-action string, required The action to perfom

Sample Usage

[
mv_clcdat(-date1='2004-02-12 08:00:00', -date2='2004-02-25 21:30:00', -action='DF');
mv_clcdat(-date1='9.8.05', -action='FS');
]
						

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<?lassoscript
// Date formatting to and from screen fields
// For Lasso 8 and higher.
// Copyright (c) 2005-... by MHE Vos (http://marc.vos.net)

// Put this file in the LassoStartup folder of the SERVER folder.
// Restart Lasso on the console by entering 'sudo lasso8ctl restart'

define_tag('mv_clcdat', -optional='date1', -optional='date2', -required='action');
	local('result') = '';
	local('tmp') = '';
	local('num') = 0;
	local('myDate') = (array);
	local('myMDAYS') = array('31','28','31','30','31','30','31','31','30','31','30','31');
	local('zzdate') = '';

	if(#action == '**');
		// ** = Current date
		// In:  nothing
		// Out: YYYYMMDD

		#result = date->format('%Y%m%d');

	else((#action == 'AN') || (#action == 'EX') || (#action == 'LD') || (#action == 'LY'));
		// AN = Alpha to Numeric
		// In:  DATE1 = a Date YYYY-MM-DD with or without a time HH:MM:SS, separated by a space : "YYYY-MM-DD HH:MM:SS"
		// Out: YYYYMMDDHHMMSS

		// EX = Export, meant for creating ascii files
		// In:  DATE1 = a Date from a database DATE-type field, like yyyy-mm-dd
		// Out: YYYYMMDD
		
		// LD = Last day of month
		// In:  DATE1 = a Date from a database field, like YYYYMMDD or YYYY-MM-DD or YYYY.MM.DD or YYYY/MM/DD
		// Out: YYYYMMDD where DD = 28/29, 30 or 31.

		// LY = Leap year test
		// In:  DATE1 = a Date from a database field, like YYYYMMDD or YYYY-MM-DD or YYYY.MM.DD or YYYY/MM/DD
		//		  or a year, like YYYY.
		// Out: 29 (true) / 28 (false)

		#result = '';
		// get only the numbers
		loop(#date1->size);
			#tmp = #date1->get(loop_count);
			if((#tmp >= '0') && (#tmp <= '9'));
				#result = #result + #tmp;
			/if;
		/loop;
			
		// check the length
		if(#result->size <= 4);
			#result->padleading(4, '0');
			if(integer(#result) != 0);
				#result = #result + '0101';
			/if;
		else(#result->size <= 6);
			#result->padleading(6, '0');
			if(integer(#result) != 0);
				#result = '20' + #result;
			/if;
		else(#result->size <= 8);
			#result->padleading(8, '0');
		else(#result->size > 8);
			if(#action == 'AN');
				#result = #result->substring(1,14);
			else;
				#result = #result->substring(1,8);
			/if;
		/if;
		
		if(integer(#result) == 0);
			#result = '0';
		else((#action == 'LY') || (#action == 'LD'));
			#num = integer(#result->substring(1,4));
			if(((math_mod(#num, 4) == 0) && (math_mod(#num, 100) != 0)) || (math_mod(#num, 400) == 0));
				#tmp = '29';
			else;
				#tmp = '28';
			/if;
			
			if(#action == 'LD');
				#myMDAYS->get(2) = #tmp;
				#result = #result->substring(1,6) + #myMDAYS->get(integer(#result->substring(5,2)));
			else;
				#result = #tmp;
			/if;
		/if;
		
	else((#action == 'FS') || (#action == 'FSD'));
		// FS = From Screen (FSD = FS + force date)
		// In:  DATE1 = a Date entered by a user, like dd/mm/yy or dd-mm-yyyy or ddmmyy
		//				or a period like mmyyyy or mm-yyyy or mm/yyyy
		// Out: YYYYMMDD

		#zzdate = #date1;
		if(#zzdate->size > 4);
			// Check if day is two digits (1-1-2006)
			#tmp = #zzdate->get(2);
			if('0123456789' !>> #tmp);
				#zzdate = '0' + #zzdate;		// 01-1-2006
			/if;

			// Check if month is two digits (01-1-2006)
			#tmp = #zzdate->get(5);
			if('0123456789' !>> #tmp);
				#zzdate = #zzdate->substring(1,3) + '0' + #zzdate->substring(4); // 01-01-2006
			/if;
		/if;

		#result = '';
		// get only the numbers
		loop(#zzdate->size);
			#tmp = #zzdate->get(loop_count);
			if((#tmp >= '0') && (#tmp <= '9'));
				#result = #result + #tmp;
			/if;
		/loop;
			
		// check the length
		if(#result->size <= 6);
			#result->padleading(6, '0');
			// If it is not a month, we got MMYYYY
			if(integer(#result->substring(3,2)) > 12);
				#result->padleading(8, '0');
			/if;
		else(#result->size <= 8);
			#result->padleading(8, '0');
		else(#result->size > 8);
			#result = #result->substring((#result->size)-7);
		/if;
			
		if(integer(#result) == 0);
			#result = '0';
		else;
			// add valid separators
			#result = #result->substring(1,2) + '/' + #result->substring(3,2) + '/' + #result->substring(5);
		/if;

		// Now we should have a date like d/m/y
		// Only problem is: Lasso sees it as an American date: m/d/y
		if(string_findposition(#result, -find='/') != '-1');
			#myDate = #result->split('/');

			#myDate->get(1)->padleading(2, '0');
			#myDate->get(2)->padleading(2, '0');
			#myDate->get(3)->padleading(2, '0');

			if(#action == 'FSD');
				if(integer(#myDate->get(1)) == 0); #myDate->get(1) = date->day; /if;
				if(integer(#myDate->get(2)) == 0); #myDate->get(2) = date->month; /if;
				if(integer(#myDate->get(3)) == 0); #myDate->get(3) = date->year; /if;
			/if;

			if((#myDate->get(3)->size) < 3);
				#myDate->get(3) = '20' + #myDate->get(3);
			/if;
			if(integer(#myDate->get(1)) == 0);
				#result = #myDate->get(3) + #myDate->get(2);	// YYYYMM
			else;
				#result = #myDate->get(3) + #myDate->get(2) + #myDate->get(1);	// YYYYMMDD
			/if;
		/if;
		
	else((#action == 'TS') || (#action == 'NA'));
		// TS = To Screen
		// In:  DATE1 = a Date from a database field, like YYYYMMDD or YYYY-MM-DD or YYYY.MM.DD or YYYY/MM/DD
		//				or a period like yyyymm or yyyymm or yyyy/mm
		// Out: DD-MM-YYYY

		// NA = Numeric to Alfa
		// In:  DATE1 = a Date from a database field, like YYYYMMDD or YYYY-MM-DD or YYYY.MM.DD or YYYY/MM/DD
		//				or a period like yyyymm or yyyymm or yyyy/mm
		// Out: YYYY-MM-DD or blanks when date1 is zero.
				
		// Try to make it a valid date: y/m/d to y-m-d
		#result = string_replace(#date1, -find='/', -replace='-');
				
		// Try to make it a valid date: y.m.d to y-m-d
		#result = string_replace(#result, -find='.', -replace='-');

		if(integer(#result) == 0);
			#result = '';
		else;
			// add valid separators
			if(string_findposition(#result, -find='-') != '-1');
				#myDate = #result->split('-');
				if(#action == 'NA');
					if(#myDate->size < 3);	// YYYY-MM
						#result = date_format(#result, -format='%Y-%m');
					else;
						#result = date_format(#result, -format='%Y-%m-%d');
					/if;
				else;
					if(#myDate->size < 3);	// YYYY-MM
						#result = date_format(#result, -format='%m-%Y');
					else;
						#result = date_format(#result, -format='%d-%m-%Y');
					/if;
				/if;
			else;
				#result = string(integer(#result));
				select(#result->size);
				case(1);
					if(#action == 'NA');
						#result = date->format('%Y') + '-' + '0' + #result;		// yyyy-mm
					else;
						#result = '0' + #result + '-' + date->format('%Y');		// mm-yyyy
					/if;
				case(2);
					if(#action == 'NA');
						#result = date->format('%Y') + '-' + #result;		// yyyy-mm
					else;
						#result = #result + '-' + date->format('%Y');		// mm-yyyy
					/if;
				case(3);
					if(#action == 'NA');
						#result = date->format('%Y') + '-0' + #result->substring(1,1) + '-' + #result->substring(2,2);		// mmd -> yyyy-mm-dd
					else;
						#result = #result->substring(2,2) + '-0' + #result->substring(1,1) + '-' + date->format('%Y');		// mmd -> dd-mm-yyyy
					/if;
				case(4);
					if(#action == 'NA');
						#result = date->format('%Y') + '-' + #result->substring(1,2) + '-' + #result->substring(2,2);		// mmd -> yyyy-mm-dd
					else;
						#result = #result->substring(3,2) + '-' + #result->substring(1,2) + '-' + date->format('%Y');		// mmdd -> dd-mm-yyyy
					/if;
				case(5);
					if(#action == 'NA');
						#result =  #result->substring(1,4) + '-' + '0' + #result->substring(5,1);		// yyyym -> yyyy-mm
					else;
						#result = '0' + #result->substring(5,1) + '-' + #result->substring(1,4);		// yyyym -> mm-yyyy
					/if;
				case(6);
					if(#action == 'NA');
						#result = #result->substring(1,4) + '-' + #result->substring(4,2);				// yyyymm / yyyyww -> yyyy-xx
					else;
						#result = #result->substring(4,2) + '-' + #result->substring(1,4);				// yyyymm / yyyyww -> xx-yyyy
					/if;
				case(7);
					if(#action == 'NA');
						#result = #result->substring(1,4) + '-' + #result->substring(5,3);		// yyyyddd -> yyyy-ddd
					else;
						#result = #result->substring(5,3) + '-' + #result->substring(1,4);		// yyyyddd -> ddd-yyyy
					/if;
				case;
					if(#action == 'NA');
						#result = #result->substring(1,4) + '-' + #result->substring(5,2) + '-' + #result->substring(7,2); // yyyymmdd -> yyyy-mm-dd
					else;
						#result = #result->substring(7,2) + '-' + #result->substring(5,2) + '-' + #result->substring(1,4); // yyyymmdd -> dd-mm-yyyy
					/if;
				/select;
			/if;
		/if;
		
	else(#action == 'DF');
		// DF = Difference
		// In:  DATE1 = a Date YYYY-MM-DD with or without a time HH:MM:SS, separated by a space : "YYYY-MM-DD HH:MM:SS"
		// In:  DATE2 = a Date YYYY-MM-DD with or without a time HH:MM:SS, separated by a space : "YYYY-MM-DD HH:MM:SS"
		// Out: array(days, hours, minutes, seconds), HMS are padded leading with zeros.

		if(#date1 <= #date2);
			#zzdate = 0;
			#tmp = date(#date1)->difference(date(#date2));
		else;
			#zzdate = 1;
			#tmp = date(#date2)->difference(date(#date1));
		/if;
		#num = (duration(#tmp)->hour) / 24;
		#tmp = #tmp - duration(-day=#num);
		#result = string(string(#num) + ':' + string(#tmp))->split(':');
		
		if(#zzdate == 1);
			#result->get(1) = '-' + #result->get(1);
			#result->get(2) = '-' + #result->get(2);
			#result->get(3) = '-' + #result->get(3);
			#result->get(4) = '-' + #result->get(4);
		/if;
		
		#result->get(2)->padleading(2, '0');
		#result->get(3)->padleading(2, '0');
		#result->get(4)->padleading(2, '0');
		
	/if;
	
	return(#result);
/define_Tag;
?>

 

Comments

none

Email:


Password:



Newest

Most Popular

Support tagSwap.net