Solved

XML date type to TDatetime

Posted on 2011-02-23
3
518 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

739 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