CFLib.org – Common Function Library Project

CreateTimeStruct(timespan)

Last updated January 7, 2002

Version: 1 | Requires: ColdFusion 5 | Library: DateLib

 
Rated 3 time(s). Average Rating: 5.0

Description:
User supplies timespan (a number) and a mask of d, h, m, or s (day, hour, minute, second respectively) to denote the time unit timespan is measured in. Timespan is required, mask defaults to s. The function returns a structure of days, hours, minutes, and seconds equivalent to supplied timespan.

Return Values:
Returns a structure.

Example:

view plain print about
<cfset timestruct = CreateTimeStruct(21403, "m")>

<cfoutput>
21403 minutes = #timestruct.d# days, #timestruct.h# hours, #timestruct.m# minutes, #timestruct.s# seconds
</cfoutput>

Parameters:

Name Description Required
timespan The timespan to convert. Yes

Full UDF Source:

view plain print about
<cfscript>
/**
 * Converts a given number of days, hours, minutes, OR seconds to a struct of days, hours, minutes, AND seconds.
 * 
 * @param timespan      The timespan to convert. 
 * @return Returns a structure. 
 * @author Dave Pomerance (dpomerance@mos.org) 
 * @version 1, January 7, 2002 
 */

function CreateTimeStruct(timespan) {
    var timestruct = StructNew();
    var mask = "s";

    if (ArrayLen(Arguments) gte 2) mask = Arguments[2];

    // if timespan isn't an integer, convert mask towards s until timespan is an integer or mask is s
    while (Int(timespan) neq timespan AND mask neq "s"{
        if (mask eq "d"{
            timespan = timespan * 24;
            mask = "h";
        } else if (mask eq "h"{
            timespan = timespan * 60;
            mask = "m";
        } else if (mask eq "m"{
            timespan = timespan * 60;
            mask = "s";
        }
    }
    
    // only 4 allowed values for mask - if not one of those, return blank struct
    if (ListFind("d,h,m,s", mask)) {
        // compute seconds
        if (mask eq "s"{
            timestruct.s = (timespan mod 60) + (timespan - Int(timespan));
            timespan = int(timespan/60);
            mask = "m";
        } else timestruct.s = 0;
        // compute minutes
        if (mask eq "m"{
            timestruct.m = timespan mod 60;
            timespan = int(timespan/60);
            mask = "h";
        } else timestruct.m = 0;
        // compute hours, days
        if (mask eq "h"{
            timestruct.h = timespan mod 24;
            timestruct.d = int(timespan/24);
        } else {
            timestruct.h = 0;
            timestruct.d = timespan;
        }
    }
    
    return timestruct;
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Adam Cameron Adam Cameron added
savecontent
16 day(s) ago

Pete Ruckelshaus Pete Ruckelshaus added
firstDayOfWeek
a while ago

Chris Weller Chris Weller added
convertQueryStri...
a while ago

Dan Bracuk Dan Bracuk added
getExcelPercenti...
a while ago

Top Rated

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 41 time(s)

Barney Boisvert indentXml
Rated 5.0, 12 time(s)

Rachel Lehman deAccent
Rated 5.0, 8 time(s)

Markus Schneebeli                                 ListRemoveByStri...
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson