[lp_date_stringToOffset]

Description

Link: [lp_date_stringToOffset]
Author: Bil Corry
Category: Date
Version: 8.x
License: Public Domain
Posted: Mar. 02, 2006
Updated: Jan. 01, 0001
More by this author...
Returns a standardize timezone string (e.g. +0100) given a timezone offset string. Optionally can return a map of the hours and minutes.

Requires [lp_string_getnumeric] [lp_string_pad]

Parameters

-timezone_offset string, required The timezone offset string to standarize.
-map boolean, optional Return a map containing 'hours' and 'minutes'. Default is to return an offset string.

Sample Usage

var:'test' = (: '-0800', '-1445', '1445', '+0100', '0', '', null, '-', '9:00', '+12', '-5');
	iterate:$test, local:'t';
		#t ' ' (lp_date_stringtoOffset:#t);'<br>';
	/iterate;
	'<hr>';
	iterate:$test, local:'t';
		#t ' ' (lp_date_stringtoOffset:#t,-map);'<br>';
	/iterate;
						

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
[

define_tag:'lp_date_stringToOffset',
	-description='Returns a standardize timezone string (e.g. +0100) given a timezone offset string. Optionally can return a map of the hours and minutes.',
	-priority='replace',
	-required='timezone_offset', -copy,
	-optional='map', -copy;

	// Note: I use a workaround due to Lasso 8.1 bug where (integer: -08) == 0 instead of -8
	// So this is far more complicated that it should be :(

	if: local_defined:'map';
		#map = true;
	else;
		local:'map' = false;
	/if;

	#timezone_offset = string: #timezone_offset;

	local:'direction' = 1;
	local:'sign' = '+';
	if: #timezone_offset->(contains:'-');
		#direction = -1;
		#sign = '-';
	/if;

	// default at UTC/GMT
	local:'hours' = 0;
	local:'minutes' = 0;

	local:'offset' = lp_string_getnumeric: #timezone_offset;

	if: #offset->size == 0;
		// go with default
	else: #offset->size <= 2; // e.g. 5, 10, 3
		#offset->(removeleading:'0');
		#hours = #direction * (integer: #offset);
	else: #offset->size == 3;  // e.g. 500, 800, 300
		#hours = #direction * (integer: #offset->(substring: 1,1));
		#minutes = (integer: #offset->(substring: 2,2));
	else: #offset->size == 4; // e.g. 0500, 1300, 0130
		#hours = #direction * (integer: #offset->(substring: 1,2));
		#minutes = (integer: #offset->(substring: 3,2));
	else;
		fail: -1,'Offset not recognized.';
	/if;

	if: #map;
		return: (map: 'hours' = #hours, 'minutes' = #minutes);
	else;

		if: #hours == 0 && #minutes == 0;
			#sign = '+';
		/if;

		return: #sign (lp_string_pad: (math_abs:#hours), 2) (lp_string_pad: #minutes, 2);

	/if;

/define_tag;

]

 

Comments

none

Email:


Password:



Newest

Most Popular

Support tagSwap.net