Results 1 to 7 of 7

Thread: Get current time in ms?

  1. #1
    Join Date
    Oct 2011
    Posts
    207
    Mentioned
    1 Post(s)
    Quoted
    20 Post(s)

    Default Get current time in ms?

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

  2. #2
    Join Date
    Feb 2006
    Location
    Tracy/Davis, California
    Posts
    12,631
    Mentioned
    135 Post(s)
    Quoted
    418 Post(s)

    Default

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

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

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

  3. #3
    Join Date
    Sep 2012
    Location
    Australia.
    Posts
    839
    Mentioned
    16 Post(s)
    Quoted
    225 Post(s)

    Default

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

    Simba Code:
    (*
    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.

  4. #4
    Join Date
    Feb 2006
    Location
    Helsinki, Finland
    Posts
    1,395
    Mentioned
    30 Post(s)
    Quoted
    107 Post(s)

    Default

    Quote Originally Posted by djborec View Post
    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:

    Code:
    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.

  5. #5
    Join Date
    Oct 2006
    Location
    Netherlands
    Posts
    3,285
    Mentioned
    105 Post(s)
    Quoted
    494 Post(s)

    Default

    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.
    Last edited by masterBB; 07-18-2013 at 10:33 AM.
    Working on: Tithe Farmer

  6. #6
    Join Date
    Feb 2006
    Location
    Helsinki, Finland
    Posts
    1,395
    Mentioned
    30 Post(s)
    Quoted
    107 Post(s)

    Default

    Quote Originally Posted by masterBB View Post
    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):

    Code:
    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.

  7. #7
    Join Date
    Oct 2006
    Location
    Netherlands
    Posts
    3,285
    Mentioned
    105 Post(s)
    Quoted
    494 Post(s)

    Default

    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:
    Simba Code:
    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.

    Simba Code:
    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.

    Simba Code:
    t := GetSystemTime;
    repeat
      wait(100);
    until(TreeDown() or (GetSystemTime - t) > 3 * 60 * 1000);
    Working on: Tithe Farmer

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •