Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (*
- Time
- ====
- The time file includes any functions that have anything to do with time: timers,
- the time of day, conversions, etc.
- The source for this file can be found `here <https://github.com/SRL/SRL-6/blob/master/lib/utilities/time.simba>`_.
- *)
- {$f-}
- (*
- const Date/Time
- ~~~~~~~~~~~~~~~
- Integer constants that represent the different time formats that can be used
- to display the time and dates in a string.
- Example:
- .. code-block:: pascal
- writeln(500000, TIME_SHORT);
- *)
- const
- TIME_FORMAL = 0;
- TIME_SHORT = 1;
- TIME_ABBREV = 2;
- TIME_BARE = 3;
- TIME_FSTOP = 4;
- TIME_FORMAL_LONG = 5;
- DATE_FORMAL = 1;
- DATE_MONTH = 2;
- DATE_DAY = 3;
- (*
- 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
- - TIME_FORMAL_LONG: 1 Years, 3 Months, 2 Weeks, 4 Days, 13 Hours, 3 Minutes, and 20 Seconds
- .. note::
- - by Zephyrsfury, Nava2 and Rasta Magician.
- - Last updated: 4/12/2013 by Ashaman88
- Example:
- .. code-block:: pascal
- writeln(500000, TIME_SHORT);
- *)
- function msToTime(MS, TheType: Integer): string;
- var
- STA: array [0..5] of TVariantArray;
- Time: array of Integer;
- i, t, tl: Integer;
- begin
- Result := '';
- if (not(InRange(TheType, 0, High(STA)))) then
- TheType := TIME_BARE;
- if TheType = TIME_FORMAL_LONG then
- begin
- tl := 7;
- t := 6;
- SetLength(Time,tl);
- ConvertTime64(MS, Time[0], Time[1], Time[2], Time[3], Time[4], Time[5], Time[6]);
- end else
- begin
- tl := 3;
- t := 2;
- SetLength(Time,tl);
- ConvertTime(MS, Time[0], Time[1], Time[2]);
- 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];
- STA[TIME_FORMAL_LONG] := [' Years, ', ' Months, ', ' Weeks, ', ' Days, ', ' Hours, ', ' Minutes and ', ' Seconds', False, 0];
- case theType of
- TIME_FORMAL:
- begin
- if time[0]= 1 then
- sta[TIME_FORMAL][0]:=' Hour, ';
- if time[1]= 1 then
- sta[TIME_FORMAL][1]:=' Minute and ';
- if time[2]= 1 then
- sta[TIME_FORMAL][2]:=' Second';
- end;
- TIME_FORMAL_LONG:
- begin
- if time[0]= 1 then
- sta[TIME_FORMAL_LONG][0]:=' Year, ';
- if time[1]= 1 then
- sta[TIME_FORMAL_LONG][1]:=' Month, ';
- if time[2]= 1 then
- sta[TIME_FORMAL_LONG][2]:=' Week, ';
- if time[3]= 1 then
- sta[TIME_FORMAL_LONG][3]:=' Day, ';
- if time[4]= 1 then
- sta[TIME_FORMAL_LONG][4]:=' Hour, ';
- if time[5]= 1 then
- sta[TIME_FORMAL_LONG][5]:=' Minute and ';
- if time[6]= 1 then
- sta[TIME_FORMAL_LONG][6]:=' Second';
- end;
- end;
- for i := 0 to t do
- if (Time[i] > 0) or (STA[TheType][tl]) or (i = t) then
- Result := Result + PadZ(IntToStr(Time[i]), STA[TheType][tl+1]) + STA[TheType][i];
- end;
- (*
- timeRunning
- ~~~~~~~~~~~
- .. code-block:: pascal
- function timeRunning: String;
- Returns Time since the script was started (GetTimeRunning).
- .. note::
- - by Rasta Magician.
- - Last updated: 4/12/2013 by Ashaman88
- Example:
- .. code-block:: pascal
- writeln('Script time: '+timeRunning);
- *)
- function timeRunning(TheType: Integer = TIME_FORMAL_LONG): string;
- begin
- Result := msToTime(GetTimeRunning(), TheType);
- end;
- (*
- theTime
- ~~~~~~~~
- .. code-block:: pascal
- function theTime: string;
- Returns current time as a string.
- .. note::
- - by RsN (fixed by Ron and Markus)
- Example:
- .. code-block:: pascal
- writeln(theTime);
- *)
- function theTime: string;
- var
- Hour, Mins, Sec, MSec: Word;
- PAM: string;
- begin
- DecodeTime(Now(), Hour, Mins, Sec, MSec);
- PAM := 'AM';
- if (Hour > 12) then
- begin
- Hour := Hour - 12;
- PAM := 'PM';
- end else if (Hour = 12) then
- PAM := 'PM'
- else if (Hour = 0) then
- Hour := 12;
- Result := (Padz(IntToStr(Hour), 2) + ':' + Padz(IntToStr(Mins), 2) + ':' + Padz(IntToStr(Sec), 2) + ' ' + PAM);
- end;
- (*
- theDate
- ~~~~~~~
- .. code-block:: pascal
- function theDate(DateFormat: Integer): String;
- Returns the current date. DateFormats can be:
- - DATE_FORMAL = April 2nd, 2007 Month Day, Year
- - DATE_MONTH = 04/02/07 Month/Day/Year
- - DATE_DAY = 02-04-07 Day-Month-Year
- .. note::
- - by Ron, Nava2 & Narcle
- Example:
- .. code-block:: pascal
- writeln(theDate);
- *)
- function theDate(DateFormat: Integer): string;
- var
- Year, Month, Day: Word;
- D: string;
- Mnths, sfx: TStringArray;
- begin
- DecodeDate(Date(), Year, Month, Day);
- Mnths := ['January', 'February', 'March', 'April', 'May', 'June', 'July',
- 'August', 'September', 'October', 'November', 'December'];
- sfx := ['st', 'nd', 'rd'];
- case DateFormat of
- DATE_FORMAL:
- begin
- if InRange(Day mod 10, 1, 3) and not InRange(Day, 11, 13) then
- D := sfx[(Day mod 10)-1]
- else
- D := 'th';
- Result := Mnths[Month - 1] + ' ' + IntToStr(Day) + D +', ' + IntToStr(Year);
- end;
- DATE_MONTH:
- Result := Padz(IntToStr(Month), 2) + '/' + Padz(IntToStr(Day), 2) + '/' + Copy(IntToStr(Year), 3, 4);
- DATE_DAY:
- Result := Padz(IntToStr(Day), 2) + '-' + Padz(IntToStr(Month), 2) + '-' + Copy(IntToStr(Year), 3, 4);
- end;
- end;
- (*
- waitFunc
- ~~~~~~~~
- .. code-block:: pascal
- function waitFunc(Func: Function: Boolean; WaitPerLoop, MaxTime: Integer; Value: Boolean = true): Boolean;
- Waits for function Func to be true or false (default true). WaitPerLoop is how often you
- want to call "Func" function.
- Example: "waitFunc(@BankScreen, 10 + Random(15), 750);" will check if BankScreen
- is open every 10-25th millisecond, for a maximum of 750 milliseconds.
- Notice the '@'.
- .. note::
- - by Rasta Magician, small edit by EvilChicken!
- - Last Updated: 17 July 2016 by BMWxi
- Example:
- .. code-block:: pascal
- waitFunc(@funcName, 50, 5000);
- *)
- function waitFunc(Func: function: Boolean; WaitPerLoop, MaxTime: Integer; Value: Boolean = True): Boolean;
- var
- T: UInt64;
- begin
- T := GetTickCount64() + MaxTime;
- while (GetTickCount64() < T) do
- begin
- if (Func() = value) then
- begin
- exit(True);
- end;
- wait(WaitPerLoop);
- end;
- end;
- (*
- waitTypeFunc
- ~~~~~~~~~~~~
- .. code-block:: pascal
- function waitTypeFunc(Func: function: boolean of object; WaitPerLoop, MaxTime: Integer; Value: Boolean = True): Boolean;
- Waits for a type function Func to be true or false (default true). WaitPerLoop is how often you
- want to call "Func" function.
- Example: "waitTypeFunc(@bankScreen.isOpen, 10 + Random(15), 750);" will check if bankScreen
- is open every 10-25th millisecond, for a maximum of 750 milliseconds.
- Notice the '@'.
- .. note::
- - by Olly
- - Last Updated: 17 July 2016 by BMWxi
- Example:
- .. code-block:: pascal
- waitTypeFunc(@minimap.isResting, 50, 5000);
- *)
- function waitTypeFunc(Func: function: Boolean of object; WaitPerLoop, MaxTime: Integer; Value: Boolean = True): Boolean;
- var
- T: UInt64;
- begin
- T := GetTickCount64() + MaxTime;
- while (GetTickCount64() < T) do
- begin
- if (Func() = Value) then
- begin
- exit(True);
- end;
- wait(WaitPerLoop);
- end;
- end;
- (*
- TTimeMarker
- ~~~~~~~~~~~
- .. code-block:: pascal
- type TTimeMarker = record
- time, startTime: LongWord;
- paused: Boolean;
- end;
- Timer type which is useful for loops, timing and writing progress reports.
- .. note::
- - by Bart de Boer
- *)
- type
- TTimeMarker = record
- name: string;
- time, startTime, __prevMark: UInt64;
- paused: Boolean;
- end;
- (*
- TTimeMarker.start
- ~~~~~~~~~~~~~~~~~
- .. code-block pascal
- procedure TTimeMarker.start();
- Starts the timer. Can also be used when paused to continue where it left.
- .. note::
- - by Bart de Boer
- Example:
- .. code-block:: pascal
- MyScriptTimer.start();
- *)
- procedure TTimeMarker.start();
- begin
- Self.__prevMark := getTickCount64(); // used for adding time after a pause
- // if it hasn't been started yet
- if (not Self.paused) then
- begin
- Self.startTime := getTickCount64();
- Self.time := 0;
- end;
- Self.paused := False;
- end;
- (*
- TTimeMarker.reset
- ~~~~~~~~~~~~~~~~~
- .. code-block pascal
- procedure TTimeMarker.reset();
- Stops the timer and resets it to zero.
- .. note::
- - by Bart de Boer
- Example:
- .. code-block:: pascal
- MyScriptTimer.reset();
- *)
- procedure TTimeMarker.reset();
- begin
- Self.paused := False;
- Self.time := 0;
- Self.startTime := 0;
- Self.__prevMark := 0;
- end;
- (*
- TTimeMarker.pause
- ~~~~~~~~~~~~~~~~~
- .. code-block pascal
- procedure TTimeMarker.pause();
- Pauses the timer. It can be continued with start().
- .. note::
- - by Bart de Boer
- Example:
- .. code-block:: pascal
- MyScriptTimer.pause();
- TakeABreak(90000);
- MyScriptTimer.start();
- *)
- procedure TTimeMarker.pause();
- begin
- Self.time := Self.time + (getTickCount64() - Self.__prevMark);
- Self.paused := True;
- end;
- (*
- TTimeMarker.getTime
- ~~~~~~~~~~~~~~~~~~~
- .. code-block pascal
- function TTimeMarker.getTime(): LongWord;
- Gets the time from the timer. Returns zero if the timer was not set.
- .. note::
- - by Bart de Boer
- Example:
- .. code-block:: pascal
- MyScriptTimer.start();
- repeat
- DoStuff;
- until(MyScriptTimer.getTime() > 60000);
- *)
- function TTimeMarker.getTime(): LongWord;
- begin
- if not Self.paused then
- Result := Self.time + (getTickCount64() - Self.__prevMark)
- else
- Result := Self.time;
- end;
- (*
- TTimeMarker.getTotalTime
- ~~~~~~~~~~~~~~~~~~~~~~~~
- .. code-block pascal
- function TTimeMarker.getTotalTime(): LongWord;
- Gets the time from the timer including the time it was paused. Returns zero if
- the timer was not set.
- .. note::
- - by Bart de Boer
- Example:
- .. code-block:: pascal
- BreakTime := MyTimer.getTotalTime() - MyTimer.getTime();
- *)
- function TTimeMarker.getTotalTime(): LongWord;
- begin
- if (Self.startTime > 0) then
- Result := getTickCount64() - Self.startTime;
- end;
- (*
- TCountDown
- ~~~~~~~~~~
- .. code-block:: pascal
- type
- TCountDown = type UInt64;
- Timer type which is useful for loops, timing and writing progress reports.
- .. note::
- - by Obscurity
- *)
- type
- TCountDown = type UInt64;
- function TCountDown.setTime(Time: UInt64): UInt64;
- begin
- self := GetTickCount64() + Time;
- Result := self;
- end;
- function TCountDown.timeRemaining(): uInt32;
- begin
- if not self.isFinished() then
- Result := self - GetTickCount64();
- end;
- function TCountDown.isFinished(): Boolean;
- begin
- result := GetTickCount64() >= self;
- end;
- {$f+}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement