| 1 | unit UXMLUtils;
|
|---|
| 2 |
|
|---|
| 3 | {$mode delphi}
|
|---|
| 4 |
|
|---|
| 5 | interface
|
|---|
| 6 |
|
|---|
| 7 | uses
|
|---|
| 8 | {$IFDEF WINDOWS}Windows,{$ENDIF}
|
|---|
| 9 | Classes, SysUtils, DateUtils, XMLRead, XMLWrite, DOM;
|
|---|
| 10 |
|
|---|
| 11 | function XMLTimeToDateTime(XMLDateTime: string): TDateTime;
|
|---|
| 12 | function DateTimeToXMLTime(Value: TDateTime; ApplyLocalBias: Boolean = True): WideString;
|
|---|
| 13 | procedure WriteInteger(Node: TDOMNode; Name: string; Value: Integer);
|
|---|
| 14 | procedure WriteBoolean(Node: TDOMNode; Name: string; Value: Boolean);
|
|---|
| 15 | procedure WriteString(Node: TDOMNode; Name: string; Value: string);
|
|---|
| 16 | function ReadInteger(Node: TDOMNode; Name: string; DefaultValue: Integer): Integer;
|
|---|
| 17 | function ReadBoolean(Node: TDOMNode; Name: string; DefaultValue: Boolean): Boolean;
|
|---|
| 18 | function ReadString(Node: TDOMNode; Name: string; DefaultValue: string): string;
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 | implementation
|
|---|
| 22 |
|
|---|
| 23 | function GetTimeZoneBias: Integer;
|
|---|
| 24 | {$IFDEF WINDOWS}
|
|---|
| 25 | var
|
|---|
| 26 | TimeZoneInfo: TTimeZoneInformation;
|
|---|
| 27 | begin
|
|---|
| 28 | case GetTimeZoneInformation(TimeZoneInfo) of
|
|---|
| 29 | TIME_ZONE_ID_STANDARD: Result := TimeZoneInfo.Bias + TimeZoneInfo.StandardBias;
|
|---|
| 30 | TIME_ZONE_ID_DAYLIGHT: Result := TimeZoneInfo.Bias + TimeZoneInfo.DaylightBias;
|
|---|
| 31 | else
|
|---|
| 32 | Result := 0;
|
|---|
| 33 | end;
|
|---|
| 34 | end;
|
|---|
| 35 | {$ELSE}
|
|---|
| 36 | begin
|
|---|
| 37 | Result := 0;
|
|---|
| 38 | end;
|
|---|
| 39 | {$ENDIF}
|
|---|
| 40 |
|
|---|
| 41 | function LeftCutString(var Source: string; out Output: string; Delimiter: string; Allowed: string = ''): Boolean;
|
|---|
| 42 | var
|
|---|
| 43 | I, J: Integer;
|
|---|
| 44 | Matched: Boolean;
|
|---|
| 45 | begin
|
|---|
| 46 | I := 1;
|
|---|
| 47 | Matched := True;
|
|---|
| 48 | while (I < Length(Source)) and Matched do begin
|
|---|
| 49 | Matched := True;
|
|---|
| 50 | if (Source[I] = Delimiter) then Matched := False;
|
|---|
| 51 | //for J := 1 to Length(Allowed) do
|
|---|
| 52 | // if Source[I] = Allowed[J] then Matched := True;
|
|---|
| 53 | if Matched then Inc(I);
|
|---|
| 54 | end;
|
|---|
| 55 | if (Delimiter = Copy(Source, I, Length(Delimiter))) or (I = Length(Source)) then begin
|
|---|
| 56 | Output := Copy(Source, 1, I - 1);
|
|---|
| 57 | Delete(Source, 1, Length(Output) + Length(Delimiter));
|
|---|
| 58 | Result := True;
|
|---|
| 59 | end else begin
|
|---|
| 60 | Output := '';
|
|---|
| 61 | Result := False;
|
|---|
| 62 | end;
|
|---|
| 63 | end;
|
|---|
| 64 |
|
|---|
| 65 | function XMLTimeToDateTime(XMLDateTime: string): TDateTime;
|
|---|
| 66 | var
|
|---|
| 67 | Part: string;
|
|---|
| 68 | Year: Integer;
|
|---|
| 69 | Month: Integer;
|
|---|
| 70 | Day: Integer;
|
|---|
| 71 | Hour: Integer;
|
|---|
| 72 | Minute: Integer;
|
|---|
| 73 | Second: Integer;
|
|---|
| 74 | Millisecond: Integer;
|
|---|
| 75 | begin
|
|---|
| 76 | if LeftCutString(XMLDateTime, Part, '-') then
|
|---|
| 77 | Year := StrToInt(Part);
|
|---|
| 78 | if LeftCutString(XMLDateTime, Part, '-') then
|
|---|
| 79 | Month := StrToInt(Part);
|
|---|
| 80 | if Pos('T', XMLDateTime) > 0 then begin
|
|---|
| 81 | if LeftCutString(XMLDateTime, Part, 'T') then
|
|---|
| 82 | Day := StrToInt(Part);
|
|---|
| 83 | if LeftCutString(XMLDateTime, Part, ':') then
|
|---|
| 84 | Hour := StrToInt(Part);
|
|---|
| 85 | if LeftCutString(XMLDateTime, Part, ':') then
|
|---|
| 86 | Minute := StrToInt(Part);
|
|---|
| 87 | if Pos('.', XMLDateTime) > 0 then begin
|
|---|
| 88 | if LeftCutString(XMLDateTime, Part, '.') then
|
|---|
| 89 | Second := StrToInt(Part);
|
|---|
| 90 | if Pos('+', XMLDateTime) > 0 then
|
|---|
| 91 | LeftCutString(XMLDateTime, Part, '+') else
|
|---|
| 92 | if Pos('-', XMLDateTime) > 0 then
|
|---|
| 93 | LeftCutString(XMLDateTime, Part, '-') else
|
|---|
| 94 | if Pos('Z', XMLDateTime) > 0 then
|
|---|
| 95 | LeftCutString(XMLDateTime, Part, 'Z');
|
|---|
| 96 | Millisecond := StrToInt(Part);
|
|---|
| 97 | end else begin
|
|---|
| 98 | if Pos('+', XMLDateTime) > 0 then
|
|---|
| 99 | LeftCutString(XMLDateTime, Part, '+') else
|
|---|
| 100 | if Pos('-', XMLDateTime) > 0 then
|
|---|
| 101 | LeftCutString(XMLDateTime, Part, '-') else
|
|---|
| 102 | if Pos('Z', XMLDateTime) > 0 then
|
|---|
| 103 | LeftCutString(XMLDateTime, Part, 'Z');
|
|---|
| 104 | Second := StrToInt(Part);
|
|---|
| 105 | Millisecond := 0;
|
|---|
| 106 | end;
|
|---|
| 107 | end else begin
|
|---|
| 108 | Day := StrToInt(XMLDateTime);
|
|---|
| 109 | end;
|
|---|
| 110 | Result := EncodeDateTime(Year, Month, Day, Hour, Minute, Second, Millisecond);
|
|---|
| 111 | // TODO: Correct time by zone bias
|
|---|
| 112 | end;
|
|---|
| 113 |
|
|---|
| 114 | function DateTimeToXMLTime(Value: TDateTime; ApplyLocalBias: Boolean = True): WideString;
|
|---|
| 115 | const
|
|---|
| 116 | Neg: array[Boolean] of string = ('+', '-');
|
|---|
| 117 | var
|
|---|
| 118 | Bias: Integer;
|
|---|
| 119 | begin
|
|---|
| 120 | Result := FormatDateTime('yyyy''-''mm''-''dd''T''hh'':''nn'':''ss''.''zzz', Value); { Do not localize }
|
|---|
| 121 | Bias := GetTimeZoneBias;
|
|---|
| 122 | if (Bias <> 0) and ApplyLocalBias then
|
|---|
| 123 | begin
|
|---|
| 124 | Result := Format('%s%s%.2d:%.2d', [Result, Neg[Bias > 0], { Do not localize }
|
|---|
| 125 | Abs(Bias) div MinsPerHour,
|
|---|
| 126 | Abs(Bias) mod MinsPerHour]);
|
|---|
| 127 | end else
|
|---|
| 128 | Result := Result + 'Z'; { Do not localize }
|
|---|
| 129 | end;
|
|---|
| 130 |
|
|---|
| 131 | procedure WriteInteger(Node: TDOMNode; Name: string; Value: Integer);
|
|---|
| 132 | var
|
|---|
| 133 | NewNode: TDOMNode;
|
|---|
| 134 | begin
|
|---|
| 135 | NewNode := Node.OwnerDocument.CreateElement(Name);
|
|---|
| 136 | NewNode.TextContent := IntToStr(Value);
|
|---|
| 137 | Node.AppendChild(NewNode);
|
|---|
| 138 | end;
|
|---|
| 139 |
|
|---|
| 140 | procedure WriteBoolean(Node: TDOMNode; Name: string; Value: Boolean);
|
|---|
| 141 | var
|
|---|
| 142 | NewNode: TDOMNode;
|
|---|
| 143 | begin
|
|---|
| 144 | NewNode := Node.OwnerDocument.CreateElement(Name);
|
|---|
| 145 | NewNode.TextContent := BoolToStr(Value);
|
|---|
| 146 | Node.AppendChild(NewNode);
|
|---|
| 147 | end;
|
|---|
| 148 |
|
|---|
| 149 | procedure WriteString(Node: TDOMNode; Name: string; Value: string);
|
|---|
| 150 | var
|
|---|
| 151 | NewNode: TDOMNode;
|
|---|
| 152 | begin
|
|---|
| 153 | NewNode := Node.OwnerDocument.CreateElement(Name);
|
|---|
| 154 | NewNode.TextContent := Value;
|
|---|
| 155 | Node.AppendChild(NewNode);
|
|---|
| 156 | end;
|
|---|
| 157 |
|
|---|
| 158 | function ReadInteger(Node: TDOMNode; Name: string; DefaultValue: Integer): Integer;
|
|---|
| 159 | var
|
|---|
| 160 | NewNode: TDOMNode;
|
|---|
| 161 | begin
|
|---|
| 162 | Result := DefaultValue;
|
|---|
| 163 | NewNode := Node.FindNode(Name);
|
|---|
| 164 | if Assigned(NewNode) then
|
|---|
| 165 | Result := StrToInt(NewNode.TextContent);
|
|---|
| 166 | end;
|
|---|
| 167 |
|
|---|
| 168 | function ReadBoolean(Node: TDOMNode; Name: string; DefaultValue: Boolean): Boolean;
|
|---|
| 169 | var
|
|---|
| 170 | NewNode: TDOMNode;
|
|---|
| 171 | begin
|
|---|
| 172 | Result := DefaultValue;
|
|---|
| 173 | NewNode := Node.FindNode(Name);
|
|---|
| 174 | if Assigned(NewNode) then
|
|---|
| 175 | Result := StrToBool(NewNode.TextContent);
|
|---|
| 176 | end;
|
|---|
| 177 |
|
|---|
| 178 | function ReadString(Node: TDOMNode; Name: string; DefaultValue: string): string;
|
|---|
| 179 | var
|
|---|
| 180 | NewNode: TDOMNode;
|
|---|
| 181 | begin
|
|---|
| 182 | Result := DefaultValue;
|
|---|
| 183 | NewNode := Node.FindNode(Name);
|
|---|
| 184 | if Assigned(NewNode) then
|
|---|
| 185 | Result := NewNode.TextContent;
|
|---|
| 186 | end;
|
|---|
| 187 |
|
|---|
| 188 | end.
|
|---|
| 189 |
|
|---|