source: trunk/Packages/Common/UXMLUtils.pas

Last change on this file was 38, checked in by chronos, 7 years ago
  • Modified: Updated Common package.
File size: 7.3 KB
Line 
1unit UXMLUtils;
2
3{$mode delphi}
4
5interface
6
7uses
8 {$IFDEF WINDOWS}Windows,{$ENDIF}
9 Classes, SysUtils, DateUtils, DOM;
10
11function XMLTimeToDateTime(XMLDateTime: string): TDateTime;
12function DateTimeToXMLTime(Value: TDateTime; ApplyLocalBias: Boolean = True): string;
13procedure WriteInteger(Node: TDOMNode; Name: string; Value: Integer);
14procedure WriteInt64(Node: TDOMNode; Name: string; Value: Int64);
15procedure WriteBoolean(Node: TDOMNode; Name: string; Value: Boolean);
16procedure WriteString(Node: TDOMNode; Name: string; Value: string);
17procedure WriteDateTime(Node: TDOMNode; Name: string; Value: TDateTime);
18function ReadInteger(Node: TDOMNode; Name: string; DefaultValue: Integer): Integer;
19function ReadInt64(Node: TDOMNode; Name: string; DefaultValue: Int64): Int64;
20function ReadBoolean(Node: TDOMNode; Name: string; DefaultValue: Boolean): Boolean;
21function ReadString(Node: TDOMNode; Name: string; DefaultValue: string): string;
22function ReadDateTime(Node: TDOMNode; Name: string; DefaultValue: TDateTime): TDateTime;
23
24
25implementation
26
27function GetTimeZoneBias: Integer;
28{$IFDEF WINDOWS}
29var
30 TimeZoneInfo: TTimeZoneInformation;
31begin
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}
40end;
41{$ELSE}
42begin
43 Result := 0;
44end;
45{$ENDIF}
46
47function LeftCutString(var Source: string; out Output: string; Delimiter: string; Allowed: string = ''): Boolean;
48var
49 I: Integer;
50 Matched: Boolean;
51begin
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;
69end;
70
71function XMLTimeToDateTime(XMLDateTime: string): TDateTime;
72var
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;
82begin
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
120end;
121
122function DateTimeToXMLTime(Value: TDateTime; ApplyLocalBias: Boolean = True): string;
123const
124 Neg: array[Boolean] of string = ('+', '-');
125var
126 Bias: Integer;
127begin
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 }
137end;
138
139procedure WriteInteger(Node: TDOMNode; Name: string; Value: Integer);
140var
141 NewNode: TDOMNode;
142begin
143 NewNode := Node.OwnerDocument.CreateElement(DOMString(Name));
144 NewNode.TextContent := DOMString(IntToStr(Value));
145 Node.AppendChild(NewNode);
146end;
147
148procedure WriteInt64(Node: TDOMNode; Name: string; Value: Int64);
149var
150 NewNode: TDOMNode;
151begin
152 NewNode := Node.OwnerDocument.CreateElement(DOMString(Name));
153 NewNode.TextContent := DOMString(IntToStr(Value));
154 Node.AppendChild(NewNode);
155end;
156
157procedure WriteBoolean(Node: TDOMNode; Name: string; Value: Boolean);
158var
159 NewNode: TDOMNode;
160begin
161 NewNode := Node.OwnerDocument.CreateElement(DOMString(Name));
162 NewNode.TextContent := DOMString(BoolToStr(Value));
163 Node.AppendChild(NewNode);
164end;
165
166procedure WriteString(Node: TDOMNode; Name: string; Value: string);
167var
168 NewNode: TDOMNode;
169begin
170 NewNode := Node.OwnerDocument.CreateElement(DOMString(Name));
171 NewNode.TextContent := DOMString(Value);
172 Node.AppendChild(NewNode);
173end;
174
175procedure WriteDateTime(Node: TDOMNode; Name: string; Value: TDateTime);
176var
177 NewNode: TDOMNode;
178begin
179 NewNode := Node.OwnerDocument.CreateElement(DOMString(Name));
180 NewNode.TextContent := DOMString(DateTimeToXMLTime(Value));
181 Node.AppendChild(NewNode);
182end;
183
184function ReadInteger(Node: TDOMNode; Name: string; DefaultValue: Integer): Integer;
185var
186 NewNode: TDOMNode;
187begin
188 Result := DefaultValue;
189 NewNode := Node.FindNode(DOMString(Name));
190 if Assigned(NewNode) then
191 Result := StrToInt(string(NewNode.TextContent));
192end;
193
194function ReadInt64(Node: TDOMNode; Name: string; DefaultValue: Int64): Int64;
195var
196 NewNode: TDOMNode;
197begin
198 Result := DefaultValue;
199 NewNode := Node.FindNode(DOMString(Name));
200 if Assigned(NewNode) then
201 Result := StrToInt64(string(NewNode.TextContent));
202end;
203
204function ReadBoolean(Node: TDOMNode; Name: string; DefaultValue: Boolean): Boolean;
205var
206 NewNode: TDOMNode;
207begin
208 Result := DefaultValue;
209 NewNode := Node.FindNode(DOMString(Name));
210 if Assigned(NewNode) then
211 Result := StrToBool(string(NewNode.TextContent));
212end;
213
214function ReadString(Node: TDOMNode; Name: string; DefaultValue: string): string;
215var
216 NewNode: TDOMNode;
217begin
218 Result := DefaultValue;
219 NewNode := Node.FindNode(DOMString(Name));
220 if Assigned(NewNode) then
221 Result := string(NewNode.TextContent);
222end;
223
224function ReadDateTime(Node: TDOMNode; Name: string; DefaultValue: TDateTime
225 ): TDateTime;
226var
227 NewNode: TDOMNode;
228begin
229 Result := DefaultValue;
230 NewNode := Node.FindNode(DOMString(Name));
231 if Assigned(NewNode) then
232 Result := XMLTimeToDateTime(string(NewNode.TextContent));
233end;
234
235end.
236
Note: See TracBrowser for help on using the repository browser.