DrHeadgear
Programmer
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="GENERATOR" Content="Microsoft Visual Studio 6.0">
</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("msxml2.domdocument.4.0"
oDom.loadXML"<root/>"
processSetting 1033,"US"
processSetting 1030,"Denmark"
processSetting 2057,"UK"
%>
</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("DateSet"
ochild.setAttribute "lcid",session.lcid
ochild.setAttribute "LocationSetting",strLocationSetting
oChild.AppendChild createDateNode ("setAttribute","implicit"
oChild.AppendChild createDateNode ("setAttribute","explicit"
oChild.AppendChild createDateNode ("text","implicit"
oChild.AppendChild createDateNode ("text","explicit"
set createDateNodeSet=oChild
end function
function createDateNode(strMethod,strConversionType)
dim oDateNode, tmpDate
set oDateNode=oDom.createElement("DateNode"
oDateNode.setattribute "method",strMethod
oDateNode.setattribute "conversion_type",strConversionType
oDateNode.setattribute "mydate","empty"
if strConversionType="implicit" then
tmpDate=curtime
else
tmpDate=cStr(curtime)
end if
if strMethod="setAttribute" then
oDateNode.setAttribute "mydate",tmpDate
elseif strMethod="text" then
oDateNode.setattribute "mydate","empty"
oDateNode.attributes.getNamedItem("mydate"
.text=tmpDate
end if
set createDateNode=oDateNode
end function
sub presentDom()
dim oNode, cNode
for each onode in oDom.documentElement.childnodes
Response.Write "<xmp>" & onode.clonenode(false).xml & "</xmp>"
for each cNode in onode.childnodes
Response.Write "<br/>" & replace(replace(cnode.xml,"<","<"
,">",">"
next
next
end sub
%>
"Lets integrate!"
If it doesn't work it's not my fault.
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="GENERATOR" Content="Microsoft Visual Studio 6.0">
</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("msxml2.domdocument.4.0"
oDom.loadXML"<root/>"
processSetting 1033,"US"
processSetting 1030,"Denmark"
processSetting 2057,"UK"
%>
</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("DateSet"
ochild.setAttribute "lcid",session.lcid
ochild.setAttribute "LocationSetting",strLocationSetting
oChild.AppendChild createDateNode ("setAttribute","implicit"
oChild.AppendChild createDateNode ("setAttribute","explicit"
oChild.AppendChild createDateNode ("text","implicit"
oChild.AppendChild createDateNode ("text","explicit"
set createDateNodeSet=oChild
end function
function createDateNode(strMethod,strConversionType)
dim oDateNode, tmpDate
set oDateNode=oDom.createElement("DateNode"
oDateNode.setattribute "method",strMethod
oDateNode.setattribute "conversion_type",strConversionType
oDateNode.setattribute "mydate","empty"
if strConversionType="implicit" then
tmpDate=curtime
else
tmpDate=cStr(curtime)
end if
if strMethod="setAttribute" then
oDateNode.setAttribute "mydate",tmpDate
elseif strMethod="text" then
oDateNode.setattribute "mydate","empty"
oDateNode.attributes.getNamedItem("mydate"
end if
set createDateNode=oDateNode
end function
sub presentDom()
dim oNode, cNode
for each onode in oDom.documentElement.childnodes
Response.Write "<xmp>" & onode.clonenode(false).xml & "</xmp>"
for each cNode in onode.childnodes
Response.Write "<br/>" & replace(replace(cnode.xml,"<","<"
next
next
end sub
%>
"Lets integrate!"
If it doesn't work it's not my fault.