Delphi 6 SOAP 源码中的BUG修正
最近我正在用delphi 6 做一个关于SOAP的项目,调试程序的时候跟踪源码发现了delphi 6的源码中的一些bug :(
在 delphi/source/soap 目录下面的XSBuiltIns.pas 文件中,第438行如下:
procedure TXSDate.XSToNative(Value: WideString);
var
TempDate: TDateTime;
begin
FAdditionalYearDigits := Pos(XMLDateSeparator,Value) - (1 + XMLDefaultYearDigits);
TempDate := StrToDate(XMLDateToStr(Value, FAdditionalYearDigits)); // 注意这行代码
DecodeDate(TempDate, FYear, FMonth, FDay);
end;
其中调用了一个xmldatetostr函数,下面是该函数的代码(在xsbuiltins.pas 的241行):
function XMLDateToStr(ADate: WideString; AddDigits: Word = 0): WideString;
begin
Result := Copy(ADate, XMLMonthPos + AddDigits, 2) + DateSeparator +
Copy(ADate, XMLDayPos + AddDigits, 2 ) +
DateSeparator + Copy(ADate, XMLYearPos, XMLDefaultYearDigits + AddDigits);
end;
注意, XMLDateToStr返回的日期格式是 MM-DD-YYYY。例如如果传过去的参数Adate = '2001-12-08',则XMLDateToStr('2001-12-08', 0)的结果是'12-08-2001'。XMLDateToStr的第二个参数AddDigits是多余的年代的位数,估计是用来解决千年虫问题的,对于标准的10位日期格式AddDigits始终是0。
现在问题来了,回到xstonative的代码中,这行代码:
TempDate := StrToDate(XMLDateToStr(Value, FAdditionalYearDigits));
以函数xmldatetostr的返回值作为参数调用了strtodate这个函数, 函数StrToDate的作用是将字符串转化为日期,这个函数的参数应该是一个表示日期的字符串,但是日期的字符串格式必须符合当前平台的区域设置,比如我们常用的中文平台的短日期格式一般是 YYYY-MM-DD,而英文平台一般是 MM-DD-YYYY,所以如果在中文的平台上调用strToDate()来格式化一个格式为"MM-DD-YYYY"的字符串就会产生一个日期格式错误异常!!
下面是delphi的帮助中对StrToDate函数的解释的内容:
function StrToDate(const S: string): TDateTime; description Call StrToDate to parse a string that specifies a date. If S does not contain a valid date,
StrToDate raises an EConvertError exception.S must consist of two or three numbers, separated by the character
defined by the DateSeparator global variable.
the order for month, day, and year is determined by the ShortDateFormat global variable --possible
combinations are m/d/y, d/m/y, and y/m/d.
过程decodedate(tempdate, FYear, FMonth, FDay);的最后一行代码
DecodeDate(TempDate, FYear, FMonth, FDay);
作用是将tempdate重新解析为fyear, FMonth, FDay三个域,其实可以直接从原来的字符串表示的日期中解析出这三个域,所以稍微修改一下就可以解决这个bug。
具体做法是:
复制本页网址和标题,发送给你QQ/Msn的好友一起分享
上一篇:delphi “for” 循环中循环变量的注意事项
下一篇:Delphi 6 Web Services初步评估