0

I just set up a short toy learning ColdFusion page on a server. The page makes a call out to a cffunction, which gets the current date, determines the year, and then returns a boolean reflecting whether or not the current year is a leap year. This information is displayed on the main page in plain text.

page.cfm

<html>
<head>
        <cfinclude template="./func.cfm" />
</head>
<body>
        <cfset yearList = "2000;2001,2002/2003,2004,2005;2006/2007,2008,2009;2010,2011,2012" >

        <cfloop index="year" list=#yearList# delimiters=",;/" >

                <cfset isLeapYear = #My_IsLeapYear(year)# >

                <cfif isLeapYear is True>
                        <cfoutput>
                                #year# is a leap year!
                        </cfoutput>

                        <cfelse>
                                <cfoutput>
                                #year# is not a leap year.
                                </cfoutput>
                </cfif>
                <br>
        </cfloop>

</body>
</html>

func.cfm

<cffunction name="My_IsLeapYear" output="false" access="public" returnType="boolean">
        <cfargument name="year" type="numeric" required="true" default="" />
        <cfset var isLeapYear = (DaysInYear(CreateDate(arguments.year,1,1)) EQ 366) />

        <cfreturn isLeapYear>
</cffunction>

Trying to access this page caused a horrific memory leak and took down the server where it was hosted. I'm at a loss. Any thoughts?

lyonssp
  • 117
  • 1
  • 1
  • 7

2 Answers2

0

I think this is a faster way to check this:

<cfset yearList = "2000;2001,2002/2003,2004,2005;2006/2007,2008,2009;2010,2011,2012" >

<cfoutput>
<cfloop index="year" list="#yearList#" delimiters=",;/" >
        <p>#year# is <cfif !(val(year) MOD 4 EQ 0)>not </cfif>a leap year!</p>
</cfloop>
</cfoutput>

Avoids the more expensive calls to DaysInYear(). You just need to check if the value of year is a number divisible by 4.

Update

Point taken re: calculation. As for the original function, You can just return the result of the comparison. No need to create a function local variable.

<cffunction name="My_IsLeapYear" output="false" access="public" returnType="boolean">
        <cfargument name="year" type="numeric" required="true" default="" />
        <cfreturn (DaysInYear(CreateDate(arguments.year,1,1)) EQ 366) />
</cffunction>

and in page.cfm, you can change this code:

<cfset isLeapYear = #My_IsLeapYear(year)# >
<cfif isLeapYear is True>

to a simpler call:

<cfif My_IsLeapYear(year)>

since that function will only return a Boolean value.

Adrian J. Moreno
  • 14,350
  • 1
  • 37
  • 44
  • Actually, that rule is incorrect. There is a weird characteristic of the Gregorian calendar that makes some years divisible by 4 not leap years. Regardless, this isn't really about speed. – lyonssp Feb 04 '15 at 21:29
  • OP is correct , when the `Year / 100`, it must also be evenly divisible by 4. wwu.edu/skywise/leapyear.html, so 1700, 1800 and 1900 are not leap years but 1600 and 2000 are. – Regular Jo Feb 05 '15 at 05:35
0

The memory leak was caused to a strange external issue. Thanks for the comments and such.

lyonssp
  • 117
  • 1
  • 1
  • 7
  • Glad you figured thing out, but it sounds like a localized issue. If the actual solution wouldn't be of use to others, you should delete the thread. – Leigh Feb 06 '15 at 02:18
  • @Leigh I reported it to have it deleted by a mod. How do I delete it myself? – lyonssp Feb 06 '15 at 21:02
  • Honestly, I am not sure. I thought there was a "delete" option visible to the question owner. If not, wait for the mods I guess. – Leigh Feb 10 '15 at 15:53