CFLib.org – Common Function Library Project

convertTimeStringToSeconds(timeAsString[, workingHoursPerDay])

Last updated September 29, 2012

author

Simon Bingham

Version: 1 | Requires: CF9 | Library: DateLib

Description:
Takes a time string in "4d 12h 30m" format and converts to seconds. Also, takes an optional working hours per day argument as the function was originally used for timesheets.

Return Values:
An integer number of seconds

Example:

<cfoutput>
  <cfset timeasstring = "4d 12h 30m" />
  <p>Original Time: #timeasstring#</p>
 
  <p>Converted To Seconds: #convertTimeStringToSeconds( timeasstring )#</p>
</cfoutput>

Parameters:

Name Description Required
timeAsString String formatted in h/m/s, eg: 4d 12h 30m Yes
workingHoursPerDay Number of hours to consider "a day" No

Full UDF Source:

/**
 * Takes a time string in &quot;4d 12h 30m&quot; format and converts to seconds.
 * v1.0 by Simon Bingham
 * 
 * @param timeAsString      String formatted in h/m/s, eg: 4d 12h 30m (Required)
 * @param workingHoursPerDay      Number of hours to consider "a day" (Optional)
 * @return An integer number of seconds 
 * @author Simon Bingham (me@simonbingham.me.uk) 
 * @version 1.0, September 29, 2012 
 */
public numeric function convertTimeStringToSeconds( required string timeAsString, string workingHoursPerDay=24 ){
    // create a struct containing placeholder values for days, hours and minutes
    var timeStruct = { days=0, hours=0, minutes=0 };
    
    // create a variable to store the return value
    var timeInSeconds = "";
    
    // check the timeAsString argument has a length
    if( listLen( trim( arguments.timeAsString ), " " ) ){
        // loop through the values in the timeAsString argument
        for ( var i=1; i lte listLen( arguments.timeAsString, " " ); i=i+1 ){
            // if the current value ends in 'd' add the value to the 'days' element of our structure 
            if( right( listGetAt( arguments.timeAsString, i, " " ), 1 ) eq "d" ) {
                timeStruct.days = val( listGetAt( arguments.timeAsString, i, " " ) );
            // if the current value ends in 'h' add the value to the 'hours' element of our structure 
            }else if( right( listGetAt( arguments.timeAsString, i, " " ), 1 ) eq "h" ){
                timeStruct.hours = val( listGetAt( arguments.timeAsString, i, " " ) );
            // if the current value ends in 'm' add the value to the 'minutes' element of our structure 
            }else if( right( listGetAt( arguments.timeAsString, i, " " ), 1 ) eq "m" ){
                timeStruct.minutes = val( listGetAt( arguments.timeAsString, i, " " ) );
            }
        }
        
        // convert each of the structure elements to seconds and add them
        timeInSeconds = 
        ( timeStruct.days * ( arguments.workingHoursPerDay * 3600 ) )
        + ( timeStruct.hours * 3600 )
        + ( timeStruct.minutes * 60 );
    }
    
    // return the time in seconds
    return timeInSeconds;
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson