Recent

Author Topic: TDateTime to number  (Read 41698 times)

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: TDateTime to number
« Reply #15 on: October 05, 2016, 12:30:45 pm »
@rvk:
Well, i've seen similar solutions using truncation beforehand.

I agree with your assessment. But, my agreement might not be considered 'normal :D

(effectively i was doing the same thing by truncating the data-time-value before handing it over to function DateTimeToUnix).

ASerge

  • Hero Member
  • *****
  • Posts: 2477
Re: TDateTime to number
« Reply #16 on: October 05, 2016, 05:01:04 pm »
Are there some lazarus/FP functions to convert a TDateTime to integer numbers and back again?

I need to compact a TDateTime to a string, before to save to a file.

I have tested DateTimeToFileDate() and FileDateToDateTime(), but FileDateToDateTime() not always restore the same date-time coded by DateTimeToFileDate().
What about DateTimeToTimeStamp and TimeStampToDateTime? Two LongInt or make one Int64, if you prefer one number.

Edson

  • Hero Member
  • *****
  • Posts: 1327
Re: TDateTime to number
« Reply #17 on: October 05, 2016, 06:04:45 pm »
What about DateTimeToTimeStamp and TimeStampToDateTime? Two LongInt or make one Int64, if you prefer one number.

Interesting suggestion. But I want to have only one number (the shorter possible). No need milliseconds precision by now.

But because DateTimeToStr only shows the truncated (to seconds) of a TDateTime you could end up with a difference if you don't truncate it the same way for DateTimeToUnix.

Yes. I also think it's deserable DateTimeToUnix() must works the same way DateTimeToStr() works, and in general, every Date-Time function in Lazarus.

But the main problem is that  DateTimeToUnix() is not reversible.  :'(
Lazarus 2.2.6 - FPC 3.2.2 - x86_64-win64 on Windows 10

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12706
  • FPC developer.
Re: TDateTime to number
« Reply #18 on: October 05, 2016, 06:39:59 pm »

dt * (3600*24)

Paulo França Lacerda

  • New Member
  • *
  • Posts: 44
    • BlaisePoint Informática
Re: TDateTime to number
« Reply #19 on: October 05, 2016, 06:50:53 pm »
Already tried typecasting the TDateTime to Int64 and back?

Code: Pascal  [Select][+][-]
  1. var
  2.   I :Int64;
  3.   D :TDateTime;
  4. begin
  5.   D := Now;
  6.   I := Int64(D);  // pack
  7.   D := TDateTime(I);  // unpack
  8. end;
Lazarus 1.6.0.4 on FreePascal 3.0
Windows 7 32-bit

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: TDateTime to number
« Reply #20 on: October 05, 2016, 06:53:31 pm »
Attempting to use simply casts unfortunately also fails. Operator overloading not allowed, and more of that, so:

Code: [Select]
Function DT2Number(dt: TDateTime): QWord; inline;
var
  dtq : Qword absolute dt;
begin
  Result := dtq;
end;


function Number2DT(n: QWord): TDateTime; inline;
var
  dtdt : TDateTime absolute n;
begin
  Result := dtdt;
end;


Already tried typecasting the TDateTime to Int64 and back?

Yes, it crashes;

Paulo França Lacerda

  • New Member
  • *
  • Posts: 44
    • BlaisePoint Informática
Re: TDateTime to number
« Reply #21 on: October 05, 2016, 07:01:15 pm »
Already tried typecasting the TDateTime to Int64 and back?
Yes, it crashes;

This also works for me on Windows 7:
Code: Pascal  [Select][+][-]
  1. function dt2int (dt:TDateTime) :Int64;
  2. var I:Int64 absolute dt;
  3. begin
  4.   Result := I;
  5. end;
  6.  
  7. function int2dt (I:Int64) :TDateTime;
  8. var dt:TDateTime absolute I;
  9. begin
  10.   Result := dt;
  11. end;
Lazarus 1.6.0.4 on FreePascal 3.0
Windows 7 32-bit

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: TDateTime to number
« Reply #22 on: October 05, 2016, 07:17:05 pm »
This also works for me on Windows 7:
I do hope so, otherwise there is something serious amiss. I just did not wanted to sound too negative  :D

Paulo França Lacerda

  • New Member
  • *
  • Posts: 44
    • BlaisePoint Informática
Re: TDateTime to number
« Reply #23 on: October 05, 2016, 10:09:56 pm »
This also works for me on Windows 7:
I do hope so, otherwise there is something serious amiss. I just did not wanted to sound too negative  :D
Although he (the post creator) is running on a 64-bit version of Windows 7. Mine is 32-bit.
This may be the key difference in the results.
Lazarus 1.6.0.4 on FreePascal 3.0
Windows 7 32-bit

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: TDateTime to number
« Reply #24 on: October 05, 2016, 10:38:18 pm »
Sorry @patulinu.

My remark was a poor attempt of making a joke about why my example code uses QWord instead of Int64 (the QWord is used to express/store unsigned integer values, while an Int64 also allows for negative values).

Paulo França Lacerda

  • New Member
  • *
  • Posts: 44
    • BlaisePoint Informática
Re: TDateTime to number
« Reply #25 on: October 05, 2016, 10:46:28 pm »
My remark was a poor attempt of making a joke about why my example code uses QWord instead of Int64 (the QWord is used to express/store unsigned integer values, while an Int64 also allows for negative values).
No problem about the joke.  :)
As for the QWord/Int64, I believe in this case both should work fine, provided no conversion is made, just raw-byte encapsulation.

P.S.: Since he's on a 64-bit platform, the QWord approach
is probably the best solution.
« Last Edit: October 05, 2016, 10:49:14 pm by Paulo França Lacerda (patulinu) »
Lazarus 1.6.0.4 on FreePascal 3.0
Windows 7 32-bit

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1575
    • Lebeau Software
Re: TDateTime to number
« Reply #26 on: October 05, 2016, 10:52:40 pm »
I need to compact a TDateTime to a string, before to save to a file.

Why not just encode the TDateTime in a standard ISO 8601 format?
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: TDateTime to number
« Reply #27 on: October 05, 2016, 11:01:55 pm »
@Remy Lebeau:
TDateTime back-n-forth.

Are there msecs in ISO 8601 ? (i was unable to locate)

If not, then TS is running into the same issue.

rvk

  • Hero Member
  • *****
  • Posts: 6948
Re: TDateTime to number
« Reply #28 on: October 05, 2016, 11:04:47 pm »
Are there msecs in ISO 8601 ? (i was unable to locate)
Yes.
Quote
Decimal fractions may be added to any of the three time elements. However, a fraction may only be added to the lowest order time element in the representation.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: TDateTime to number
« Reply #29 on: October 05, 2016, 11:48:06 pm »
thank you very much rvk. (and sorry, as that's what you get when peeking too quickly over those pages).

 

TinyPortal © 2005-2018