Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

msxml2 setAttribute date format bug

Status
Not open for further replies.

DrHeadgear

Programmer
Oct 12, 2001
14
0
0
DK
VB/VBScript

Adding a date as an attribute value, without explicitly converting it to a string first (i.e. using implicit conversion) results in the date being formatted according to US settings. Adding the value to the attribute using attribute.text=[variable] results in the date being formatted according to system locale settings.

The following asp sample code (VBScript) shows the varying results between implicit and explicit date conversion, using three lcids. My system locale setting is Danish.

<%@ Language=VBScript %>
<HTML>
<HEAD>
<META NAME=&quot;GENERATOR&quot; Content=&quot;Microsoft Visual Studio 6.0&quot;>
</HEAD>
<BODY>
<h2>Date conversion bug in msxml2 setAttribute method</h2>
<p>Conversion of a date to string when writing to a DOMDocument.4.0 object uses system settings for conversion. The SetAttribute method, however, doesn't read the system settings, it uses the default (US) date/time format.
<br/>The results below show how setting the lcid in an asp page does not affect implicit conversion of a date (presumably by design). Explicit conversion using the cStr(expression) method should be used when writing date formats other than those specified by sytem locale settings.
<br/>The bug is clearly visible in the first DateNode of each DateSet, where the format is always default (US).
<br/><b>Summary:</b>
<br/>Implicit date conversion will result in a system defined date format
<br/>Explicit date conversion will result in a specified lcid format.
<br/>Implicit conversion using setAttribute will result in default (US) format.
<br/><b>Note:</b>
<br/>This bug was discovered using a Windows 2000 5.00.2195 PC with Danish locale settings.
<br />To test this code ensure your system does not use US locale settings.
<h2>Table of settings/results as presented by vbScript</h2>
<table border=1>
<tr><td>LCID</td><td>Location Setting</td><td>Implicit conversion</td><td>Explicit Conversion</td></tr>
<%
dim oDom, curTime
set odom=server.CreateObject(&quot;msxml2.domdocument.4.0&quot;)
oDom.loadXML&quot;<root/>&quot;
processSetting 1033,&quot;US&quot;
processSetting 1030,&quot;Denmark&quot;
processSetting 2057,&quot;UK&quot;

%>
</table>
<h2>This is the xml produced:</h2>
<%call presentDom()%>
</BODY>
</HTML>

<%
sub processSetting(this_lcid,strLocation)
session.LCID=this_lcid
curtime=now()
oDom.documentElement.appendchild createDateNodeSet(strLocation)
%><tr><td><%=session.LCID%></td><td><%=strLocation%></td><td><%=curtime%></td><td><%=cStr(curtime)%></td></tr>
<%
end sub

function createDateNodeSet(strLocationSetting)
dim oChild
set oChild=oDom.createElement(&quot;DateSet&quot;)
ochild.setAttribute &quot;lcid&quot;,session.lcid
ochild.setAttribute &quot;LocationSetting&quot;,strLocationSetting
oChild.AppendChild createDateNode (&quot;setAttribute&quot;,&quot;implicit&quot;)
oChild.AppendChild createDateNode (&quot;setAttribute&quot;,&quot;explicit&quot;)
oChild.AppendChild createDateNode (&quot;text&quot;,&quot;implicit&quot;)
oChild.AppendChild createDateNode (&quot;text&quot;,&quot;explicit&quot;)
set createDateNodeSet=oChild
end function

function createDateNode(strMethod,strConversionType)
dim oDateNode, tmpDate
set oDateNode=oDom.createElement(&quot;DateNode&quot;)
oDateNode.setattribute &quot;method&quot;,strMethod
oDateNode.setattribute &quot;conversion_type&quot;,strConversionType
oDateNode.setattribute &quot;mydate&quot;,&quot;empty&quot;

if strConversionType=&quot;implicit&quot; then
tmpDate=curtime
else
tmpDate=cStr(curtime)
end if

if strMethod=&quot;setAttribute&quot; then
oDateNode.setAttribute &quot;mydate&quot;,tmpDate
elseif strMethod=&quot;text&quot; then
oDateNode.setattribute &quot;mydate&quot;,&quot;empty&quot;
oDateNode.attributes.getNamedItem(&quot;mydate&quot;).text=tmpDate
end if
set createDateNode=oDateNode
end function

sub presentDom()
dim oNode, cNode
for each onode in oDom.documentElement.childnodes
Response.Write &quot;<xmp>&quot; & onode.clonenode(false).xml & &quot;</xmp>&quot;
for each cNode in onode.childnodes
Response.Write &quot;<br/>&quot; & replace(replace(cnode.xml,&quot;<&quot;,&quot;&lt;&quot;),&quot;>&quot;,&quot;&gt;&quot;)
next
next

end sub
%>


&quot;Lets integrate!&quot;
If it doesn't work it's not my fault.
 
In VB6, I always run my dates through the format command:
Code:
oMyAttr.Text = Formst$(MyDate, &quot;yyyy-mm-ddTHh:Nn:Ss&quot;)
to get them into ISO-8601 format.

In .NET, I'll use the XmlConvert class to do it.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
SORRY, vital info missing!

The point was that using setAttribute gives a different result to using attribute.text=[variable]. setAttribute isn't reading the system locale settings to convert the date to a string, it's using default US settings.

Chip H is right though, format explicitly, it's good practice. I discovered the bug whilst writing an app that made some date comparisons. I was trying to be locale neutral, so didn't do any conversions, just left vbScript doing implicit conversions. As everything *should* have been using systems settings it should have gone fine, XML written in format native to system, calculations made in format native to system. No formatting should be necessary.

I still can write this without explicit formatting, as long as I explicitly convert from date to string.

&quot;Lets integrate!&quot;
If it doesn't work it's not my fault.
 
&quot;As everything *should* have been using systems settings it should have gone fine&quot;

That's not entirely true. Since you add a date to a node and Microsoft always tries to 'help' you, it is assumed that you try to add a node of the type 'Date'. By design of the W3C a Date node in XML always must be in US date format, so your date is converted to US date format.

Of course it is possible to add the date using the text property, but this way XSD validation expecting a date will fail.

Jordi Reineman
 
JJR - the inbuilt data type date in xsd should be specified in gregorian format (year month day), according to W3 (see
My attribute node has no xsd schema definition, there is no associated schema for the document.

There is currently no date specification for xml 1.0 itself (see - i.e. no mention of date in the xml (default) namespace.

So firstly, if MSXML were just trying to be helpful it should add the date in Gregorian format (it doesn't) and secondly I find it a little twisted that it makes any conversion at all. I have a system locale setting for a reason.

So I call it a bug, I'm certain it's not there by design.

&quot;Lets integrate!&quot;
If it doesn't work it's not my fault.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top