Solved

XML date type to TDatetime

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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 Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

746 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

14 Experts available now in Live!

Get 1:1 Help Now