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