source: trunk/Packages/Common/UXMLUtils.pas

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