Solved

XML date type to TDatetime

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Specific format 21 182
XSLT: Is it possible to assign number format from a variable? 5 38
XML filtering Windows Event Viewer 10 46
how to update exe applicatio from internet ? 6 68
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

895 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now