Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <cffunction name="testHarness">
- <cfargument name="myfunc" required="true">
- <cfset var dates = []>
- <cfset var x = "">
- <cfset var result = {}>
- <!--- first make the dates --->
- <cfloop index="x" from="1" to="100">
- <cfset arrayAppend(dates, dateAdd("d", randRange(-1000,1000), now()))>
- </cfloop>
- <cfloop index="x" from="1" to="#arrayLen(dates)#">
- <cfif dayOfWeek(dates[x]) neq myfunc(dates[x])>
- <cfset result.status = "fail">
- <cfset result.message = "Your function said the DOW for #dateformat(dates[x])# was #myfunc(dates[x])# and it should be #dayofweek(dates[x])#">
- <cfreturn result>
- </cfif>
- </cfloop>
- <cfset result.status = "pass">
- <cfreturn result>
- </cffunction>
- <cffunction name="whatDayIsThis" output="false">
- <cfargument name="d" type="date" required="true">
- <cfset var baseYear = 2000>
- <cfset var baseMonth = 1>
- <cfset var baseDay = 1>
- <cfset var baseDayOfWeek = 7>
- <cfset var dYear = Year(d)>
- <cfset var dMonth = Month(d)>
- <cfset var dDay = Day(d)>
- <cfset var nonLeapMonths = [31,28,31,30,31,30,31,31,30,31,30,31]>
- <cfset var dayOffset = 0>
- <cfset var daysOut = 0>
- <cfset var jumpedLeapDay = false>
- <cfoutput><h3>#dateFormat(d, "YYYY-MM-DD")#</h3></cfoutput>
- <!--- each year we add a day, each leapyear we add two = 5 every 4 years. A year falls on the same day every 24 (4*7) years [1,6,4,2,7,5,3,1...] --->
- <cfset yearSpan = dYear - baseYear>
- <!--- if we're ahead of base --->
- <cfif yearSpan GTE 0>
- AHEAD<br>
- <cfoutput>YearSpan: #yearSpan#<br></cfoutput>
- <!--- div 4 to get 4 year segments --->
- <cfset leap4 = ((yearSpan - 1) \ 4)>
- <cfif yearSpan GT 0><cfset leap4++></cfif>
- <cfoutput>Leap4: #leap4#<br></cfoutput>
- <!--- since years evenly divisible by 400 are not leap, check the span and correct the offset --->
- <cfset leap400 = (yearSpan \ 400)>
- <cfoutput>Leap400: #leap400#<br></cfoutput>
- <cfset daysOut = yearSpan>
- <cfoutput>DaysOut (with yearspan): #daysOut#<br></cfoutput>
- <cfset daysOut += leap4>
- <cfoutput>DaysOut (leaps): #daysOut#<br></cfoutput>
- <!--- calculate the days from the start of the year --->
- <cfloop from="1" to="#dMonth-1#" index="mon"><cfset dayOffset += nonLeapMonths[mon]></cfloop>
- <cfset dayOffset = dayOffset + dDay - baseDay>
- <cfoutput>DayOffset: #dayOffset#<br></cfoutput>
- <cfset daysOut += dayOffset>
- <cfoutput>DaysOut (with day offset): #daysOut#<br></cfoutput>
- <!--- if the d year is leap and we're past march-01 and we're ahead, add leap offset --->
- <cfif dYear % 400 NEQ 0 AND dYear % 4 EQ 0 AND dMonth GTE 3>
- <cfset daysOut++>
- <cfoutput>DaysOut (leap adjusted): #daysOut#<br></cfoutput>
- </cfif>
- <cfset daysOut = baseDayOfWeek + daysOut>
- <cfelse>
- BEHIND<br>
- <cfset yearSpan++>
- <cfoutput>YearSpan: #yearSpan#<br></cfoutput>
- <!--- div 4 to get 4 year segments --->
- <cfset leap4 = (yearSpan \ 4)>
- <cfoutput>Leap4: #leap4# [#yearSpan#]<br></cfoutput>
- <!--- since years evenly divisible by 400 are not leap, check the span and correct the offset --->
- <cfset leap400 = (yearSpan \ 400)>
- <cfoutput>Leap400: #leap400#<br></cfoutput>
- <cfset daysOut = yearSpan>
- <cfoutput>DaysOut (with yearspan): #daysOut#<br></cfoutput>
- <!--- add on the leap years --->
- <cfset daysOut += leap4>
- <cfoutput>DaysOut (leap4): #daysOut#<br></cfoutput>
- <!--- take off div 400 years --->
- <cfset daysOut -= leap400>
- <cfoutput>DaysOut (leap400): #daysOut#<br></cfoutput>
- <!--- calculate the days to the end of the year --->
- <cfloop from="#dMonth#" to="12" index="mon"><cfset dayOffset += nonLeapMonths[mon]></cfloop>
- <cfset dayOffset = dayOffset - dDay + baseDay>
- <cfoutput>DayOffset: #dayOffset#<br></cfoutput>
- <cfset daysOut = daysOut-dayOffset>
- <cfoutput>DaysOut (with day offset): #daysOut#<br></cfoutput>
- <!--- if the d year is leap and we're past march-01 and we're ahead, add leap offset --->
- <cfif dYear % 400 NEQ 0 AND dYear % 4 EQ 0 AND dMonth LT 3>
- <cfset daysOut-->
- <cfoutput>DaysOut (leap adjusted): #daysOut#<br></cfoutput>
- </cfif>
- <cfset daysOut = baseDayOfWeek + daysOut>
- </cfif>
- <!--- mod the offset to within a week --->
- <cfset daysOut = daysOut % 7>
- <cfoutput>DaysOut (mod 7): #daysOut#<br></cfoutput>
- <!--- if the offset is negative, add 7 to get positive --->
- <cfif daysOut LTE 0><cfset daysOut = 7 + daysOut></cfif>
- <cfoutput>DaysOut: #daysOut# [#dayOfWeek(d)#]<br></cfoutput>
- <cfreturn daysOut>
- </cffunction>
- <cfset res = testHarness(whatDayIsThis)>
- <cfdump var="#res#" label="The Result">
- <cfabort>
- <!--- test dummy test data
- <cfset whatDayIsThis(CreateDate(2000, 1, 1))><br>
- <cfset whatDayIsThis(CreateDate(2000, 2, 1))><br>
- <cfset whatDayIsThis(CreateDate(2000, 3, 1))><br>
- <cfset whatDayIsThis(CreateDate(1999, 12, 1))><br>
- <cfset whatDayIsThis(CreateDate(1999, 1, 1))><br>
- <cfset whatDayIsThis(CreateDate(1999, 2, 26))><br>
- <cfset whatDayIsThis(CreateDate(1997, 2, 26))><br>
- <cfset whatDayIsThis(CreateDate(1996, 2, 26))><br>
- <cfset whatDayIsThis(CreateDate(1996, 6, 26))><br>
- <cfset whatDayIsThis(CreateDate(1980, 1, 1))><br>
- <cfset whatDayIsThis(CreateDate(1988, 6, 26))><br>
- --->
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement