From d4cee0403c467ca21f7984c71c984cc4c50f976b Mon Sep 17 00:00:00 2001 From: Mark van Renswoude Date: Wed, 4 May 2016 11:48:14 +0000 Subject: [PATCH] Fixed: account for milliseconds in XML DateTime longer than 3 digits --- UnitTests/Units/XMLDataBindingUtilsTest.pas | 6 ++++++ XMLDataBindingUtils.pas | 16 +++++++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/UnitTests/Units/XMLDataBindingUtilsTest.pas b/UnitTests/Units/XMLDataBindingUtilsTest.pas index 1d099b0..2269143 100644 --- a/UnitTests/Units/XMLDataBindingUtilsTest.pas +++ b/UnitTests/Units/XMLDataBindingUtilsTest.pas @@ -14,6 +14,7 @@ type procedure TestXMLToDate; procedure TestDateTimeToXML; procedure TestDateToXML; + procedure TestLongMilliseconds; end; @@ -94,6 +95,11 @@ begin end; +procedure TXMLDataBindingUtilsTest.TestLongMilliseconds; +begin + CheckEquals('04-05-2016 13:17:08.013', FormatDateTime('dd-mm-yyyy hh:nn:ss.zzz', XMLToDateTime('2016-05-04T11:17:08.0136668Z', xdtDateTime))); +end; + initialization RegisterTest(TXMLDataBindingUtilsTest.Suite); diff --git a/XMLDataBindingUtils.pas b/XMLDataBindingUtils.pas index 31dcbc9..becef77 100644 --- a/XMLDataBindingUtils.pas +++ b/XMLDataBindingUtils.pas @@ -314,6 +314,7 @@ var msec: Integer; hasTimezone: Boolean; xmlOffset: Integer; + endPos: Integer; begin Result := 0; @@ -373,11 +374,20 @@ begin begin if time[1] = '.' then begin - { Parse milliseconds (.zzz) } - if not TryStrToInt(Copy(time, 2, 3), msec) then + { Parse milliseconds (.zzz+) } + Delete(time, 1, 1); + endPos := 1; + + while (endPos <= Length(time)) and (CharInSet(time[endPos], ['0'..'9'])) do + Inc(endPos); + + Dec(endPos); + + if (endPos = 0) or (not TryStrToInt(Copy(time, 1, Min(endPos, 3)), msec)) then msec := 0; - Delete(time, 1, 4); + if endPos > 0 then + Delete(time, 1, endPos); end; end;