Advertisement
Guest User

Camden's Friday Puzzler - 2011/10/14

a guest
Oct 14th, 2011
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <cffunction name="testHarness">
  2.     <cfargument name="myfunc" required="true">
  3.     <cfset var dates = []>
  4.     <cfset var x = "">
  5.     <cfset var result = {}>
  6.    
  7.     <!--- first make the dates --->
  8.     <cfloop index="x" from="1" to="100">
  9.         <cfset arrayAppend(dates, dateAdd("d", randRange(-1000,1000), now()))>
  10.     </cfloop>  
  11.    
  12.     <cfloop index="x" from="1" to="#arrayLen(dates)#">
  13.         <cfif dayOfWeek(dates[x]) neq myfunc(dates[x])>
  14.             <cfset result.status = "fail">
  15.             <cfset result.message = "Your function said the DOW for #dateformat(dates[x])# was #myfunc(dates[x])# and it should be #dayofweek(dates[x])#">
  16.             <cfreturn result>
  17.         </cfif>
  18.     </cfloop>
  19.    
  20.     <cfset result.status = "pass">
  21.     <cfreturn result>
  22. </cffunction>
  23.  
  24. <cffunction name="whatDayIsThis" output="false">
  25.     <cfargument name="d" type="date" required="true">
  26.    
  27.     <cfset var baseYear  = 2000>
  28.     <cfset var baseMonth = 1>
  29.     <cfset var baseDay   = 1>
  30.     <cfset var baseDayOfWeek = 7>
  31.    
  32.     <cfset var dYear = Year(d)>
  33.     <cfset var dMonth = Month(d)>
  34.     <cfset var dDay = Day(d)>
  35.    
  36.     <cfset var nonLeapMonths = [31,28,31,30,31,30,31,31,30,31,30,31]>
  37.     <cfset var dayOffset = 0>
  38.     <cfset var daysOut = 0>
  39.     <cfset var jumpedLeapDay = false>
  40.    
  41.     <cfoutput><h3>#dateFormat(d, "YYYY-MM-DD")#</h3></cfoutput>
  42.    
  43.     <!--- 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...] --->
  44.     <cfset yearSpan = dYear - baseYear>
  45.    
  46.     <!--- if we're ahead of base --->
  47.     <cfif yearSpan GTE 0>
  48.         AHEAD<br>
  49.         <cfoutput>YearSpan: #yearSpan#<br></cfoutput>
  50.    
  51.         <!--- div 4 to get 4 year segments --->
  52.         <cfset leap4 = ((yearSpan - 1) \ 4)>
  53.         <cfif yearSpan GT 0><cfset leap4++></cfif>
  54.         <cfoutput>Leap4: #leap4#<br></cfoutput>
  55.    
  56.         <!--- since years evenly divisible by 400 are not leap, check the span and correct the offset --->
  57.         <cfset leap400 = (yearSpan \ 400)>
  58.         <cfoutput>Leap400: #leap400#<br></cfoutput>
  59.    
  60.         <cfset daysOut = yearSpan>
  61.         <cfoutput>DaysOut (with yearspan): #daysOut#<br></cfoutput>
  62.    
  63.         <cfset daysOut += leap4>
  64.         <cfoutput>DaysOut (leaps): #daysOut#<br></cfoutput>
  65.  
  66.         <!--- calculate the days from the start of the year --->
  67.         <cfloop from="1" to="#dMonth-1#" index="mon"><cfset dayOffset += nonLeapMonths[mon]></cfloop>
  68.         <cfset dayOffset = dayOffset + dDay - baseDay>
  69.         <cfoutput>DayOffset: #dayOffset#<br></cfoutput>
  70.        
  71.         <cfset daysOut += dayOffset>
  72.         <cfoutput>DaysOut (with day offset): #daysOut#<br></cfoutput>
  73.        
  74.         <!--- if the d year is leap and we're past march-01 and we're ahead, add leap offset --->
  75.         <cfif dYear % 400 NEQ 0 AND dYear % 4 EQ 0 AND dMonth GTE 3>
  76.             <cfset daysOut++>
  77.             <cfoutput>DaysOut (leap adjusted): #daysOut#<br></cfoutput>
  78.         </cfif>
  79.        
  80.         <cfset daysOut = baseDayOfWeek + daysOut>
  81.     <cfelse>
  82.         BEHIND<br>
  83.         <cfset yearSpan++>
  84.         <cfoutput>YearSpan: #yearSpan#<br></cfoutput>
  85.        
  86.         <!--- div 4 to get 4 year segments --->
  87.         <cfset leap4 = (yearSpan \ 4)>
  88.         <cfoutput>Leap4: #leap4# [#yearSpan#]<br></cfoutput>
  89.    
  90.         <!--- since years evenly divisible by 400 are not leap, check the span and correct the offset --->
  91.         <cfset leap400 = (yearSpan \ 400)>
  92.         <cfoutput>Leap400: #leap400#<br></cfoutput>
  93.    
  94.         <cfset daysOut = yearSpan>
  95.         <cfoutput>DaysOut (with yearspan): #daysOut#<br></cfoutput>
  96.    
  97.         <!--- add on the leap years --->
  98.         <cfset daysOut += leap4>
  99.         <cfoutput>DaysOut (leap4): #daysOut#<br></cfoutput>
  100.  
  101.         <!--- take off div 400 years --->
  102.         <cfset daysOut -= leap400>
  103.         <cfoutput>DaysOut (leap400): #daysOut#<br></cfoutput>
  104.        
  105.         <!--- calculate the days to the end of the year --->
  106.         <cfloop from="#dMonth#" to="12" index="mon"><cfset dayOffset += nonLeapMonths[mon]></cfloop>
  107.         <cfset dayOffset = dayOffset - dDay + baseDay>
  108.         <cfoutput>DayOffset: #dayOffset#<br></cfoutput>
  109.        
  110.         <cfset daysOut = daysOut-dayOffset>
  111.         <cfoutput>DaysOut (with day offset): #daysOut#<br></cfoutput>
  112.        
  113.         <!--- if the d year is leap and we're past march-01 and we're ahead, add leap offset --->
  114.         <cfif dYear % 400 NEQ 0 AND dYear % 4 EQ 0 AND dMonth LT 3>
  115.             <cfset daysOut-->
  116.             <cfoutput>DaysOut (leap adjusted): #daysOut#<br></cfoutput>
  117.         </cfif>
  118.        
  119.         <cfset daysOut = baseDayOfWeek + daysOut>
  120.     </cfif>
  121.    
  122.     <!--- mod the offset to within a week --->
  123.     <cfset daysOut = daysOut % 7>
  124.     <cfoutput>DaysOut (mod 7): #daysOut#<br></cfoutput>
  125.    
  126.     <!--- if the offset is negative, add 7 to get positive --->
  127.     <cfif daysOut LTE 0><cfset daysOut = 7 + daysOut></cfif>
  128.     <cfoutput>DaysOut: #daysOut# [#dayOfWeek(d)#]<br></cfoutput>
  129.    
  130.     <cfreturn daysOut>
  131. </cffunction>
  132.  
  133. <cfset res = testHarness(whatDayIsThis)>
  134. <cfdump var="#res#" label="The Result">
  135. <cfabort>
  136.  
  137. <!--- test dummy test data
  138. <cfset whatDayIsThis(CreateDate(2000, 1, 1))><br>
  139. <cfset whatDayIsThis(CreateDate(2000, 2, 1))><br>
  140. <cfset whatDayIsThis(CreateDate(2000, 3, 1))><br>
  141. <cfset whatDayIsThis(CreateDate(1999, 12, 1))><br>
  142. <cfset whatDayIsThis(CreateDate(1999, 1, 1))><br>
  143. <cfset whatDayIsThis(CreateDate(1999, 2, 26))><br>
  144. <cfset whatDayIsThis(CreateDate(1997, 2, 26))><br>
  145. <cfset whatDayIsThis(CreateDate(1996, 2, 26))><br>
  146. <cfset whatDayIsThis(CreateDate(1996, 6, 26))><br>
  147. <cfset whatDayIsThis(CreateDate(1980, 1, 1))><br>
  148. <cfset whatDayIsThis(CreateDate(1988, 6, 26))><br>
  149. --->
  150.  
  151.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement