SRL Archive - OSR Help

Get current time in ms?


djborec

2013-07-18 04:18:42

Is there any function which return current time in ms, not time computer running (GetSystemTime) ,but current time in ms.

YoHoJo

2013-07-18 04:39:19

SRL has a function called TimeRunning built into it.
It works like this:

function TimeRunning: string;
begin
Result := MsToTime(GetTimeRunning, Time_Formal);
end;

I'm sure you could somehow edit or mod that to become pure MS

Chris!

2013-07-18 04:44:01

The function you're looking for is 'GetTimeRunning'. GetTimeRunning outputs in pure ms, which TimeRunning lacks as it utilises MsToTime:

(*
MsToTime
~~~~~~~~

.. code-block:: pascal

function MsToTime(MS, StrType: Integer): string;

Takes MS in milliseconds and outputs a string with hours, mins and
seconds. Different styles can be created with different StrType values:
Str Type:

- Time_Formal: 2 Hours, 47 Minutes and 28 Seconds
- Time_Short: 02h, 47m, 28s
- Time_Abbrev: 2 hr, 47 min, 28 sec
- Time_Bare: 02:47:28
- Time_FStop: 12.04.40

.. note::

by Zephyrsfury, Nava2 and Rasta Magician.

Example:

.. code-block:: pascal

*)
function MsToTime(MS, TheType: Integer): string;
var
STA: array [0..4] of TVariantArray;
Time: array [0..2] of Integer;
i: Integer;
begin
Result := '';
if (not(InRange(TheType, 0, High(STA)))) then
begin
srl_Warn('MsToTime', 'TheType: ''' + IntToStr(TheType) + ''' is not a valid value. Using Time_Bare.', warn_AllVersions);
TheType := Time_Bare;
end;
STA[Time_Formal] := [' Hours, ', ' Minutes and ', ' Seconds', False, 0];
STA[Time_Short] := ['h ', 'm ', 's', False, 2];
STA[Time_Abbrev] := [' hr ', ' min ', ' sec', False, 0];
STA[Time_Bare] := [':', ':', '', True, 2];
STA[Time_FStop] := ['.', '.', '', True, 2];
ConvertTime(MS, Time[0], Time[1], Time[2]);
for i := 0 to 2 do
if (Time[i] > 0) or (STA[TheType][3]) or (i = 2) then
Result := Result + PadZ(IntToStr(Time[i]), STA[TheType][4]) + STA[TheType][i];
end;

Enjoy.

Janilabo

2013-07-18 06:06:47

From: djborec
Is there any function which return current time in ms, not time computer running (GetSystemTime) ,but current time in ms.
Hey djborec, is this possibly what you are after:

function CurrentTimeToMS: Int64;
var
h, m, s, ms: Word;
begin
DecodeTime(Now, h, m, s, ms);
Result := ((Integer(h) * 3600000) + (Integer(m) * 60000) + (Integer(s) * 1000) + Integer(ms));
end;

function ConvertToMS(hours, minutes, seconds, milliseconds: Word): Int64;
begin
Result := ((Integer(hours) * 3600000) + (Integer(minutes) * 60000) + (Integer(seconds) * 1000) + Integer(milliseconds));
end;

var
h, m, s, ms: Word;

begin
ClearDebug;
WriteLn('CurrentTimeToMS: ' + IntToStr(CurrentTimeToMS));
DecodeTime(Now, h, m, s, ms);
WriteLn('DecodeTime(Now, h, m, s, ms) + ConvertTime(h, m, s, ms): ' + IntToStr(ConvertToMS(h, m, s, ms)));
end.

Added 2 ways to do it. ;)

masterBB

2013-07-18 06:25:41

Dudes… wtf…

writeln(86400000 * (Now - Floor(Now)));

e:
nvm, I am not sure what he is asking. But what I posted should be used instead of what janilabo posted.

Janilabo

2013-07-18 06:42:51

From: masterBB
Dudes… wtf…

writeln(86400000 * (Now - Floor(Now)));

e:
nvm, I am not sure what he is asking. But what I posted should be used instead of what janilabo posted.
Nice Bart, that's definitely better way to do this!

So including Bart's method (which is A LOT more accurate):

function CurrentTimeToMS: Int64;
var
h, m, s, ms: Word;
begin
DecodeTime(Now, h, m, s, ms);
Result := ((Integer(h) * 3600000) + (Integer(m) * 60000) + (Integer(s) * 1000) + Integer(ms));
end;

function CurrentTimeToMSEx: Extended;
begin
Result := (86400000 * (Now - Floor(Now)));
end;

function ConvertToMS(hours, minutes, seconds, milliseconds: Word): Int64;
begin
Result := ((Int64(hours) * 3600000) + (Integer(minutes) * 60000) + (Integer(seconds) * 1000) + Integer(milliseconds));
end;

var
h, m, s, ms: Word;

begin
ClearDebug;
WriteLn('CurrentTimeToMS: ' + IntToStr(CurrentTimeToMS));
WriteLn('CurrentTimeToMSEx: ' + FloatToStr(CurrentTimeToMSEx));
DecodeTime(Now, h, m, s, ms);
WriteLn('DecodeTime(Now, h, m, s, ms) + ConvertTime(h, m, s, ms): ' + IntToStr(ConvertToMS(h, m, s, ms)));
end.

masterBB

2013-07-18 07:04:23

Time

  • GetTimeRunning
  • Now
  • GetSystemTime

GetTimeRunning



Returns the time since you pressed run in simba in ms. Is very useful for progress reports etc. Example usage:
logsAnHour := logs * 60 * 1000 / GetTimeRunning();

Now



Returns the current time and date. The number before the point represents the amount of days since 12/30/1899. Today it is 41473.

The second part returns the fraction of the day. For example, 6:00 -> 0.25 and 12:00 -> 0.5 and 18:00 -> 0.75. By multiplying it with the amount of ms in a day you can get the amount of ms passes since midnight.

writeln(86400000 * (Now - Floor(Now)));

GetSystemTime



GetSystemTime is the time the system is up and running in ms. It can be used for various tasks suchs as adding it as a failsafe to loops.

t := GetSystemTime;
repeat
wait(100);
until(TreeDown() or (GetSystemTime - t) > 3 * 60 * 1000);