source: trunk/Packages/Common/UXMLUtils.pas

Last change on this file was 18, checked in by chronos, 12 years ago
  • Used external packages are now stored in uncompressed form rather in zipped files. This allow better package version synchronisation.
File size: 3.7 KB
Line 
1unit UXMLUtils;
2
3{$mode delphi}
4
5interface
6
7uses
8 {$IFDEF WINDOWS}Windows,{$ENDIF}
9 Classes, SysUtils, DateUtils;
10
11function XMLTimeToDateTime(XMLDateTime: string): TDateTime;
12function DateTimeToXMLTime(Value: TDateTime; ApplyLocalBias: Boolean = True): WideString;
13
14
15implementation
16
17function GetTimeZoneBias: Integer;
18{$IFDEF WINDOWS}
19var
20 TimeZoneInfo: TTimeZoneInformation;
21begin
22 case GetTimeZoneInformation(TimeZoneInfo) of
23 TIME_ZONE_ID_STANDARD: Result := TimeZoneInfo.Bias + TimeZoneInfo.StandardBias;
24 TIME_ZONE_ID_DAYLIGHT: Result := TimeZoneInfo.Bias + TimeZoneInfo.DaylightBias;
25 else
26 Result := 0;
27 end;
28end;
29{$ELSE}
30begin
31 Result := 0;
32end;
33{$ENDIF}
34
35function LeftCutString(var Source: string; out Output: string; Delimiter: string; Allowed: string = ''): Boolean;
36var
37 I, J: Integer;
38 Matched: Boolean;
39begin
40 I := 1;
41 Matched := True;
42 while (I < Length(Source)) and Matched do begin
43 Matched := True;
44 if (Source[I] = Delimiter) then Matched := False;
45 //for J := 1 to Length(Allowed) do
46 // if Source[I] = Allowed[J] then Matched := True;
47 if Matched then Inc(I);
48 end;
49 if (Delimiter = Copy(Source, I, Length(Delimiter))) or (I = Length(Source)) then begin
50 Output := Copy(Source, 1, I - 1);
51 Delete(Source, 1, Length(Output) + Length(Delimiter));
52 Result := True;
53 end else begin
54 Output := '';
55 Result := False;
56 end;
57end;
58
59function XMLTimeToDateTime(XMLDateTime: string): TDateTime;
60var
61 Part: string;
62 Year: Integer;
63 Month: Integer;
64 Day: Integer;
65 Hour: Integer;
66 Minute: Integer;
67 Second: Integer;
68 Millisecond: Integer;
69begin
70 if LeftCutString(XMLDateTime, Part, '-') then
71 Year := StrToInt(Part);
72 if LeftCutString(XMLDateTime, Part, '-') then
73 Month := StrToInt(Part);
74 if Pos('T', XMLDateTime) > 0 then begin
75 if LeftCutString(XMLDateTime, Part, 'T') then
76 Day := StrToInt(Part);
77 if LeftCutString(XMLDateTime, Part, ':') then
78 Hour := StrToInt(Part);
79 if LeftCutString(XMLDateTime, Part, ':') then
80 Minute := StrToInt(Part);
81 if Pos('.', XMLDateTime) > 0 then begin
82 if LeftCutString(XMLDateTime, Part, '.') then
83 Second := StrToInt(Part);
84 if Pos('+', XMLDateTime) > 0 then
85 LeftCutString(XMLDateTime, Part, '+') else
86 if Pos('-', XMLDateTime) > 0 then
87 LeftCutString(XMLDateTime, Part, '-') else
88 if Pos('Z', XMLDateTime) > 0 then
89 LeftCutString(XMLDateTime, Part, 'Z');
90 Millisecond := StrToInt(Part);
91 end else begin
92 if Pos('+', XMLDateTime) > 0 then
93 LeftCutString(XMLDateTime, Part, '+') else
94 if Pos('-', XMLDateTime) > 0 then
95 LeftCutString(XMLDateTime, Part, '-') else
96 if Pos('Z', XMLDateTime) > 0 then
97 LeftCutString(XMLDateTime, Part, 'Z');
98 Second := StrToInt(Part);
99 Millisecond := 0;
100 end;
101 end else begin
102 Day := StrToInt(XMLDateTime);
103 end;
104 Result := EncodeDateTime(Year, Month, Day, Hour, Minute, Second, Millisecond);
105 // TODO: Correct time by zone bias
106end;
107
108function DateTimeToXMLTime(Value: TDateTime; ApplyLocalBias: Boolean = True): WideString;
109const
110 Neg: array[Boolean] of string = ('+', '-');
111var
112 Bias: Integer;
113begin
114 Result := FormatDateTime('yyyy''-''mm''-''dd''T''hh'':''nn'':''ss''.''zzz', Value); { Do not localize }
115 Bias := GetTimeZoneBias;
116 if (Bias <> 0) and ApplyLocalBias then
117 begin
118 Result := Format('%s%s%.2d:%.2d', [Result, Neg[Bias > 0], { Do not localize }
119 Abs(Bias) div MinsPerHour,
120 Abs(Bias) mod MinsPerHour]);
121 end else
122 Result := Result + 'Z'; { Do not localize }
123end;
124
125end.
126
Note: See TracBrowser for help on using the repository browser.