Created
July 24, 2011 23:02
-
-
Save boughtonp/1103217 to your computer and use it in GitHub Desktop.
modified version of getEveryDOW from http://cflib.org/udf/getEveryDOW to support a range of dates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getEveryDOW( DowList , StartDate , EndDate ) | |
{ | |
var x = 0; | |
var ThisDow = 0; | |
var DayToAdd = 0; | |
var Result = ArrayNew(1); | |
var Day1 = Arguments.StartDate; | |
var InitialDow = InitialDow = DayOfWeek(Day1); | |
while( Day1 LT Arguments.EndDate ) | |
{ | |
for( x=1 ; x LTE ListLen(Arguments.DowList) ; x=x+1 ) | |
{ | |
ThisDOW = ListGetAt(Arguments.DowList, x); | |
DayToAdd = DateAdd( "d" , ThisDOW - InitialDow, Day1 ); | |
if ( DayToAdd GTE Arguments.StartDate | |
AND DayToAdd LTE Arguments.EndDate | |
) | |
{ | |
ArrayAppend( Result , DayToAdd ); | |
} | |
} | |
Day1 = DateAdd( "ww" , 1 , Day1 ); | |
} | |
return Result; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cffunction name="getEveryDayOfWeek" returntype="Array" output="false"> | |
<cfargument name="DayList" type="String" required /> | |
<cfargument name="StartDate" type="Date" required /> | |
<cfargument name="EndDate" type="Date" required /> | |
<cfset var Result = [] /> | |
<!--- | |
Compile array of steps. | |
---> | |
<cfset var DaySteps = [] /> | |
<cfset var PrevDayNum = ListFirst(Arguments.DayList) /> | |
<cfset var CurDay = 0 /> | |
<cfloop index="CurDay" list="#ListRest(Arguments.DayList)#"> | |
<cfset ArrayAppend( DaySteps , CurDay - PrevDayNum ) /> | |
<cfset PrevDayNum = CurDay /> | |
</cfloop> | |
<cfset ArrayAppend( DaySteps , ListFirst(Arguments.DayList) - (ListLast(Arguments.DayList)-7) ) /> | |
<cfset var CurDay = Arguments.StartDate /> | |
<!--- | |
Find starting position | |
---> | |
<cfloop condition="NOT ListContains(Arguments.DayList,DayOfWeek(CurDay))"> | |
<cfset CurDay++ /> | |
</cfloop> | |
<cfset var StepPos = ListFind(Arguments.DayList,Day(CurDay)) /> | |
<!--- | |
Step through adding days. | |
---> | |
<cfset var Result = [] /> | |
<cfset var EndDays = Int(EndDate) /> | |
<cfloop condition="CurDay LT EndDays"> | |
<cfset ArrayAppend( Result , CurDay ) /> | |
<cfset StepPos++ /> | |
<cfif StepPos GT ArrayLen(DaySteps) > | |
<cfset StepPos = 1 /> | |
</cfif> | |
<cfset CurDay += DaySteps[StepPos] /> | |
</cfloop> | |
<cfreturn Result /> | |
</cffunction> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cffunction name="getEveryDayOfWeek" returntype="Array" output="false"> | |
<cfargument name="DayList" type="String" required /> | |
<cfargument name="StartDate" type="Date" required /> | |
<cfargument name="EndDate" type="Date" required /> | |
<cfset var Result = [] /> | |
<!--- | |
Compile array of steps. | |
---> | |
<cfset var DaySteps = [] /> | |
<cfset var PrevDayNum = ListFirst(Arguments.DayList) /> | |
<cfset var CurDay = 0 /> | |
<cfloop index="CurDay" list="#ListRest(Arguments.DayList)#"> | |
<cfset ArrayAppend( DaySteps , CurDay - PrevDayNum ) /> | |
<cfset PrevDayNum = CurDay /> | |
</cfloop> | |
<cfset ArrayAppend( DaySteps , ListFirst(Arguments.DayList) - (ListLast(Arguments.DayList)-7) ) /> | |
<cfset var CurDay = Arguments.StartDate /> | |
<!--- | |
Find starting position | |
---> | |
<cfwhile NOT ListContains(Arguments.DayList,DayOfWeek(CurDay))> | |
<cfset CurDay++ /> | |
</cfwhile> | |
<cfset var StepPos = ListFind(Arguments.DayList,Day(CurDay)) /> | |
<!--- | |
Step through adding days. | |
---> | |
<cfset var Result = [] /> | |
<cfset var EndDays = Int(EndDate) /> | |
<cfwhile CurDay LT EndDays > | |
<cfset ArrayAppend( Result , parseDateTime(CurDay) ) /> | |
<cfset StepPos++ /> | |
<cfif StepPos GT ArrayLen(DaySteps) > | |
<cfset StepPos = 1 /> | |
</cfif> | |
<cfset CurDay += DaySteps[StepPos] /> | |
</cfwhile> | |
<cfreturn Result /> | |
</cffunction> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Added udf_getEveryDayOfWeek.cfm which seems to be approx twice as fast (when running both on Railo 3.3, at ~95 vs 180 for a thousand iterations), and nearly three times as fast when both on ACF9.0.1 (~130 vs 360) - however, ACF version is not converting number back to date (since it threw an error on the parseDateTime there).