Solved

XML date type to TDatetime

Posted on 2011-02-23
3
516 Views
Last Modified: 2012-05-11
I am reading an xml file which has dates in yyy-mmddThh-nn-ssz which i understand is a standard format.

How can I get that into a tdatetime without parsing the the xml date myself?  Is there a function somewhere i can use?

Using Delphi 2010.
0
Comment
Question by:soozh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 19

Expert Comment

by:Thommy
ID: 34967712
I think this is the solution for you...

function XmlDateTimeStrToDateTime(Str: string; ReturnUTC: Boolean):TDateTime

ISO Date/Time Conversion
http://www.delphigroups.info/2/1/248328.html

0
 
LVL 19

Assisted Solution

by:Thommy
Thommy earned 250 total points
ID: 34967913
This is the function you should try (from http://www.delphigroups.info/2/1/248328.html)...

function XmlDateTimeStrToDateTime(Str: string; ReturnUTC: Boolean):TDateTime;
// '2004-06-08T00:00:00.0000000+02:00'
// '2004-06-08T11:47:00.0000000+01:00' // 11:47:00=LokalTid, 12:47:00=UTC
// '2004-06-08T18:47:58.0000000+01:00'
// '2004-06-08T18:47:58.0000000Z'
// ReturnUTC: False=returner lokal tid, True=returner UTC
var
  h, n, s: Word;
  p: Integer;
  Sfrac: string;
  V: Double;
  Code: Integer;
begin
  Result := XmlDateStrToDate(Str);
  p := Pos('T', Str);
  if p = 0 then
    Exit;
  Delete(Str, 1, p); // '11:47:00.0000000+01:00'
  h := StrToInt(Copy(Str, 1, 2));
  n := StrToInt(Copy(Str, 4, 2));
  s := StrToInt(Copy(Str, 7, 2));
  if h = 24 then
  begin
    Result := Result + 1;
    h := 0;
  end;
  Result := Result + EncodeTime(h, n, s, 0);
  Delete(Str, 1, 8); // '.0000000+01:00'
  // fractional seconds // '.0000000+01:00'
  if Copy(Str, 1, 1) = '.' then
  begin
    Delete(Str, 1, 1); // '0000000+01:00'
    p := 0;
    while (p < Length(Str)) and (Str[p+1] in ['0'..'9']) do
      Inc(p);
    if p>0 then
    begin
      Sfrac := '0.' + Copy(Str, 1, p); // '0.0000000'
      Delete(Str, 1, p); // '+01:00' eller 'Z'
      Val(Sfrac, V, Code);
      if Code = 0 then
        Result := Result + (V / SysUtils.SecsPerDay);
    end;
  end;
  // Nu har vi lokal tid
  if ReturnUTC then
  begin
  //Hvis der skal bruges
  // timezone // '+01:00' eller 'Z'
    if Copy(Str, 1, 1) = 'Z' then
      Exit;
    if (Copy(Str, 1, 1) = '-') or (Copy(Str, 1, 1) = '+') then
    begin
      h := StrToInt(Copy(Str, 2, 2));
      n := StrToInt(Copy(Str, 5, 2));
      s := 0;
      if (Copy(Str, 1, 1) = '-') then
        Result := Result + EncodeTime(h, n, s, 0);
      if (Copy(Str, 1, 1) = '+') then
        Result := Result - EncodeTime(h, n, s, 0);
    end;
  end;
end;

Open in new window

0
 
LVL 45

Accepted Solution

by:
aikimark earned 250 total points
ID: 34973592
You should have TXSDateTime in your run-time library.
0

Featured Post

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question