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
|
<?LassoScript
define_tag('LSD_String_ToDuration', -required='a_string');
// This tag takes in inputs and tries to match numbers that are next to certain abbreviations.
// Based on that data, it returns a duration type.
//
// Whitespace is unimportant, the order is unimportant, extraneous text is unimportant,
//and only the first found instance of each time component is used
local('my_test');
//We're limiting this to certain abbreviations
//Feel free to add your abbreviations here
local('d_abbr') = array('d', 'day', 'days')->join('|');
local('h_abbr') = array('h', 'hour', 'hours', 'hr', 'hrs')->join('|');
local('m_abbr') = array('m', 'minute', 'minutes', 'min', 'mins')->join('|');
local('s_abbr') = array('s', 'second', 'seconds', 'sec', 'secs')->join('|');
//Testing days
#my_test = regexp(-find='(?i)(\\d+)\\s*(' + #d_abbr + ')(\\b|\\d|\\s)', -input=#a_string);
local('d') = (#my_test->find ? integer(#my_test->matchstring(1)) | 0);
//Testing hours
#my_test = regexp(-find='(?i)(\\d+)\\s*(' + #h_abbr + ')(\\b|\\d|\\s)', -input=#a_string);
local('h') = (#my_test->find ? integer(#my_test->matchstring(1)) | 0);
//Testing minutes
#my_test = regexp(-find='(?i)(\\d+)\\s*(' + #m_abbr + ')(\\b|\\d|\\s)', -input=#a_string);
local('m') = (#my_test->find ? integer(#my_test->matchstring(1)) | 0);
//Testing seconds
#my_test = regexp(-find='(?i)(\\d+)\\s*(' + #s_abbr + ')(\\b|\\d|\\s)', -input=#a_string);
local('s') = (#my_test->find ? integer(#my_test->matchstring(1)) | 0);
return(duration(-day=#d, -hour=#h, -minute=#m, -second=#s));
/define_tag;
?>
|