| 1 | unit UXMLUtils;
|
|---|
| 2 |
|
|---|
| 3 | {$mode delphi}
|
|---|
| 4 |
|
|---|
| 5 | interface
|
|---|
| 6 |
|
|---|
| 7 | uses
|
|---|
| 8 | {$IFDEF WINDOWS}Windows,{$ENDIF}
|
|---|
| 9 | Classes, SysUtils, DateUtils;
|
|---|
| 10 |
|
|---|
| 11 | function XMLTimeToDateTime(XMLDateTime: string): TDateTime;
|
|---|
| 12 | function DateTimeToXMLTime(Value: TDateTime; ApplyLocalBias: Boolean = True): WideString;
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 | implementation
|
|---|
| 16 |
|
|---|
| 17 | function GetTimeZoneBias: Integer;
|
|---|
| 18 | {$IFDEF WINDOWS}
|
|---|
| 19 | var
|
|---|
| 20 | TimeZoneInfo: TTimeZoneInformation;
|
|---|
| 21 | begin
|
|---|
| 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;
|
|---|
| 28 | end;
|
|---|
| 29 | {$ELSE}
|
|---|
| 30 | begin
|
|---|
| 31 | Result := 0;
|
|---|
| 32 | end;
|
|---|
| 33 | {$ENDIF}
|
|---|
| 34 |
|
|---|
| 35 | function LeftCutString(var Source: string; out Output: string; Delimiter: string; Allowed: string = ''): Boolean;
|
|---|
| 36 | var
|
|---|
| 37 | I, J: Integer;
|
|---|
| 38 | Matched: Boolean;
|
|---|
| 39 | begin
|
|---|
| 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;
|
|---|
| 57 | end;
|
|---|
| 58 |
|
|---|
| 59 | function XMLTimeToDateTime(XMLDateTime: string): TDateTime;
|
|---|
| 60 | var
|
|---|
| 61 | Part: string;
|
|---|
| 62 | Year: Integer;
|
|---|
| 63 | Month: Integer;
|
|---|
| 64 | Day: Integer;
|
|---|
| 65 | Hour: Integer;
|
|---|
| 66 | Minute: Integer;
|
|---|
| 67 | Second: Integer;
|
|---|
| 68 | Millisecond: Integer;
|
|---|
| 69 | begin
|
|---|
| 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
|
|---|
| 106 | end;
|
|---|
| 107 |
|
|---|
| 108 | function DateTimeToXMLTime(Value: TDateTime; ApplyLocalBias: Boolean = True): WideString;
|
|---|
| 109 | const
|
|---|
| 110 | Neg: array[Boolean] of string = ('+', '-');
|
|---|
| 111 | var
|
|---|
| 112 | Bias: Integer;
|
|---|
| 113 | begin
|
|---|
| 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 }
|
|---|
| 123 | end;
|
|---|
| 124 |
|
|---|
| 125 | end.
|
|---|
| 126 |
|
|---|