Ho to convert a special format of datetime?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Ho to convert a special format of datetime?

Luciano de Souza
Hello all,
I'd like to convert this date "2017-01-11T17:47:22.2912317-02:00" to TDatetime.
It seems probably it exists in Freepascal, but I don't know.
So, I ask, how to suply a date in this format and get a TDatetime?
I found it in a XPDL file representing a flowchart and I don't know
how to do it easily.
I thank you for any help.
Best Regards,

--
Luciano de Souza
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Ho to convert a special format of datetime?

Marco van de Voort-2

Op 2018-12-13 om 21:07 schreef luciano de souza:
> Hello all,
> I'd like to convert this date "2017-01-11T17:47:22.2912317-02:00" to TDatetime.

A quick attempt with some standard functions, note that it only parses
till millisecond precision, the rest (317) is ignored.

uses dateutils,sysutils,strutils;
const teststring = '2017-01-11T17:47:22.2912317-02:00';
var dt : TDatetime;
   myYear, myMonth, myDay : Word;
   myHour, myMin, mySec, myMilli : Word;
   s3:string;
   i : integer;
begin
   i:=rpos('-',teststring);
   s3:=copy(teststring,1,i-1);


   dt:=scandatetime('yyyy-mm-dd?hh:nn:ss.zzz????',s3);
  writeln(dt);
  decodedatetime(dt,myYear, myMonth, myDay,
                  myHour, myMin, mySec, myMilli);

   writeln('myHour     = '+IntToStr(myHour));
   writeln('myMin      = '+IntToStr(myMin));
   writeln('mySec      = '+IntToStr(mySec));
   writeln('myMilli    = '+IntToStr(myMilli));
   writeln('myDay      = '+IntToStr(myDay));
   writeln('myMonth    = '+IntToStr(myMonth));
   writeln('myYear     = '+IntToStr(myYear));
   s3:=copy(teststring,i+1,length(teststring)-i);

   // parse timezone info
   dt:=scandatetime('hh:nn',s3);
   decodetime(dt,
                  myHour, myMin, mySec, myMilli);
   writeln('tzmyHour     = '+IntToStr(myHour));
   writeln('tzmyMin      = '+IntToStr(myMin));
end.


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Ho to convert a special format of datetime?

silvioprog
In reply to this post by Luciano de Souza
On Thu, Dec 13, 2018 at 5:07 PM luciano de souza <[hidden email]> wrote:
Hello all,
I'd like to convert this date "2017-01-11T17:47:22.2912317-02:00" to TDatetime.

uses restbase;

var
  d: TDateTime;
begin
  d := RFC3339ToDateTime('2017-01-11T17:47:22.2912317-02:00');
  WriteLn(DateTimeToStr(d)); // prints 11-1-17 17:47:22
end.

Anyway, I'm not sure regarding the tmz part, so take a look at Marco's answer.

--
Silvio Clécio

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Ho to convert a special format of datetime?

Luciano de Souza
In reply to this post by Luciano de Souza
Marco, Sílvio, thank you both. Since a ready function is available, I
used RF3339ToDatetime. I received lots of warnings about a possible
lost of data due to a string conversion from widestring to ansistring.
However, dispite the warnings, the result was perfect.
I didn't know scandatetime. Freepascal always present to us some
pleasant surprisses.

2018-12-13 18:07 GMT-02:00, luciano de souza <[hidden email]>:

> Hello all,
> I'd like to convert this date "2017-01-11T17:47:22.2912317-02:00" to
> TDatetime.
> It seems probably it exists in Freepascal, but I don't know.
> So, I ask, how to suply a date in this format and get a TDatetime?
> I found it in a XPDL file representing a flowchart and I don't know
> how to do it easily.
> I thank you for any help.
> Best Regards,
>
> --
> Luciano de Souza
>


--
Luciano de Souza
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal