Recent

Author Topic: Get random time from range of times?  (Read 3655 times)

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Get random time from range of times?
« Reply #15 on: February 28, 2020, 05:29:21 pm »
Hi!

A more general solution:

Code: Pascal  [Select][+][-]
  1. Var DateTime1, DateTime2,DeltaDateTime, DeltaTime : TDateTime;
  2. ....
  3. DeltaDateTime := abs(DateTime1-DateTime2);
  4. DeltaTime := frac (DeltaDateTime);

Winni
« Last Edit: February 28, 2020, 06:18:43 pm by winni »

rvk

  • Hero Member
  • *****
  • Posts: 6112
Re: Get random time from range of times?
« Reply #16 on: February 28, 2020, 05:52:19 pm »
And what would be the best solution to round to second??

(Because using float will result in milliseconds being used)

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Get random time from range of times?
« Reply #17 on: February 28, 2020, 06:01:29 pm »
Code: Pascal  [Select][+][-]
  1. var seconds : Integer;
  2.       DeltaTime : TDateTime;
  3.  
  4. seconds := round(DeltaTime*SecsPerDay);

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Get random time from range of times?
« Reply #18 on: February 28, 2020, 09:04:01 pm »
A more general solution:

Code: Pascal  [Select][+][-]
  1. Var DateTime1, DateTime2,DeltaDateTime, DeltaTime : TDateTime;
  2. ....
  3. DeltaDateTime := abs(DateTime1-DateTime2);
  4. DeltaTime := frac (DeltaDateTime);

The problem with that code is that in a situation where both original datetimes contain only the time (if they are set, for example, with HourOf, an hypotheticall EncodeTime, etc) the result is not what you might expect, as seen above.

An example: Imagine that you use two TTimeEdit to get the times and that you want to know how much you slept this night: you went to bed at 22:30 (time1) and got up at 6:00 (time2): your code will tell you that you slept a whooping 16,5 hours instead of the 7,5h you really spent in bed. Note that inverting the terms (time1-time2) doesn't matter, since it just changes the sign of the substraction.


And what would be the best solution to round to second??

What I would do:

Code: Pascal  [Select][+][-]
  1. if Time1 > Time2 then
  2.   IncDay(Time2, 1);
  3. Result := SecondsBetween(Time2, Time1);
  4. {or, alternatively,
  5. Result := Round(SecondSpan(Time2, Time1));
  6. }
« Last Edit: February 28, 2020, 09:14:15 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Get random time from range of times?
« Reply #19 on: February 28, 2020, 10:59:44 pm »
Hi!

If you use only the two times of two different days, you break the logic of TDateTime.
And so you have to do some cruel workaround.

Garbage in, garbage out.

Or as the cookie monster said: Who wants to eat a broken ukelelee ?

Winni

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Get random time from range of times?
« Reply #20 on: February 28, 2020, 11:29:24 pm »
Hi!

You can get around the mess - which lucamar repairs -  with an easy solution:

If you want only to work with times and not with dates the do just one thing:

If the clock crosses midnight between the two times then add one day.

And now you can continue like I showed it above.

Winni

 

TinyPortal © 2005-2018