Time formatting issue in coldfusion - coldfusion

I am using Taffy framework for creating api endpoints. In one of my endpoint I need to display time in particular format say hh:mm:ss, I used timeformat method in CFC file to get the desired result, but when I hit the endpoint I am getting January, 01 1970 17:00:00. Any suggestions?
Note:For field "clientHoursOfOperationOpenTime" Time value is returned from Mysql, I am querying the DB in CFC file and formatting the clientHoursOfOperationOpenTimevalue.
<cfset var getLocationHours = QueryNew('') >
<cfquery name="getLocationHours" datasource="#appSettings.DSN#">
Select some fields here
</cfquery>
<cfset getLocationHours.clientHoursOfOperationOpenTime= "#timeFormat(getLocationHours.clientHoursOfOperationOpenTime, 'hh:mm:ss')#">
<cfreturn getLocationHours >
When I dump this field getLocationHours.clientHoursOfOperationOpenTime I am getting the formatted value. But when I use taffy, I am getting the format issue.

Related

ColdFusion 11 DateFormat

I am moving one of our applications from ColdFusion 9.01 to ColdFusion 11 and encountered a situation where I cannot get the date formatted the way I want it using "DateFormat". I read through the docs since things have changed in CF versions, but I honestly can't figure out why this isn't working. It worked beautifully in CF 9. I know it's probably something very easy, but I am just not seeing it.
The query (Oracle DB) provides me a list of the last 30 days and the loop is simply to reformat the date output from "2014-07-01 00:00:00.0" to a more friendly looking display of 01-Jul-2014 except that I cannot get it to format as "dd-mmm-yyyy" it just spits back the original output from the query. I hard coded the date where normally there would be a cfquerparam. Any ideas?
<cfquery name="qryDateArray" datasource="#request.db#">
select trunc(to_date('07/01/2014', 'mm/dd/yyyy') + 1 - rownum) as ref_date
from dual connect by rownum <= 30
</cfquery>
<cfloop from="1" to="#qryDateArray.recordcount#" index="j">
<cfset qryDateArray.ref_date[j] = DateFormat(qryDateArray.ref_date[j], "dd-mmm-yyyy")>
</cfloop>
<cfoutput>
<cfdump var="#qryDateArray#">
</cfoutput>
I could not test this on CF11 since I do not have it handy. I did verify that your code though returns results as you explained when I ran it on my CF10 environment here. So what you can do is add a column to the query object and define it as a varchar and add your formatted data to that. This in turn dumped out the formatted dates.
<cfquery name="qryDateArray" datasource="#request.db#">
select trunc(to_date('07/01/2014', 'mm/dd/yyyy') + 1 - rownum) as ref_date
from dual connect by rownum <= 30
</cfquery>
<cfset aryData = [] />
<cfloop from="1" to="#qryDateArray.recordcount#" index="j">
<cfset ArrayAppend(aryData, DateFormat(qryDateArray.ref_date[j], "dd-mmm-yyyy")) />
</cfloop>
<cfset QueryAddColumn(qryDateArray, "STRDATE", "VarChar", aryData) />
<cfoutput>
<cfdump var="#qryDateArray#">
</cfoutput>
If dependent on the query column names then could use something like Ben's method explained here to do some renaming of the columns: http://www.bennadel.com/blog/357-ask-ben-changing-coldfusion-query-column-names.htm
It'd be great if you'd given us a portable test case rather than one that relies on your database, but I suspect it is because ColdFusion has become more rigid with its type management of query columns.
So CF considers your ref_date column to be of type date, so when you try to put the formatted string back into the query column, CF tries (and succeeds) to convert the string back into a date.
Aside:
I have to wonder why you don't format the data string in the DB from the outset, and just return it the way you need it, rather than returning something else, then looping over the thing to adjust it..?

coldfusion - parsing a string date into datetime format

We receive an XML file with a date node as follows:
<createdDate>1/11/2008 7:04:28 a.m.</createdDate>
Dates are UK format dd/mm/yyy, so 1/11/2008 is 1st November 2008.
We run a coldfusion function to parse the xml and insert into the database. The relevant database field is of datetime datatype and needs to remain that way. How would I format this string representation of the date into a format the database will accept?
Not an ideal situation, but the format you are getting data especially dots in am/pm strings make it hard to read and on top of that it comes in UK Date format. This can help:
<cfset x="21/11/2008 7:04:28 p.m.">
<cfset x=Replace(x,".","","All")>
<cfset y=LSDateFormat(x,"mm/dd/yyyy","English (UK)")>
<cfoutput>
x====#x#
<br/>y===#y#
<cfset z=CreateDateTime(Year(y),month(y),day(y),hour(x),minute(x),second(x))>
z====#z#
<cfset someDatevare=LSParseDateTime(x,"English (UK)")>
</cfoutput>
EDIT As Leigh mentioned, removing periods or any other non-standard characters from the string and then LSParseDateTime will return a date time object.

Coldfusion Query loop works in cf10 but not 9

Why does the following work in CF10 but not CF9?
<cfset out="">
<cfif isQuery( arguments.values ) >
<cfloop query="#arguments.values#" >
<cfset out = '#out#<option value="#value#">#label#</option>'>
</cfloop>
</cfif>
CF9 states that "Complex object types cannot be converted to simple values." for the line containing the cfloop. I'm using the Coldbox framework and it's debugger information shows that arguments.values is a query with Label & Value columns.
Prior to CF10, the query attribute of cfloop can only be a string - the name of the query - not the variable itself.
So, when you put #arguments.values# it is trying to convert the complex query object to a string, to obtain a name, which is where the error comes from.
It works in CF10 because the attribute has been updated to also allow a query value.
side notes:
This line of code can be simplified:
<cfset out = '#out#<option value="#value#">#label#</option>'>
to:
<cfset out &= '<option value="#value#">#label#</option>'>
Also you very likely should be using HtmlEditFormat* on at least label, and perhaps value too.
*(or encodeForHtml if it only needs to work in CF10+)

Getting CLOB data from ColdFusion 8

I am trying to retrieve CLOB data from our Oracle database. the code is the following:
<cfstoredproc datasource="#request.site.datasource#" procedure="GETPAGESWITHMETADATA" result="myResults">
<cfprocparam cfsqltype="CF_SQL_VARCHAR" type="in" value="News">
<cfprocparam cfsqltype="CF_SQL_VARCHAR" type="in" value="News Pages">
<cfprocparam cfsqltype="CF_SQL_CLOB" type="out" variable="XML">
<cfprocresult name="rs1">
</cfstoredproc>
<cfdump var="#myResults#">
<cfoutput>#XML#</cfoutput>
<cfcatch type="any">
<cfdump var="#cfcatch#">
</cfcatch>
</cftry>
Basically, the output of the stored procedure is:
select dbms_xmlquery.getxml(queryCtx) INTO XML from dual;
I checked the data sources on the server and the "Enable long text retrieval (CLOB)." option is checked for every data source.
Surprisingly, instead of getting the XML result on screen, I get a very short string:
[C#74897f5e
It looks like a handle id instead of the content itself.
How can I retrieve the complete content of the XML?
For reference, the data source is using macromedia drivers with TNS name:
Driver class: macromedia.jdbc.MacromediaDriver
As #MarkAKruger suggested, returning a table from the procedure solved the issue.
The following PL/SQL code did the trick:
create or replace
PACKAGE PCK_Commonspot
AS
type t_clob IS record (metadata CLOB) ;
type t_clob_tab IS TABLE OF t_clob;
FUNCTION GetPagesWithMetadataAsRS(FormName varchar2, CategoryName varchar2)
RETURN t_clob_tab pipelined;
END PCK_Commonspot;
The package body contains the following code:
FUNCTION GetPagesWithMetadataAsRS(FormName varchar2, CategoryName varchar2)
RETURN t_clob_tab pipelined
IS
r t_clob;
BEGIN
GETPAGESWITHMETADATA(FormName, CategoryName, r.metadata) ;
pipe row(r) ;
RETURN;
END;
The function GETPAGESWITHMETADATA is the one returning a CLOB into r.metadata
Here, the trick is around returning a piped table.
It becomes super nice on the ColdFusion side because the call is really simple:
<cfquery name="Test" datasource="myDS" maxrows="1">
SELECT * FROM TABLE(PCK_Commonspot.GetPagesWithMetadataAsRS('abc','def'))
</cfquery>
<cfset XML = Xmlparse(Test.Metadata)>
Thanks Mark!
Check your datasource settings in the CF Administrator. Under the advanced settings, there are check boxes to allow BLOB and CLOB data to be returned. If they are unchecked you potentially could get truncated data returned in your queries
Even though Jaepetto has already answered his question, I thought I'd contribute another answer for posterity.
I was having similar problems returning CLOB data into CF8 from Oracle 11g. The original solution (that wasn't working) was roughly:
<cfquery name="GetDoc" DATASOURCE=myDS>
SELECT CLOBDATA FROM FILES WHERE FILES.FILEID = #FileID#
</cfquery>
This query would complete successfully, but GetDoc.CLOBDATA would always be an empty string. It turned out that retrieving CLOB data using cfquery would always do this, but if I wrapped it up in a stored proc then it worked. I assume this is some quirk of the JDBC drivers we're using.
Anyway, the CF guts of my solution was as follows. Note the use of CF_SQL_LONGVARCHAR instead of CF_SQL_CLOB - using CF_SQL_CLOB gave me the weird handle ID value that Jaepetto was seeing.
<cfstoredproc PROCEDURE="GETCLOB" DATASOURCE=myDS >
<CFPROCPARAM TYPE="IN" CFSQLTYPE="CF_SQL_INTEGER" DBVARNAME="pFileID" value="#fileID#"/>
<CFPROCPARAM TYPE="OUT" CFSQLTYPE="CF_SQL_LONGVARCHAR" DBVARNAME="pClob" VARIABLE="vClob" />
</cfstoredproc>
<!--- Dump the clob to the local filesystem --->
<cfscript>
fstream = CreateObject("java", "java.io.FileOutputStream").init(filepath, JavaCast("boolean","true"));
outStream = CreateObject("java", "java.io.BufferedOutputStream").init(fstream);
outStream.write(#toBinary(vClob)#);
outStream.flush();
outStream.close();
</cfscript>

Session Variables, welcome messages

Why does this not work? My welcome message, it just doesn't show up:
<p>Welcome <cfoutput>#Recordset1.UserID#</cfoutput>.</p>
The session variable on the login page I created is:
<cflock timeout=999 scope="Session" type="Exclusive">
<cfset Session.IDUsers =''>
</cflock>
is this incorrect? On the index page where I'm trying to display my welcome message I have:
<cfquery name="Recordset1" datasource="cfGossip">
SELECT *
FROM users
WHERE users.IDUsers = <cfqueryparam value="#Session.IDUsers#">
</cfquery>
I'm not sure if this works, or is necessary?
If you set the userid stored in the session to be the empty string, when you query on it, you will only get users for whom the id is the empty string, which shouldn't be any of them. Therefore, the query is returning an empty set, and your page is (correctly) not displaying a user id.
How are you initially identifying the user? Are you querying against a database when they log in? Are you storing a cookie? Reading Tarot cards? For this to work, at some point, you have to store the correct userid, probably in the session. To do that, you need to first identify who the user is.
Also, if you are using CF6+, you probably do not need the cflock. It is now used to prevent race conditions, as CF is now thread-safe.
Looks like you're just starting with CF, welcome to the community.
My understanding of your code makes the structure look like the following, if I'm understanding you correctly:
<cfset session.idUsers = '' />
<cfquery datasource = "cfgossip" name = "recordset1">
SELECT * FROM USERS WHERE USERS.ID_USERS = <cfqueryparam cfsqltype = "cf_sql_integer" value = "#session.idUsers# />
</cfquery>
<cfoutput>Welcome #recordset1.userID#</cfoutput>
The reason this doesn't work is because your session.idUsers value is blank. Assuming you have a user in your database with an ID_USERS value of 1, you could change the CFSET to look like and it should return results.
Additionally, while it's great to see you using CFQUERYPARAM, I'd recommend including a CFSQLTYPE attribute in the tag whenever possible to provide an added line of defense against injection attacks. You can check out http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags_p-q_18.html to see the list of available types.
Is there anywhere in your code where you set your session.IDUsers? You initialize it as a blank ''. Coldfusion does not populate it for you. The session scope is a place that will remember things for that user that you put there for a specified period of time inactivity, usually 20 minutes. So hopefully, somewhere before you run your query you have additional logic that fills that in, otherwise you are asking the database for a user named, ''.
This is just a point of style, but the following may work better for you:
<cfset Session.IDUsers =''>
<!--- Do something here to populate Session.IDUsers --->
<!--- Creates a blank query - not necessary, but can reduce errors later --->
<cfset Recordset1 = queryNew("UserID")>
<!--- Populate the query from the database --->
<cfquery name="Recordset1" datasource="cfGossip">
SELECT *
FROM users
WHERE users.IDUsers = <cfqueryparam value="#Session.IDUsers#">
</cfquery>
<!--- If the query has data, use it, otherwise show generic message --->
<cfoutput>
<cfif Recordset1.recordcount>
<p>Welcome #Recordset1.UserID#.</p>
<cfelse>
<p>Welcome new user!</p>
</cfif>
</cfoutput>
<!--- OR since we used queryNew("userID"), we can simplify without throwing an error. ---->
<cfoutput>
<p>Welcome <cfif len(Recordset1.userID)>#Recordset1.userID#.<cfelse>new user!</cfif></p>
</cfoutput>
Putting the cfoutput outside the paragraph block will make it easier if you have additional variables to insert into the text. (but will work either way)
Regardless of all that, unless you forgot to share a bit more of the code, I think the issue is that the session.IDUsers is blank and needs to be populated before the query. I hope this helps!