Skip to content

Commit

Permalink
rename ConvertTime to MilisecondsToTime`
Browse files Browse the repository at this point in the history
  • Loading branch information
ollydev committed Mar 1, 2024
1 parent 6ffa4a1 commit 3c522fe
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 61 deletions.
40 changes: 23 additions & 17 deletions Source/script/imports/simba.import_timing.pas
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,29 @@ procedure _LapePreciseSleep(const Params: PParamArray); LAPE_WRAPPER_CALLING_CON
end;

(*
ConvertTime
-----------
> procedure ConvertTime(Time: Integer; var h, m, s: Integer);
MillisecondsToTime
------------------
> function MillisecondsToTime(Time: UInt64; out Days, Hours, Mins, Secs: Integer): Integer;
Converts time (in milliseconds) to days,hours,mins and seconds.
Any remaining milliseconds are returned in the Result.
*)
procedure _LapeConvertTime(const Params: PParamArray); LAPE_WRAPPER_CALLING_CONV
procedure _LapeMillisecondsToTime1(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
ConvertTime(PInteger(Params^[0])^, PInteger(Params^[1])^, PInteger(Params^[2])^, PInteger(Params^[3])^);
PInteger(Result)^ := MillisecondsToTime(PUInt64(Params^[0])^, PInteger(Params^[1])^, PInteger(Params^[2])^, PInteger(Params^[3])^, PInteger(Params^[4])^);
end;

(*
ConvertTime64
-------------
> procedure ConvertTime64(Time: UInt64; var y, m, w, d, h, min, s: Integer);
MillisecondsToTime
------------------
> function MillisecondsToTime(Time: UInt64; out Years, Months, Weeks, Days, Hours, Mins, Secs: Integer): Integer;
Converts time (in milliseconds) to years,months,weeks,days,hours,mins and seconds.
Any remaining milliseconds are returned in the Result.
*)
procedure _LapeConvertTime64(const Params: PParamArray); LAPE_WRAPPER_CALLING_CONV
procedure _LapeMillisecondsToTime2(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
ConvertTime64(PUInt64(Params^[0])^, PInteger(Params^[1])^, PInteger(Params^[2])^, PInteger(Params^[3])^, PInteger(Params^[4])^, PInteger(Params^[5])^, PInteger(Params^[6])^, PInteger(Params^[7])^);
PInteger(Result)^ := MillisecondsToTime(PUInt64(Params^[0])^, PInteger(Params^[1])^, PInteger(Params^[2])^, PInteger(Params^[3])^, PInteger(Params^[4])^, PInteger(Params^[5])^, PInteger(Params^[6])^, PInteger(Params^[7])^);
end;

(*
Expand All @@ -80,7 +86,7 @@ procedure _LapePerformanceTimer(const Params: PParamArray; const Result: Pointer
WriteLn FormatMilliseconds(GetTickCount(), 'YY-MM-DD h:m:s:u');
```
*)
procedure _LapeFormatMilliseconds(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
procedure _LapeFormatMilliseconds1(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
PString(Result)^ := FormatMilliseconds(PDouble(Params^[0])^, PString(Params^[1])^);
end;
Expand All @@ -90,7 +96,7 @@ procedure _LapeFormatMilliseconds(const Params: PParamArray; const Result: Point
------------------
> function FormatMilliseconds(Time: Double; TimeSymbols: Boolean = False): String;
*)
procedure _LapeFormatMillisecondsEx(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
procedure _LapeFormatMilliseconds2(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
PString(Result)^ := FormatMilliseconds(PDouble(Params^[0])^, PBoolean(Params^[1])^);
end;
Expand All @@ -114,7 +120,7 @@ procedure _LapeFormatMillisecondsEx(const Params: PParamArray; const Result: Poi
```
T := GetTickCount();
Sleep(1000);
WriteLn('Should be around -1000 :: ', GetTickCount()-T);
WriteLn('Should be around ~1000 :: ', GetTickCount()-T);
```
Resolution is typically in the range of 10 milliseconds to 16 milliseconds.
Expand All @@ -128,11 +134,11 @@ procedure ImportTiming(Compiler: TSimbaScript_Compiler);
ImportingSection := 'Timing';

addGlobalFunc('procedure PreciseSleep(Milliseconds: UInt32);', @_LapePreciseSleep);
addGlobalFunc('procedure ConvertTime(Time: Integer; var h, m, s: Integer)', @_LapeConvertTime);
addGlobalFunc('procedure ConvertTime64(Time: UInt64; var y, m, w, d, h, min, s: Integer)', @_LapeConvertTime64);
addGlobalFunc('function PerformanceTimer: Double;', @_LapePerformanceTimer);
addGlobalFunc('function FormatMilliseconds(Time: Double; Format: String): String; overload;', @_LapeFormatMilliseconds);
addGlobalFunc('function FormatMilliseconds(Time: Double; TimeSymbols: Boolean = False): String; overload;', @_LapeFormatMillisecondsEx);
addGlobalFunc('function MillisecondsToTime(Time: UInt64; out Days, Hours, Mins, Secs: Integer): Integer; overload', @_LapeMillisecondsToTime1);
addGlobalFunc('function MillisecondsToTime(Time: UInt64; out Years, Months, Weeks, Days, Hours, Mins, Secs: Integer): Integer; overload', @_LapeMillisecondsToTime2);
addGlobalFunc('function FormatMilliseconds(Time: Double; Format: String): String; overload;', @_LapeFormatMilliseconds1);
addGlobalFunc('function FormatMilliseconds(Time: Double; TimeSymbols: Boolean = False): String; overload;', @_LapeFormatMilliseconds2);

addDelayedCode([
'function GetTimeRunning: UInt64;',
Expand Down
89 changes: 46 additions & 43 deletions Source/simba.datetime.pas
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,53 @@ interface
uses
classes, sysutils;

procedure ConvertTime(Time: Int64; var h, m, s: Integer);
procedure ConvertTime64(Time: Int64; var y, m, w, d, h, min, s: Integer);
function MillisecondsToTime(Time: UInt64; out Days, Hours, Mins, Secs: Integer): Integer; overload;
function MillisecondsToTime(Time: UInt64; out Years, Months, Weeks, Days, Hours, Mins, Secs: Integer): Integer; overload;

function FormatMilliseconds(Time: Double; Fmt: String): String; overload;
function FormatMilliseconds(Time: Double; TimeSymbols: Boolean = False): String; overload;

function HighResolutionTime: Double;

implementation

uses
simba.nativeinterface;

function MillisecondsToTime(Time: UInt64; out Days, Hours, Mins, Secs: Integer): Integer;
begin
Days := Time div 86400000; // 1000 * 60 * 60 * 24 (1 day or 24 hours)
Time := Time mod 86400000;
Hours:= Time div 3600000; // 1000 * 60 * 60 (1 hour or 60 minutes)
Time := Time mod 3600000;
Mins := Time div 60000; // 1000 * 60 (1 minute or 60 seconds)
Time := Time mod 60000;
Secs := Time div 1000; // 1000 (1 second)
Time := Time mod 1000;

Result := Time;
end;

function MillisecondsToTime(Time: UInt64; out Years, Months, Weeks, Days, Hours, Mins, Secs: Integer): Integer;
begin
Years := Time div 31536000000; // 1000 * 60 * 60 * 24 * 365 (1 year or 365 days)
Time := Time mod 31536000000;
Months := Time div 2592000000; // 1000 * 60 * 60 * 24 * 30 (1 month or 30 days)
Time := Time mod 2592000000;
Weeks := Time div 604800000; // 1000 * 60 * 60 * 24 * 7 (1 week or 7 days)
Time := Time mod 604800000;
Days := Time div 86400000; // 1000 * 60 * 60 * 24 (1 day or 24 hours)
Time := Time mod 86400000;
Hours := Time div 3600000; // 1000 * 60 * 60 (1 hour or 60 minutes)
Time := Time mod 3600000;
Mins := Time div 60000; // 1000 * 60 (1 minute or 60 seconds)
Time := Time mod 60000;
Secs := Time div 1000; // 1000 (1 second)
Time := Time mod 1000;

Result := Time;
end;

// Author: slacky
// https://pastebin.com/zwue0VCt
function FormatMilliseconds(Time: Double; Fmt: string): String;
Expand Down Expand Up @@ -195,7 +231,7 @@ function FormatMilliseconds(Time: Double; Fmt: string): String;
'Y': begin
while Next() = 'Y' do ;
if fmtYearsNoPad in flags then
Result += IntToStr(Trunc(years))
Result += IntToStr(Trunc(years{%H-}))
else begin
if years < 10 then Result += '0';
Result += IntToStr(Trunc(years));
Expand All @@ -204,7 +240,7 @@ function FormatMilliseconds(Time: Double; Fmt: string): String;
'M': begin
while Next() = 'M' do ;
if fmtMonthsNoPad in flags then
Result += IntToStr(Trunc(months))
Result += IntToStr(Trunc(months{%H-}))
else begin
if months < 10 then Result += '0';
Result += IntToStr(Trunc(months));
Expand All @@ -213,7 +249,7 @@ function FormatMilliseconds(Time: Double; Fmt: string): String;
'W': begin
while Next() = 'W' do ;
if fmtWeeksNoPad in flags then
Result += IntToStr(Trunc(weeks))
Result += IntToStr(Trunc(weeks{%H-}))
else begin
if weeks < 10 then Result += '0';
Result += IntToStr(Trunc(weeks));
Expand All @@ -222,7 +258,7 @@ function FormatMilliseconds(Time: Double; Fmt: string): String;
'D': begin
while Next() = 'D' do ;
if fmtDaysNoPad in flags then
Result += IntToStr(Trunc(days))
Result += IntToStr(Trunc(days{%H-}))
else begin
if days < 10 then Result += '0';
Result += IntToStr(Trunc(days));
Expand All @@ -231,7 +267,7 @@ function FormatMilliseconds(Time: Double; Fmt: string): String;
'h': begin
while Next() = 'h' do ;
if fmtHoursNoPad in flags then
Result += IntToStr(Trunc(hours))
Result += IntToStr(Trunc(hours{%H-}))
else begin
if hours < 10 then Result += '0';
Result += IntToStr(Trunc(hours));
Expand All @@ -240,7 +276,7 @@ function FormatMilliseconds(Time: Double; Fmt: string): String;
'm': begin
while Next() = 'm' do ;
if fmtMinutesNoPad in flags then
Result += IntToStr(Trunc(minutes))
Result += IntToStr(Trunc(minutes{%H-}))
else begin
if minutes < 10 then Result += '0';
Result += IntToStr(Trunc(minutes));
Expand All @@ -249,7 +285,7 @@ function FormatMilliseconds(Time: Double; Fmt: string): String;
's': begin
while Next() = 's' do ;
if fmtSecondsNoPad in flags then
Result += IntToStr(Trunc(sec))
Result += IntToStr(Trunc(sec{%H-}))
else begin
if sec < 10 then Result += '0';
Result += IntToStr(Trunc(sec));
Expand All @@ -258,7 +294,7 @@ function FormatMilliseconds(Time: Double; Fmt: string): String;
'u': begin
while Next() = 'u' do ;
if fmtMillisecondsNoPad in flags then
Result += IntToStr(Trunc(ms))
Result += IntToStr(Trunc(ms{%H-}))
else begin
if ms < 1000 then Result += '0';
if ms < 100 then Result += '0';
Expand Down Expand Up @@ -333,38 +369,5 @@ function HighResolutionTime: Double;
Result := SimbaNativeInterface.HighResolutionTime;
end;

procedure ConvertTime(Time: Int64; var h, m, s: Integer);
var
x: Int64;
begin
x := time;
h := x div (3600000);
x := x mod (3600000);
m := x div (60000);
x := x mod (60000);
s := x div (1000);
end;

procedure ConvertTime64(Time: Int64; var y, m, w, d, h, min, s: Integer);
var
x: Int64;
begin
x := time;
y := x div (31536000000); // 1000 * 60 * 60 * 24 * 365 (1 year or 365 days)
x := x mod (31536000000);
m := x div (2592000000); // 1000 * 60 * 60 * 24 * 30 (1 month or 30 days)
x := x mod (2592000000);
w := x div (604800000); // 1000 * 60 * 60 * 24 * 7 (1 week or 7 days)
x := x mod (604800000);
d := x div (86400000); // 1000 * 60 * 60 * 24 (1 day or 24 hours)
x := x mod (86400000);
h := x div (3600000); // 1000 * 60 * 60 (1 hour or 60 minutes)
x := x mod (3600000);
min := x div (60000); // 1000 * 60 (1 minute or 60 seconds)
x := x mod (60000);
s := x div (1000); // 1000 (1 second)
x := x mod (1000);
end;

end.

25 changes: 25 additions & 0 deletions Tests/millisecondstotime.simba
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{$assertions on}

var
Days,Hours,Mins,Secs,Milli: Integer;
Years,Months,Weeks: Integer;
begin
Milli := MillisecondsToTime((60000*60)*49+61111, Days,Hours,Mins,Secs);

Assert(Days = 2);
Assert(Hours = 1);
Assert(Mins = 1);
Assert(Secs = 1);
Assert(Milli = 111);

Milli := MillisecondsToTime((1000 * 60 * 60 * 24 * 365) + (1000 * 60 * 60 * 24 * 38) + 60001, Years,Months,Weeks,Days,Hours,Mins,Secs);

Assert(Years = 1);
Assert(Months = 1);
Assert(Weeks = 1);
Assert(Days = 1);
Assert(Hours = 0);
Assert(Mins = 1);
Assert(Secs = 0);
Assert(Milli = 1);
end;
2 changes: 1 addition & 1 deletion Third-Party/lape

0 comments on commit 3c522fe

Please sign in to comment.