While working on my CFUG preso for this month, I found another bug: dates weren't handled properly coming back from web service calls. So I added support for them. Patch file for js/io/RemoteObject.js
below, or you can get the update and supporting test scripts from Subversion.
===================================================================
— RemoteObject.js (revision 10)
+++ RemoteObject.js (revision 11)
@@ -26,6 +26,7 @@
var DATATYPE_BOOLEAN = "soapenc:boolean";
var DATATYPE_NUMBER = "soapenc:double";
var DATATYPE_NUMBER2 = "xsd:double";
+var DATATYPE_DATE_TIME = "xsd:dateTime";
/**
* Variable: REMOTE_OBJECT_VERSION
@@ -85,6 +86,10 @@
{
value = parseFloat(dItem.item(z).firstChild.nodeValue);
}
+ else if (xsiType == DATATYPE_DATE_TIME)
+ {
+ value = DefaultHandler.xmlDate2JSDate(dItem.item(z).firstChild.nodeValue);
+ }
else if(xsiType == DATATYPE_MAP)
{
value = new Map();
@@ -95,7 +100,7 @@
}
}
}
- nstruct.put(key,value);
+ nstruct.put(key,value);
}
}
}
@@ -716,6 +721,10 @@
{
eval(__dfh__variable + " = parseFloat(resvalnodes.item(0).firstChild.nodeValue)");
}
+ else if (returntype == DATATYPE_DATE_TIME)
+ {
+ eval(__dfh__variable + " = DefaultHandler.xmlDate2JSDate(resvalnodes.item(0).firstChild.nodeValue)");
+ }
//}
//this is a structure (a coldfusion struct)
//else if(complextypeid != null && complextypeid.length > 1)
@@ -765,6 +774,8 @@
//show the value to stdout
if(__dfh__variable == "__neuro__myvar__")
{
+ // TODO: this throws not defined errors. Probably should be if(typeof neuro_SystemOut == "function") instead
+ // Hopefully, however, no one will ever get here, because they'll always be using a callback.
if(neuro_SystemOut != null)
{
neuro_SystemOut("\n");
@@ -774,4 +785,29 @@
neuro_Runner("");
}
}
+};
+
+DefaultHandler.xmlDate2JSDate = function __xmlDate2JSDate(xmlDate) {
+ var val = xmlDate.split("T");
+ // split it into date and time portions
+ var date = val[0];
+ var time = val[1];
+ date = date.split("-");
+ time = time.split(":");
+ // rip out the date portions
+ var year = date[0];
+ var month = date[1] – 1; // JS uses 0-11, not 1-12
+ var day = date[2];
+ // rip out the time portions
+ var hours = time[0];
+ var minutes = time[1];
+ var seconds = parseFloat(time[2]);
+ // convert fractional seconds to milliseconds
+ var millis = Math.round((seconds – Math.floor(seconds) ) * 1000);
+ seconds = Math.floor(seconds);
+ // assemble the completed date
+ var completeDate = new Date(year, month, day, hours, minutes, seconds, millis);
+ // adjust the time from UTC (Zulu) to local time
+ // TODO: change the to check for the appropriate adjustment, rather than blindly assuming UTC
+ return new Date(completeDate.getTime() – (completeDate.getTimezoneOffset() * 60 * 1000));
};
Comments are closed.