Skip to main content
Known Participant
January 14, 2011
Question

Functions returning TRUE or FALSE

  • January 14, 2011
  • 3 replies
  • 1813 views

Morning chaps,

Wrote a function that converts a date in the format "01 Jan 2010" to a Unix timestamp. It also returns FALSE if it cannot convert the date it is being passed, which is great because I can use:

<cfif convertDateToUTS(FORM.date)>

     <cfset #date# = #convertDateToUTS(FORM.date)# />

     <p>Your date: <cfoutput>#date#</cfoutput></p>

<cfelse>

     <p>Could not convert date.</p>

</cfif>

Here is the function:

<cffunction name="convertDateToUTS" output="no">     <cfargument name="date" required="Yes" />     <cfargument name="time" default="00:00"/> <cftry>     <cfset #date# = trim(left(trim(date), 11)) />     <cfset #time# = trim(left(trim(time), 5)) />     <cfset #dateArr# = #listToArray(date, " ")# />     <cfset #dateArr[2]# = #replace(dateArr[2], "Jan", 1)# />     <cfset #dateArr[2]# = #replace(dateArr[2], "Feb", 2)# />     ...etc...     <cfset #dateArr[2]# = #replace(dateArr[2], "Dec", 12)# />     <cfset #dateTime# = ArrayToList(dateArr, " ")&" "&time />     <cfset #dateTimeBits# = listToArray(dateTime, " :") />     <cfset #ODBCdateTime# = CreateODBCDateTime(CreateDateTime(dateTimeBits[3], dateTimeBits[2], dateTimeBits[1], dateTimeBits[4], dateTimeBits[5], 0)) />     <cfset #unixDateTime# = #DateDiff("s", CreateDate(1970,1,1), ODBCdateTime)# />     <cfreturn #unixDateTime# /> <cfcatch>     <cfreturn FALSE /> </cfcatch> </cftry> </cffunction>

However, now I want to write a similar one which converts a timestamp to a date in that format:

<cffunction name="convertUTStoDate" output="no">      <cfargument name="uts" required="Yes" /> <cftry>      <cfset #date# = #DateFormat(DateAdd("s",uts,"1970/01/01 00:00:00"),"DD MMM YYYY")# />      <cfset #time# = #TimeFormat(DateAdd("s",uts,"1970/01/01 00:00:00"), "HH:mm")# />      <cfreturn #date#&" "&#time# /> <cfcatch>      <cfreturn FALSE /> </cfcatch> </cftry> </cffunction>

This does the conversion correctly but it it won't let me use it as the condition in a CFIF like the first function does eg <cfif convertUTStoDate(uts)> but, it does work exactly as I want if I do this: <cfif convertUTStoDate(uts) NEQ FALSE>

What am I doing wrong? I really want it to behave like the first function when used in a condition. Massive thanks to anyone who can help!

T

    This topic has been closed for replies.

    3 replies

    BKBK
    Community Expert
    Community Expert
    January 16, 2011

    What dou you think of the following version of your code? It is the same as your original code but, in my opinion, a bit simpler.  For example, your test now becomes something like

    <!--- remember returntype is now a struct --->
    <cfif convertDateToUTS(FORM.date).isConverted>
         <cfset date = convertDateToUTS(FORM.date).unixDateTime/>
         <p>Your date: <cfoutput>#date#</cfoutput></p>
    <cfelse>
         <p>Could not convert date.</p>
          Reason: <cfoutput>#convertDateToUTS(FORM.date).message#</cfoutput>
    </cfif>

    <cffunction name="convertDateToUTS" output="no" returntype="struct">
        <cfargument name="date" required="Yes" />
        <cfargument name="time" default="00:00"/>
        <cfset var returnStruct = structNew()>
        <cfset returnStruct.datetime = "">
        <cfset returnStruct.isConverted= false>
    <cftry>
        <cfset date = trim(left(trim(date), 11)) />
        <cfset time = trim(left(trim(time), 5)) />
        <cfset dateArr = listToArray(date, " ") />
        <cfset dateArr[2] = replace(dateArr[2], "Jan", 1) />
        <cfset dateArr[2] = replace(dateArr[2], "Feb", 2) />
        ...etc...
        <cfset dateArr[2] = replace(dateArr[2], "Dec", 12) />
        <cfset dateTime = ArrayToList(dateArr, " ")&" "&time />
        <cfset dateTimeBits = listToArray(dateTime, " :") />
        <cfset ODBCdateTime = CreateODBCDateTime(CreateDateTime(dateTimeBits[3], dateTimeBits[2], dateTimeBits[1], dateTimeBits[4], dateTimeBits[5], 0)) />
        <cfset returnStruct.unixDateTime = DateDiff("s", CreateDate(1970,1,1), ODBCdateTime) />
        <cfset returnStruct.message = "Conversion succeeded.">
        <cfset returnStruct.isConverted= true>
        <cfreturn  returnStruct>
    <cfcatch>
        <cfset returnStruct.message = cfcatch.message>
        <cfreturn  returnStruct>
    </cfcatch>
    </cftry>
    </cffunction>


    <cffunction name="convertUTStoDate" output="no" returntype="struct">
         <cfargument name="uts" required="Yes" />
         <cfset var returnStruct = structNew()>
         <cfset returnStruct.datetime = "">
         <cfset returnStruct.isConverted= false>
    <cftry>
         <cfset date = DateFormat(DateAdd("s",uts,"1970/01/01 00:00:00"),"DD MMM YYYY") />
         <cfset time = TimeFormat(DateAdd("s",uts,"1970/01/01 00:00:00"), "HH:mm") />
         <cfset returnStruct.datetime = date&" "&time />
         <cfset returnStruct.message = "Conversion succeeded.">
         <cfset returnStruct.isConverted= true>
         <cfreturn  returnStruct>
    <cfcatch>
          <cfset returnStruct.message = cfcatch.message>
          <cfreturn  returnStruct>
    </cfcatch>

    </cftry>
    </cffunction>

    BKBK
    Community Expert
    Community Expert
    January 16, 2011

    This does the conversion correctly but it it won't let me use it as the condition in a CFIF like the first function does eg <cfif convertUTStoDate(uts)> but, it does work exactly as I want if I do this: <cfif convertUTStoDate(uts) NEQ FALSE>

    What am I doing wrong?

    The expression <cfif convertUTStoDate(uts)> will only work if ColdFusion can cast convertUTStoDate(uts) to a boolean. That is in turn possible only if that value is a boolean or a number. It fails bacause convertUTStoDate(uts) is a date.

    However, the expression <cfif convertUTStoDate(uts) NEQ FALSE> is a comparison. As such, it will always work, so long as convertUTStoDate(uts) is a simple value. It will then be comparing like with like.

    Inspiring
    January 14, 2011

    I'm surprised the first function worked.  Using strings as booleans just seems wrong.  I would suggest using an empty string as your return value if the conversion fails and using your if/else logic on the length of the returned value.

    Inspiring
    January 14, 2011

    If the input's not valid, it should just raise an appropriate exception.  Having a function that might return a date-formatted string, or might return a boolean is just wrong.

    --

    Adam