?
Solved

XML date type to TDatetime

Posted on 2011-02-23
3
Medium Priority
?
535 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 1000 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 46

Accepted Solution

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

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

807 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