« Return to Thread: Does Such A Tag Already Exist?

Re: Does Such A Tag Already Exist?

by Brad Lindsay-2 :: Rate this Message:

Reply to Author | View in Thread

On Jun 30, 2009, at 1:28 PM, Brad Lindsay wrote:
> Before I go off and write this tag myself, I was wondering if a tag  
> existed to parse / cast into a duration a string of the following  
> format: '1d 17h 45m 22s'
> I haven't found any searching through tagSwap, but I thought I might  
> be missing something.

I finished the tag, and made some modifications and have posted it to  
tagSwap: http://tagswap.net/LSD_String_ToDuration/

Below is the final code that allows for multiple different types of  
abbreviations for each time component.  Please feel free to review and  
point out any uncaught bugs or errors.

Thanks,
-Brad


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



--
This list is a free service of LassoSoft: http://www.LassoSoft.com/
Search the list archives: http://www.ListSearch.com/Lasso/Browse/
Manage your subscription: http://www.ListSearch.com/Lasso/


 « Return to Thread: Does Such A Tag Already Exist?