Work with date and time. GetTickCount. Limit duration of script work.
Detailed manual on working with the date and time in Adrenaline Bot. Review of typical use cases.
Periodically there are questions about how you can get the current date or time from the script in the Adrenaline bot.
The standard SysUtlis module, available in the bot, contains everything you need, in particular, the TDateTime data type is available.
Immediately, I note that the DateUtils module in the bot is not available, so in doing some operations you will have to dodge a little.
We will analyze the main operations.
1. Get current date / time:
In the script engine bot available standard functions of the language Date, Time, Now, returning the corresponding values such as date \ time.
2. Convert date / time to string and back:
There are methods for converting dates \ framing to a string: DateTimeToStr, DateToStr, TimeToStr, FormatDateTime (read carefully the description of this function, there is a description of available output formats)
To convert a string to a date / time, the following are present: StrToDateTime, StrToDate, StrToTime.
An important point: the date / frame format may differ depending on the user's locale, i.e. in Russia it is dd.mm.yyyy, and in many foreign countries dd / mm / yyyy.
Consider a few examples:
uses SysUtils; begin Print(DateToStr(Date)); // print the current date (01.10.2018) Print(TimeToStr(Time)); // print the current time (0:49:44) Print(DateTimeToStr(Now)); // print the current date and time (01.10.2018 0:49:44) Print(FormatDateTime('hh:nn:ss.zzz', Time)); // print the current time in the format: 00:49:44.579 Print(FormatDateTime('dd mmmm yyyy hh:nn:ss.zzz', Now)); // print the current data and time in the format: 01 Октябрь 2018 00:49:44.579 Print(StrToDateTime('01.10.2018 2:07:31')); Print(DateTimeToStr(StrToDateTime('00:49:44'))); // cast the string to a TDateTime type, and then back to the string, and print end.
3. Typical Date / Time Operations:
Variables of type TDateTime can be compared with each other as well as add / subtract:
uses SysUtils; var past, future, delta: TDateTime; begin past:= Now; // remember the time 1st time Delay(1000); // wait 1 second future:= Now; // remember the time 2nd time // now we have 2 variables, with a difference of 1 second, and we can do something with them if (future > past) then ... // compare their values (in this case the condition will return true) delta:= future - past; // remember the difference between two points in time Print(TimeToStr(delta)); // print this difference (0:00:01) end.
How can all this be applied in practice?
Imagine that there should be at least 10 minutes between two key actions. And between these actions we have to run from point A to point B, and the time spent on the route is not precisely known in advance (about 3-7 minutes). We want to make a smart check so that our bot does not try to do the second action until it takes 10 minutes from the time of the 1st.
uses SysUtils; var Timer: TDateTime; begin // perform the first action Timer:= Now + StrToDateTime('00:10:00'); // add to the current time 10 minutes and remember this time // Engine.MoveTo(...); // Engine.MoveTo(...); // Engine.MoveTo(...); while (Now < Timer) do Delay(500); // wait until it takes 10 minutes from the moment of the 1st action // perform the second action end.
A primitive example of the time limit of the script. This test is not very reliable, because checks the user's PC time, and he can change it. If you are developing scripts, and you want to implement a licensing system by keys, you can go here: Verify.su (substantial discounts are available for Adrenaline scripters).
uses SysUtils; begin if (Now > StrToDateTime('30.09.2018')) then begin Print('The script has run out!'); Script.Stop; end; // code ... end.
In addition, the function
function GetTickCount: Cardinal; - returns the number of milliseconds elapsed since the system startup moments.
It can also be used as timers or, for example, to detect the execution time of some code segment.
uses SysUtils; var T: Cardinal; begin T:= GetTickCount(); // remember the moment in time // code ... Print(GetTickCount() - T); // print the number of ms spent on the execution of a piece of code T:= GetTickCount(); // remember the moment in time while (GetTickCount() < T+10*60*1000) do Delay(500); // wait until 10 minutes have passed end.