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>
- <cfset var gregOffset = 0>
- <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 100 are not leap, check the span and correct the offset --->
- <cfset leap100 = (yearSpan \ 100)>
- <cfoutput>Leap100: #leap100#<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 (leap4): #daysOut#<br></cfoutput>
- <cfset daysOut -= leap100>
- <cfoutput>DaysOut (leap100): #daysOut#<br></cfoutput>
- <cfset daysOut += leap400>
- <cfoutput>DaysOut (leap400): #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 EQ 0) OR (dYear % 100 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++>
- <!--- the Julian calendar gave way to the Gregorian calendar over the period
- Thursday, 4 October 1582 and this was followed by the first day of the Gregorian calendar, Friday, 15 October 1582
- http://en.wikipedia.org/wiki/Gregorian_calendar
- --->
- <cfif dYear LT 1582
- OR (dYear EQ 1582 AND dMonth LT 10)
- OR (dYear EQ 1582 AND dMonth EQ 10 AND dDay LTE 4)>
- <cfset gregOffset = 9>
- <!--- the max year span is 1582, before which leap years didn't exist --->
- <cfset leapYearSpan = baseYear - 1582>
- <cfelse>
- <cfset leapYearSpan = yearSpan>
- </cfif>
- <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 leap100 = (leapYearSpan \ 100)>
- <cfoutput>Leap100: #leap100#<br></cfoutput>
- <!--- since years evenly divisible by 400 are not leap, check the span and correct the offset --->
- <cfset leap400 = (leapYearSpan \ 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 100 years --->
- <cfset daysOut -= leap100>
- <cfoutput>DaysOut (leap100): #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 EQ 0) OR (dYear % 100 NEQ 0 AND dYear % 4 EQ 0)) AND dMonth LT 3>
- <cfset daysOut-->
- <cfoutput>DaysOut (leap adjusted): #daysOut#<br></cfoutput>
- </cfif>
- <cfset daysOut += gregOffset>
- <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>
- <cfif daysOut NEQ dayOfWeek(d)>
- <h1>ERRRRRRROOOOOOOORRRRRRR</h1><cfabort>
- </cfif>
- <cfreturn daysOut>
- </cffunction>
- <!--- test data --->
- <cfset whatDayIsThis(CreateDate(2000, 1, 1))>
- <cfset whatDayIsThis(CreateDate(2000, 2, 1))>
- <cfset whatDayIsThis(CreateDate(2000, 3, 1))>
- <cfset whatDayIsThis(CreateDate(1999, 12, 1))>
- <cfset whatDayIsThis(CreateDate(1999, 1, 1))>
- <cfset whatDayIsThis(CreateDate(1999, 2, 26))>
- <cfset whatDayIsThis(CreateDate(1997, 2, 26))>
- <cfset whatDayIsThis(CreateDate(1996, 2, 26))>
- <cfset whatDayIsThis(CreateDate(1996, 6, 26))>
- <cfset whatDayIsThis(CreateDate(1980, 1, 1))>
- <cfset whatDayIsThis(CreateDate(1988, 6, 26))>
- <cfset whatDayIsThis(CreateDate(1648, 6, 26))>
- <cfset whatDayIsThis(CreateDate(1582, 10, 15))>
- <cfset whatDayIsThis(CreateDate(1582, 10, 4))>
- <cfset whatDayIsThis(CreateDate(1581, 10, 4))>
- <cfset whatDayIsThis(CreateDate(1580, 1, 1))>
- <cfset whatDayIsThis(CreateDate(1579, 12, 31))>
- <cfset whatDayIsThis(CreateDate(1439, 3, 9))>
- <cfset whatDayIsThis(CreateDate(1066, 6, 2))>
- <cfset res = testHarness(whatDayIsThis)>
- <cfdump var="#res#" label="The Result">
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement