[LSD_String_ToDuration]

Description

Link: [LSD_String_ToDuration]
Author: Brad Lindsay
Category: String
Version: 8.5.x
License: Public Domain
Posted: Jun. 30, 2009
Updated: Jan. 01, 0001
More by this author...
This tag takes in a string and parses out time components for a duration. It then returns the duration it found. White space, time component order, and extraneous text are ignored. Only the first found instance of a time component is used.

Parameters

-a_string string, required The string to convert to a duration

Sample Usage

lsd_string_toduration('1d 17h 45m 22s');
lsd_string_toduration('Duration is 17h 1 d45 m22s');
lsd_string_toduration('Duration is 1 Day 17 Hours 45 Minutes and 22 Seconds');
lsd_string_toduration('17h 1 d45 m22s 0d0h0m0s');
						

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
<?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;
?>

 

Comments

none

Email:


Password:



Newest

Most Popular

Support tagSwap.net