From 0a222666f09617f77b90351af9e25585a262c58e Mon Sep 17 00:00:00 2001 From: Olly Date: Fri, 5 Jul 2024 02:06:12 +0100 Subject: [PATCH] make datetime methods type methods --- DocGen/docgen.simba | 2 +- Images/property.png | Bin 452 -> 451 bytes Images/property_150.png | Bin 666 -> 661 bytes Images/property_200.png | Bin 820 -> 818 bytes Images/variable.png | Bin 414 -> 418 bytes Images/variable_150.png | Bin 570 -> 586 bytes Images/variable_200.png | Bin 729 -> 726 bytes Source/ide/simba.form_functionlist.pas | 42 +- Source/ide/simba.form_main.lfm | 3164 ++++++++--------- .../script/imports/simba.import_datetime.pas | 617 ++++ Source/script/imports/simba.import_misc.pas | 4 +- Source/script/imports/simba.import_timing.pas | 193 - Source/script/simba.script_compiler.pas | 61 +- Source/script/simba.script_imports.pas | 4 +- Source/simba.datetime.pas | 318 +- Source/simba.openssl.pas | 2 +- Tests/file.simba | 2 +- 17 files changed, 2543 insertions(+), 1866 deletions(-) create mode 100644 Source/script/imports/simba.import_datetime.pas delete mode 100644 Source/script/imports/simba.import_timing.pas diff --git a/DocGen/docgen.simba b/DocGen/docgen.simba index e9ea343eb..bc48ac051 100644 --- a/DocGen/docgen.simba +++ b/DocGen/docgen.simba @@ -46,7 +46,7 @@ begin APIFiles += ['Source\script\imports\simba.import_web.pas', 'Web' ]; APIFiles += ['Source\script\imports\simba.import_file.pas', 'File' ]; APIFiles += ['Source\script\imports\simba.import_encoding.pas', 'Encoding' ]; - APIFiles += ['Source\script\imports\simba.import_timing.pas', 'Timing' ]; + APIFiles += ['Source\script\imports\simba.import_datetime.pas', 'Date & Time' ]; APIFiles += ['Source\script\imports\simba.import_string.pas', 'String' ]; APIFiles += ['Source\script\imports\simba.import_process.pas', 'Process' ]; APIFiles += ['Source\script\imports\simba.import_math.pas', 'Math' ]; diff --git a/Images/property.png b/Images/property.png index 511cf537eace4041e4dfdcb4f0b36cbb2d80be06..63b2138bf68b561b0a566814aaaf556e8584d5e6 100644 GIT binary patch delta 374 zcm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1=*$2B diff --git a/Images/variable_200.png b/Images/variable_200.png index f38de101066398a9bc171dcf36a86fc662f4be8f..15d9d89dde4833ce151e21295f4ec465d7dccc56 100644 GIT binary patch delta 665 zcmV;K0%rZ$1=aL{a^aGi_SbV2=k+R8|K28w2bRy)BHFo*a`15`H1s1H7a_q0pTBdbhdBpSBGQhxt2DF&mlJL0k{HaZLp+rP`k0qZ_Beq&cZ6o#ARL?-7f1MET&X z%YkMCWwVEyu5h)P^C+SPL??VQ1;$M;LKk_*enug*I$kSeapr^b?iwtrpFKKskYg`g zra669k4i3{1QoN27GHH!6{?K*0iuhc)reec%?9RJ%YPm@e&>+?PFF^l$L9MQF=>t? z`bM+*%IFka1FfK~yd|Pv>~oz|a{4bLMY9vp$8eVUEfCob6N@$?&cJVYBf>}-BPCO1 z4|s*KL-n}LN4B^Xk{Ix)ac|8Du7ClxcDQ&F)R-W_P!l}$#0Nc+8%U3za0>BH*dFGy z-p1q^Y6*jA_QItjUj$3cBcgM&z;{wufa`lxedj_wm6g( zc?Yj+i|%V}Fzs?J5Wjfhjo22?AYP@#cc}aJpFg7%NU(bCUarGbILx5i2H+1k%3g7^ zt(~#~DLx-(#BMZ8w1cj!46KO^88ZCukbVKi!V5%huNAz#QU_^g|DH%6sR>OXeWg zYp{dZ2pM@CW+b_R+a{-O6`0u|y*~I9j^|J4dFU(?7>PHqI>klxXj*%{ML!;cyi8kl zO2^n6Bbe{fPk)`$fcGp2hZv}st%wdHI^_3jh~_k|e(!_dZw52wtQK{V)=s`7jEt$^ zUGig~(LmW8;Gt1k+qq97T10f#1)IQ>=|$)&-#A1s3$2dR%4iq^SNt}}1b2t5j&SXT z8?~%Y`F_3d$CIF9HnHY6;Ag)23h~2?u7g%1bg3H+EPv~3(8}@Z}cCq!$G3oB5&a>>_5~#Z^4hnAbRse8?h<=&UKy?-)DQ?TlpC^f!N)sBOO>(?iH}cU#1@(j+D29q{)|$jNb#RT`V|eBWx7 function TDateTime.Create(AYear, AMonth, ADay, AHour, AMinute, ASecond, AMillisecond: Integer): TDateTime; static; +*) +procedure _LapeDateTime_Create1(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV +begin + PDateTime(Result)^ := TDateTime.Create(PInteger(Params^[0])^, PInteger(Params^[1])^, PInteger(Params^[2])^, PInteger(Params^[3])^, PInteger(Params^[4])^, PInteger(Params^[5])^, PInteger(Params^[6])^); +end; + +(* +TDateTime.Create +---------------- +> function TDateTime.Create(AYear, AMonth, ADay: Integer): TDateTime; static; + +Creates just the date part. +*) +procedure _LapeDateTime_Create2(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV +begin + PDateTime(Result)^ := TDateTime.Create(PInteger(Params^[0])^, PInteger(Params^[1])^, PInteger(Params^[2])^); +end; + +(* +TDateTime.Create +---------------- +> function TDateTime.Create(AHour, AMin, ASecond, AMillisecond: Integer): TDateTime; static; + +Creates just the time part. +*) +procedure _LapeDateTime_Create3(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV +begin + PDateTime(Result)^ := TDateTime.Create(PInteger(Params^[0])^, PInteger(Params^[1])^, PInteger(Params^[2])^, PInteger(Params^[3])^); +end; + +(* +TDateTime.CreateFromUnix +------------------------ +> function TDateTime.CreateFromUnix(UnixTime: Int64): TDateTime; static; +*) +procedure _LapeDateTime_CreateFromUnix(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV +begin + PDateTime(Result)^ := TDateTime.CreateFromUnix(PInteger(Params^[0])^); +end; + +(* +TDateTime.CreateFromString +-------------------------- +> function TDateTime.CreateFromString(Value: String): TDateTime; static; +*) +procedure _LapeDateTime_CreateFromString1(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV +begin + PDateTime(Result)^ := TDateTime.CreateFromString(PString(Params^[0])^); +end; + +(* +TDateTime.CreateFromString +-------------------------- +> function TDateTime.CreateFromString(Fmt, Value: String): TDateTime; static; +*) +procedure _LapeDateTime_CreateFromString2(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV +begin + PDateTime(Result)^ := TDateTime.CreateFromString(PString(Params^[0])^, PString(Params^[1])^); +end; + +(* +TDateTime.Now +------------- +> function TDateTime.Now: TDateTime; static; +*) +procedure _LapeDateTime_Now(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV +begin + PDateTime(Result)^ := TDateTime.Now(); +end; + +(* +TDateTime.NowUTC +---------------- +> function TDateTime.NowUTC: TDateTime; static; +*) +procedure _LapeDateTime_NowUTC(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV +begin + PDateTime(Result)^ := TDateTime.NowUTC(); +end; + +(* +TDateTime.ToUnix +---------------- +> function TDateTime.ToUnix(IsUTC: Boolean = True): Int64; +*) +procedure _LapeDateTime_ToUnix(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV +begin + PInt64(Result)^ := PDateTime(Params^[0])^.ToUnix(PBoolean(Params^[1])^); +end; + +(* +TDateTime.ToString +------------------ +> function TDateTime.ToString(Fmt: String): String; +*) +procedure _LapeDateTime_ToString1(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV +begin + PString(Result)^ := PDateTime(Params^[0])^.ToString(PString(Params^[1])^); +end; + +(* +TDateTime.ToString +------------------ +> function TDateTime.ToString: String; +*) +procedure _LapeDateTime_ToString2(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV +begin + PString(Result)^ := PDateTime(Params^[0])^.ToString(); +end; + +(* +TDateTime.AddYears +------------------ +> function TDateTime.AddYears(Amount: Integer = 1): TDateTime; +*) +procedure _LapeDateTime_AddYears(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV +begin + PDateTime(Result)^ := PDateTime(Params^[0])^.AddYears(PInteger(Params^[1])^); +end; + +(* +TDateTime.AddMonths +------------------- +> function TDateTime.AddMonths(Amount: Integer = 1): TDateTime; +*) +procedure _LapeDateTime_AddMonths(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV +begin + PDateTime(Result)^ := PDateTime(Params^[0])^.AddMonths(PInteger(Params^[1])^); +end; + +(* +TDateTime.AddDays +----------------- +> function TDateTime.AddDays(Amount: Integer = 1): TDateTime; +*) +procedure _LapeDateTime_AddDays(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV +begin + PDateTime(Result)^ := PDateTime(Params^[0])^.AddDays(PInteger(Params^[1])^); +end; + +(* +TDateTime.AddHours +------------------ +> function TDateTime.AddHours(Amount: Int64 = 1): TDateTime; +*) +procedure _LapeDateTime_AddHours(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV +begin + PDateTime(Result)^ := PDateTime(Params^[0])^.AddHours(PInteger(Params^[1])^); +end; + +(* +TDateTime.AddMinutes +-------------------- +> function TDateTime.AddMinutes(Amount: Int64 = 1): TDateTime; +*) +procedure _LapeDateTime_AddMinutes(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV +begin + PDateTime(Result)^ := PDateTime(Params^[0])^.AddMinutes(PInteger(Params^[1])^); +end; + +(* +TDateTime.AddSeconds +-------------------- +> function TDateTime.AddSeconds(Amount: Int64 = 1): TDateTime; +*) +procedure _LapeDateTime_AddSeconds(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV +begin + PDateTime(Result)^ := PDateTime(Params^[0])^.AddSeconds(PInteger(Params^[1])^); +end; + +(* +TDateTime.AddMilliseconds +------------------------- +> function TDateTime.AddMilliseconds(Amount: Int64 = 1): TDateTime; +*) +procedure _LapeDateTime_AddMilliseconds(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV +begin + PDateTime(Result)^ := PDateTime(Params^[0])^.AddMilliseconds(PInteger(Params^[1])^); +end; + +(* +TDateTime.YearsBetween +---------------------- +> function TDateTime.YearsBetween(Other: TDateTime): Integer; +*) +procedure _LapeDateTime_YearsBetween(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV +begin + PInteger(Result)^ := PDateTime(Params^[0])^.YearsBetween(PDateTime(Params^[1])^); +end; + +(* +TDateTime.MonthsBetween +----------------------- +> function TDateTime.MonthsBetween(Other: TDateTime): Integer; +*) +procedure _LapeDateTime_MonthsBetween(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV +begin + PInteger(Result)^ := PDateTime(Params^[0])^.MonthsBetween(PDateTime(Params^[1])^); +end; + +(* +TDateTime.WeeksBetween +---------------------- +> function TDateTime.WeeksBetween(Other: TDateTime): Integer; +*) +procedure _LapeDateTime_WeeksBetween(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV +begin + PInteger(Result)^ := PDateTime(Params^[0])^.WeeksBetween(PDateTime(Params^[1])^); +end; + +(* +TDateTime.DaysBetween +--------------------- +> function TDateTime.DaysBetween(Other: TDateTime): Integer; +*) +procedure _LapeDateTime_DaysBetween(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV +begin + PInteger(Result)^ := PDateTime(Params^[0])^.DaysBetween(PDateTime(Params^[1])^); +end; + +(* +TDateTime.HoursBetween +---------------------- +> function TDateTime.HoursBetween(Other: TDateTime): Int64; +*) +procedure _LapeDateTime_HoursBetween(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV +begin + PInt64(Result)^ := PDateTime(Params^[0])^.HoursBetween(PDateTime(Params^[1])^); +end; + +(* +TDateTime.MinutesBetween +------------------------ +> function TDateTime.MinutesBetween(Other: TDateTime): Int64; +*) +procedure _LapeDateTime_MinutesBetween(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV +begin + PInt64(Result)^ := PDateTime(Params^[0])^.MinutesBetween(PDateTime(Params^[1])^); +end; + +(* +TDateTime.SecondsBetween +------------------------ +> function TDateTime.SecondsBetween(Other: TDateTime): Int64; +*) +procedure _LapeDateTime_SecondsBetween(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV +begin + PInt64(Result)^ := PDateTime(Params^[0])^.SecondsBetween(PDateTime(Params^[1])^); +end; + +(* +TDateTime.MilliSecondsBetween +----------------------------- +> function TDateTime.MilliSecondsBetween(Other: TDateTime): Int64; +*) +procedure _LapeDateTime_MilliSecondsBetween(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV +begin + PInt64(Result)^ := PDateTime(Params^[0])^.MilliSecondsBetween(PDateTime(Params^[1])^); +end; + +(* +TDateTime.Date +-------------- +> property TDateTime.Date: TDateTime +> property TDateTime.Date(NewValue: TDateTime) + +Read or write just the date part. +*) +procedure _LapeDateTime_Date_Read(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV +begin + PDateTime(Result)^ := PDateTime(Params^[0])^.Date; +end; + +procedure _LapeDateTime_Date_Write(const Params: PParamArray); LAPE_WRAPPER_CALLING_CONV +begin + PDateTime(Params^[0])^.Date := PDateTime(Params^[1])^; +end; + +(* +TDateTime.Time +-------------- +> property TDateTime.Time: TDateTime +> property TDateTime.Time(NewValue: TDateTime) +*) +procedure _LapeDateTime_Time_Read(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV +begin + PDateTime(Result)^ := PDateTime(Params^[0])^.Time; +end; + +procedure _LapeDateTime_Time_Write(const Params: PParamArray); LAPE_WRAPPER_CALLING_CONV +begin + PDateTime(Params^[0])^.Time := PDateTime(Params^[1])^; +end; + +(* +TDateTime.Year +-------------- +> property TDateTime.Year: Integer +> property TDateTime.Year(NewValue: Integer) +*) +procedure _LapeDateTime_Year_Read(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV +begin + PInteger(Result)^ := PDateTime(Params^[0])^.Year; +end; + +procedure _LapeDateTime_Year_Write(const Params: PParamArray); LAPE_WRAPPER_CALLING_CONV +begin + PDateTime(Params^[0])^.Year := PInteger(Params^[1])^; +end; + +(* +TDateTime.Month +-------------- +> property TDateTime.Month: Integer +> property TDateTime.Month(NewValue: Integer) +*) +procedure _LapeDateTime_Month_Read(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV +begin + PInteger(Result)^ := PDateTime(Params^[0])^.Month; +end; + +procedure _LapeDateTime_Month_Write(const Params: PParamArray); LAPE_WRAPPER_CALLING_CONV +begin + PDateTime(Params^[0])^.Month := PInteger(Params^[1])^; +end; + +(* +TDateTime.Day +-------------- +> property TDateTime.Day: Integer +> property TDateTime.Day(NewValue: Integer) +*) +procedure _LapeDateTime_Day_Read(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV +begin + PInteger(Result)^ := PDateTime(Params^[0])^.Day; +end; + +procedure _LapeDateTime_Day_Write(const Params: PParamArray); LAPE_WRAPPER_CALLING_CONV +begin + PDateTime(Params^[0])^.Day := PInteger(Params^[1])^; +end; + +(* +TDateTime.Hour +-------------- +> property TDateTime.Hour: Integer +> property TDateTime.Hour(NewValue: Integer) +*) +procedure _LapeDateTime_Hour_Read(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV +begin + PInteger(Result)^ := PDateTime(Params^[0])^.Hour; +end; + +procedure _LapeDateTime_Hour_Write(const Params: PParamArray); LAPE_WRAPPER_CALLING_CONV +begin + PDateTime(Params^[0])^.Hour := PInteger(Params^[1])^; +end; + +(* +TDateTime.Minute +---------------- +> property TDateTime.Minute: Integer +> property TDateTime.Minute(NewValue: Integer) +*) +procedure _LapeDateTime_Minute_Read(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV +begin + PInteger(Result)^ := PDateTime(Params^[0])^.Minute; +end; + +procedure _LapeDateTime_Minute_Write(const Params: PParamArray); LAPE_WRAPPER_CALLING_CONV +begin + PDateTime(Params^[0])^.Minute := PInteger(Params^[1])^; +end; + +(* +TDateTime.Second +---------------- +> property TDateTime.Second: Integer +> property TDateTime.Second(NewValue: Integer) +*) +procedure _LapeDateTime_Second_Read(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV +begin + PInteger(Result)^ := PDateTime(Params^[0])^.Second; +end; + +procedure _LapeDateTime_Second_Write(const Params: PParamArray); LAPE_WRAPPER_CALLING_CONV +begin + PDateTime(Params^[0])^.Second := PInteger(Params^[1])^; +end; + +(* +TDateTime.Millisecond +--------------------- +> property TDateTime.Millisecond: Integer +> property TDateTime.Millisecond(NewValue: Integer) +*) +procedure _LapeDateTime_Millisecond_Read(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV +begin + PInteger(Result)^ := PDateTime(Params^[0])^.Millisecond; +end; + +procedure _LapeDateTime_Millisecond_Write(const Params: PParamArray); LAPE_WRAPPER_CALLING_CONV +begin + PDateTime(Params^[0])^.Millisecond := PInteger(Params^[1])^; +end; + +(* +UnixTime +-------- +> function UnixTime: Int64; +*) +procedure _LapeUnixTime(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV +begin + PInt64(Result)^ := DateTimeToUnix(Now(), False); +end; + +(* +LocalTimeOffset +--------------- +> function LocalTimeOffset: Integer; +*) +procedure _LapeLocalTimeOffset(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV +begin + PInteger(Result)^ := GetLocalTimeOffset(); +end; + + +(* +PreciseSleep +------------ +> procedure PreciseSleep(Milliseconds: UInt32); +*) +procedure _LapePreciseSleep(const Params: PParamArray); LAPE_WRAPPER_CALLING_CONV +begin + SimbaNativeInterface.PreciseSleep(PUInt32(Params^[0])^); +end; + +(* +MillisecondsToTime +------------------ +> function MillisecondsToTime(Time: UInt64; out Days, Hours, Mins, Secs: Integer): Integer; +*) +procedure _LapeMillisecondsToTime1(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV +begin + PInteger(Result)^ := MillisecondsToTime(PUInt64(Params^[0])^, PInteger(Params^[1])^, PInteger(Params^[2])^, PInteger(Params^[3])^, PInteger(Params^[4])^); +end; + +(* +MillisecondsToTime +------------------ +> function MillisecondsToTime(Time: UInt64; out Years, Months, Weeks, Days, Hours, Mins, Secs: Integer): Integer; +*) +procedure _LapeMillisecondsToTime2(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV +begin + 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; + +(* +PerformanceTimer +---------------- +> function PerformanceTimer: Double; +*) +procedure _LapePerformanceTimer(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV +begin + PDouble(Result)^ := SimbaNativeInterface.HighResolutionTime(); +end; + +(* +FormatMilliseconds +------------------ +> function FormatMilliseconds(Time: Double; Format: String): String; +*) +procedure _LapeFormatMilliseconds1(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV +begin + PString(Result)^ := FormatMilliseconds(PDouble(Params^[0])^, PString(Params^[1])^); +end; + +(* +FormatMilliseconds +------------------ +> function FormatMilliseconds(Time: Double; TimeSymbols: Boolean = False): String; +*) +procedure _LapeFormatMilliseconds2(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV +begin + PString(Result)^ := FormatMilliseconds(PDouble(Params^[0])^, PBoolean(Params^[1])^); +end; + +procedure ImportDateTime(Compiler: TSimbaScript_Compiler); +begin + with Compiler do + begin + ImportingSection := 'Date & Time'; + + addGlobalFunc('function TDateTime.Create(AYear, AMonth, ADay, AHour, AMinute, ASecond, AMillisecond: Integer): TDateTime; static; overload', @_LapeDateTime_Create1); + addGlobalFunc('function TDateTime.Create(AYear, AMonth, ADay: Integer): TDateTime; static; overload', @_LapeDateTime_Create2); + addGlobalFunc('function TDateTime.Create(AHour, AMin, ASecond, AMillisecond: Integer): TDateTime; static; overload', @_LapeDateTime_Create3); + addGlobalFunc('function TDateTime.CreateFromUnix(UnixTime: Int64): TDateTime; static;', @_LapeDateTime_CreateFromUnix); + addGlobalFunc('function TDateTime.CreateFromString(Value: String): TDateTime; static; overload;', @_LapeDateTime_CreateFromString1); + addGlobalFunc('function TDateTime.CreateFromString(Fmt, Value: String): TDateTime; static; overload;', @_LapeDateTime_CreateFromString2); + + addGlobalFunc('function TDateTime.Now: TDateTime; static', @_LapeDateTime_Now); + addGlobalFunc('function TDateTime.NowUTC: TDateTime; static', @_LapeDateTime_NowUTC); + + addGlobalFunc('function TDateTime.ToUnix(IsUTC: Boolean = True): Int64', @_LapeDateTime_ToUnix); + addGlobalFunc('function TDateTime.ToString(Fmt: String): String; overload', @_LapeDateTime_ToString1); + addGlobalFunc('function TDateTime.ToString: String; overload', @_LapeDateTime_ToString2); + + addGlobalFunc('function TDateTime.AddYears(Amount: Integer = 1): TDateTime;', @_LapeDateTime_AddYears); + addGlobalFunc('function TDateTime.AddMonths(Amount: Integer = 1): TDateTime;', @_LapeDateTime_AddMonths); + addGlobalFunc('function TDateTime.AddDays(Amount: Integer = 1): TDateTime;', @_LapeDateTime_AddDays); + addGlobalFunc('function TDateTime.AddHours(Amount: Int64 = 1): TDateTime;', @_LapeDateTime_AddHours); + addGlobalFunc('function TDateTime.AddMinutes(Amount: Int64 = 1): TDateTime;', @_LapeDateTime_AddMinutes); + addGlobalFunc('function TDateTime.AddSeconds(Amount: Int64 = 1): TDateTime;', @_LapeDateTime_AddSeconds); + addGlobalFunc('function TDateTime.AddMilliseconds(Amount: Int64 = 1): TDateTime;', @_LapeDateTime_AddMilliseconds); + + addGlobalFunc('function TDateTime.YearsBetween(Other: TDateTime): Integer;', @_LapeDateTime_YearsBetween); + addGlobalFunc('function TDateTime.MonthsBetween(Other: TDateTime): Integer;', @_LapeDateTime_MonthsBetween); + addGlobalFunc('function TDateTime.WeeksBetween(Other: TDateTime): Integer;', @_LapeDateTime_WeeksBetween); + addGlobalFunc('function TDateTime.DaysBetween(Other: TDateTime): Integer;', @_LapeDateTime_DaysBetween); + addGlobalFunc('function TDateTime.HoursBetween(Other: TDateTime): Int64;', @_LapeDateTime_HoursBetween); + addGlobalFunc('function TDateTime.MinutesBetween(Other: TDateTime): Int64;', @_LapeDateTime_MinutesBetween); + addGlobalFunc('function TDateTime.SecondsBetween(Other: TDateTime): Int64;', @_LapeDateTime_SecondsBetween); + addGlobalFunc('function TDateTime.MilliSecondsBetween(Other: TDateTime): Int64;', @_LapeDateTime_MilliSecondsBetween); + + addProperty('TDateTime', 'Date', 'TDateTime', @_LapeDateTime_Date_Read, @_LapeDateTime_Date_Write); + addProperty('TDateTime', 'Time', 'TDateTime', @_LapeDateTime_Time_Read, @_LapeDateTime_Time_Write); + addProperty('TDateTime', 'Year', 'Integer', @_LapeDateTime_Year_Read, @_LapeDateTime_Year_Write); + addProperty('TDateTime', 'Month', 'Integer', @_LapeDateTime_Month_Read, @_LapeDateTime_Month_Write); + addProperty('TDateTime', 'Day', 'Integer', @_LapeDateTime_Day_Read, @_LapeDateTime_Day_Write); + addProperty('TDateTime', 'Hour', 'Integer', @_LapeDateTime_Hour_Read, @_LapeDateTime_Hour_Write); + addProperty('TDateTime', 'Minute', 'Integer', @_LapeDateTime_Minute_Read, @_LapeDateTime_Minute_Write); + addProperty('TDateTime', 'Second', 'Integer', @_LapeDateTime_Second_Read, @_LapeDateTime_Second_Write); + addProperty('TDateTime', 'Millisecond', 'Integer', @_LapeDateTime_Millisecond_Read, @_LapeDateTime_Millisecond_Write); + + addGlobalFunc('function UnixTime: Int64;', @_LapeUnixTime); + addGlobalFunc('function LocalTimeOffset: Integer', @_LapeLocalTimeOffset); + + addGlobalFunc('procedure PreciseSleep(Milliseconds: UInt32);', @_LapePreciseSleep); + addGlobalFunc('function PerformanceTimer: Double;', @_LapePerformanceTimer); + 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([ + 'type', + ' TStopwatch = record', + ' Name: String;', + ' StartTime: Double;', + ' StopTime: Double;', + ' end;', + '', + 'procedure TStopwatch.Start(AName: String = "");', + 'begin', + ' Self.Name := AName;', + ' Self.StartTime := PerformanceTimer();', + 'end;', + '', + 'procedure TStopwatch.Stop;', + 'begin', + ' Self.StopTime := PerformanceTimer();', + 'end;', + '', + 'function TStopwatch.Elapsed: Double;', + 'begin', + ' if (Self.StopTime > 0) then', + ' Result := Self.StopTime - Self.StartTime', + ' else', + ' Result := PerformanceTimer() - Self.StartTime;', + 'end;', + '', + 'function TStopwatch.ElapsedFmt(Format: String = "u"): String;', + 'begin', + ' Result := FormatMilliseconds(Self.Elapsed(), Format);', + 'end;', + '', + 'procedure TStopwatch.Reset;', + 'begin', + ' Self.StopTime := Self.StartTime := 0;', + 'end;' + ]); + + ImportingSection := ''; + end; +end; + +end. + + diff --git a/Source/script/imports/simba.import_misc.pas b/Source/script/imports/simba.import_misc.pas index 22f795b8d..0de4ed8e7 100644 --- a/Source/script/imports/simba.import_misc.pas +++ b/Source/script/imports/simba.import_misc.pas @@ -530,7 +530,7 @@ procedure ImportMisc(Compiler: TSimbaScript_Compiler); 'begin', ' with TImage.CreateFromTarget() do', ' try', - ' FileName := SimbaEnv.ScreenshotsPath + PathExtractNameWithoutExt(SCRIPT_FILE) + #32 + FormatDateTime("dd-mm hh-mm-ss", Now()) + ".png";', + ' FileName := SimbaEnv.ScreenshotsPath + PathExtractNameWithoutExt(SCRIPT_FILE) + #32 + TDateTime.Now().ToString("dd-mm hh-mm-ss") + ".png";', ' if Save(FileName) then', ' Exit(FileName);', '', @@ -538,7 +538,7 @@ procedure ImportMisc(Compiler: TSimbaScript_Compiler); ' T := GetTickCount() + 1000;', ' while (GetTickCount() < T) do', ' begin', - ' FileName := SimbaEnv.ScreenshotsPath + PathExtractNameWithoutExt(SCRIPT_FILE) + #32 + FormatDateTime("dd-mm hh-mm-ss-zzz", Now()) + ".png";', + ' FileName := SimbaEnv.ScreenshotsPath + PathExtractNameWithoutExt(SCRIPT_FILE) + #32 + TDateTime.Now().ToString("dd-mm hh-mm-ss-zzz") + ".png";', ' if Save(FileName) then', ' Exit(FileName);', '', diff --git a/Source/script/imports/simba.import_timing.pas b/Source/script/imports/simba.import_timing.pas deleted file mode 100644 index a31fca4e5..000000000 --- a/Source/script/imports/simba.import_timing.pas +++ /dev/null @@ -1,193 +0,0 @@ -unit simba.import_timing; - -{$i simba.inc} - -interface - -uses - Classes, SysUtils, - simba.base, simba.script_compiler; - -procedure ImportTiming(Compiler: TSimbaScript_Compiler); - -implementation - -uses - lptypes, - simba.datetime, simba.nativeinterface; - -(* -Timing -====== -Timing -*) - -(* -Sleep ------ -> procedure Sleep(MilliSeconds: UInt32); -*) - -(* -PreciseSleep ------------- -> procedure PreciseSleep(Milliseconds: UInt32); - -Much more accurate sleep method, if you need millisecond accurate sleeps under -50ms use this. - -```{note} - This is only useful on Windows since on Linux and MacOS the regular `Sleep` is accurate to the milliseconds. -``` -*) -procedure _LapePreciseSleep(const Params: PParamArray); LAPE_WRAPPER_CALLING_CONV -begin - SimbaNativeInterface.PreciseSleep(PUInt32(Params^[0])^); -end; - -(* -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 _LapeMillisecondsToTime1(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV -begin - PInteger(Result)^ := MillisecondsToTime(PUInt64(Params^[0])^, PInteger(Params^[1])^, PInteger(Params^[2])^, PInteger(Params^[3])^, PInteger(Params^[4])^); -end; - -(* -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 _LapeMillisecondsToTime2(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV -begin - 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; - -(* -PerformanceTimer ----------------- -> function PerformanceTimer: Double; -*) -procedure _LapePerformanceTimer(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV -begin - PDouble(Result)^ := SimbaNativeInterface.HighResolutionTime(); -end; - -(* -FormatMilliseconds ------------------- -> function FormatMilliseconds(Time: Double; Format: String): String; - -Formats milliseconds into a string. Formatting is defined by the `Format` string. - -``` -WriteLn FormatMilliseconds(GetTickCount(), 's\s\e\c, m\m\i\n'); -WriteLn FormatMilliseconds(GetTickCount(), 'hh\h:mm\m:ss\s:uu\m\s'); -WriteLn FormatMilliseconds(GetTickCount(), 'YY-MM-DD h:m:s:u'); -``` -*) -procedure _LapeFormatMilliseconds1(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV -begin - PString(Result)^ := FormatMilliseconds(PDouble(Params^[0])^, PString(Params^[1])^); -end; - -(* -FormatMilliseconds ------------------- -> function FormatMilliseconds(Time: Double; TimeSymbols: Boolean = False): String; -*) -procedure _LapeFormatMilliseconds2(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV -begin - PString(Result)^ := FormatMilliseconds(PDouble(Params^[0])^, PBoolean(Params^[1])^); -end; - -(* -GetTimeRunning --------------- -> function GetTimeRunning: UInt64; - -Returns the current script runtime in milliseconds. -*) - -(* -GetTickCount ------------- -> function GetTickCount: UInt64; - -Returns the number of milliseconds that have elapsed since the system was started. -However the more important use case of this function is for measuring time: - -``` - T := GetTickCount(); - Sleep(1000); - WriteLn('Should be around ~1000 :: ', GetTickCount()-T); -``` -Resolution is typically in the range of 10 milliseconds to 16 milliseconds. - -If you need a more accurate timer use `PerformanceTimer` -*) - -procedure ImportTiming(Compiler: TSimbaScript_Compiler); -begin - with Compiler do - begin - ImportingSection := 'Timing'; - - addGlobalFunc('procedure PreciseSleep(Milliseconds: UInt32);', @_LapePreciseSleep); - addGlobalFunc('function PerformanceTimer: Double;', @_LapePerformanceTimer); - 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([ - 'type', - ' TStopwatch = record', - ' Name: String;', - ' StartTime: Double;', - ' StopTime: Double;', - ' end;', - '', - 'procedure TStopwatch.Start(AName: String = "");', - 'begin', - ' Self.Name := AName;', - ' Self.StartTime := PerformanceTimer();', - 'end;', - '', - 'procedure TStopwatch.Stop;', - 'begin', - ' Self.StopTime := PerformanceTimer();', - 'end;', - '', - 'function TStopwatch.Elapsed: Double;', - 'begin', - ' if (Self.StopTime > 0) then', - ' Result := Self.StopTime - Self.StartTime', - ' else', - ' Result := PerformanceTimer() - Self.StartTime;', - 'end;', - '', - 'function TStopwatch.ElapsedFmt(Format: String = "u"): String;', - 'begin', - ' Result := FormatMilliseconds(Self.Elapsed(), Format);', - 'end;', - '', - 'procedure TStopwatch.Reset;', - 'begin', - ' Self.StopTime := Self.StartTime := 0;', - 'end;' - ]); - - ImportingSection := ''; - end; -end; - -end. - diff --git a/Source/script/simba.script_compiler.pas b/Source/script/simba.script_compiler.pas index 2168194b5..854623339 100644 --- a/Source/script/simba.script_compiler.pas +++ b/Source/script/simba.script_compiler.pas @@ -352,31 +352,13 @@ procedure TSimbaScript_Compiler.InitBaseString; ImportingSection := ''; end; -// lpeval_import_datetime but moved sleep & gettickcount to timing section +// Import our own methods later (import_datetime.pas) procedure TSimbaScript_Compiler.InitBaseDateTime; begin - ImportingSection := 'DateTime'; - - addGlobalVar(HoursPerDay, 'HoursPerDay').isConstant := True; - addGlobalVar(MinsPerHour, 'MinsPerHour').isConstant := True; - addGlobalVar(SecsPerMin, 'SecsPerMin').isConstant := True; - addGlobalVar(MSecsPerSec, 'MSecsPerSec').isConstant := True; - addGlobalVar(MinsPerDay, 'MinsPerDay').isConstant := True; - addGlobalVar(SecsPerDay, 'SecsPerDay').isConstant := True; - addGlobalVar(MSecsPerDay, 'MSecsPerDay').isConstant := True; + ImportingSection := 'Date & Time'; addGlobalType(getBaseType(ltDouble).createCopy(True), 'TDateTime', False); - addGlobalFunc('function EncodeDate(Year, Month, Day: UInt16): TDateTime;', @_LapeEncodeDate); - addGlobalFunc('function EncodeTime(Hour, Min, Sec, MSec: UInt16): TDateTime;', @_LapeEncodeTime); - addGlobalFunc('procedure DecodeDate(DateTime: TDateTime; var Year, Month, Day: UInt16);', @_LapeDecodeDate); - addGlobalFunc('function DecodeDateFully(DateTime: TDateTime; var Year, Month, Day, DOW: UInt16): Boolean;', @_LapeDecodeDateFully); - addGlobalFunc('procedure DecodeTime(DateTime: TDateTime; var Hour, Min, Sec, MSec: UInt16);', @_LapeDecodeTime); - - addGlobalFunc('function DateTimeToStr(const DateTime: TDateTime): string;', @_LapeDateTimeToStr); - addGlobalFunc('function DateToStr(const DateTime: TDateTime): string;', @_LapeDateToStr); - addGlobalFunc('function TimeToStr(const DateTime: TDateTime): string;', @_LapeTimeToStr); - TLapeType_OverloadedMethod(Globals['ToString'].VarType).addMethod( TLapeType_Method(addManagedType( TLapeType_Method.Create( @@ -389,45 +371,6 @@ procedure TSimbaScript_Compiler.InitBaseDateTime; )).NewGlobalVar(@_LapeDateTimeToStr) ); - addGlobalFunc('function Date: TDateTime;', @_LapeDate); - addGlobalFunc('function Time: TDateTime;', @_LapeTime); - addGlobalFunc('function Now: TDateTime;', @_LapeNow); - addGlobalFunc('function NowUTC: TDateTime;', @_LapeNowUTC); - - addGlobalFunc('procedure ReplaceTime(var DateTime: TDateTime; NewTime: TDateTime);', @_LapeReplaceTime); - addGlobalFunc('procedure ReplaceDate(var DateTime: TDateTime; NewDate: TDateTime);', @_LapeReplaceDate); - - addGlobalFunc('function FormatDateTime(Format: string; DateTime: TDateTime): string;', @_LapeFormatDateTime); - addGlobalFunc('function StrToDate(s: string): TDateTime; overload;', @_LapeStrToDate); - addGlobalFunc('function StrToDate(s: string; Default: TDateTime): TDateTime; overload;', @_LapeStrToDateDef); - addGlobalFunc('function StrToTime(s: string): TDateTime; overload;', @_LapeStrToTime); - addGlobalFunc('function StrToTime(s: string; Default: TDateTime): TDateTime; overload;', @_LapeStrToTimeDef); - addGlobalFunc('function StrToDateTime(s: string): TDateTime; overload;', @_LapeStrToDateTime); - addGlobalFunc('function StrToDateTime(s: string; Default: TDateTime): TDateTime; overload;', @_LapeStrToDateTimeDef); - - addGlobalFunc('function DateTimeToUnix(const Value: TDateTime; InputIsUTC: Boolean = True): Int64;', @_LapeDateTimeToUnix); - addGlobalFunc('function UnixToDateTime(const Value: Int64; ReturnUTC: Boolean = True): TDateTime;', @_LapeUnixToDateTime); - addGlobalFunc('function UnixTime: Int64;', @_LapeUnixTime); - - addGlobalFunc('function YearsBetween(const ANow, AThen: TDateTime): Int32;', @_LapeYearsBetween); - addGlobalFunc('function MonthsBetween(const ANow, AThen: TDateTime): Int32;', @_LapeMonthsBetween); - addGlobalFunc('function WeeksBetween(const ANow, AThen: TDateTime): Int32;', @_LapeWeeksBetween); - addGlobalFunc('function DaysBetween(const ANow, AThen: TDateTime): Int32;', @_LapeDaysBetween); - addGlobalFunc('function HoursBetween(const ANow, AThen: TDateTime): Int64;', @_LapeHoursBetween); - addGlobalFunc('function MinutesBetween(const ANow, AThen: TDateTime): Int64;', @_LapeMinutesBetween); - addGlobalFunc('function SecondsBetween(const ANow, AThen: TDateTime): Int64;', @_LapeSecondsBetween); - addGlobalFunc('function MilliSecondsBetween(const ANow, AThen: TDateTime): Int64;', @_LapeMilliSecondsBetween); - - addGlobalFunc('function IncYear(const Value: TDateTime; const NumberOfYears: Int32 = 1): TDateTime;', @_LapeIncYear); - addGlobalFunc('function IncWeek(const Value: TDateTime; const NumberOfWeeks: Int32 = 1): TDateTime;', @_LapeIncWeek); - addGlobalFunc('function IncDay(const Value: TDateTime; const NumberOfDays: Int32 = 1): TDateTime;', @_LapeIncDay); - addGlobalFunc('function IncHour(const Value: TDateTime; const NumberOfHours: Int64 = 1): TDateTime;', @_LapeIncHour); - addGlobalFunc('function IncMinute(const Value: TDateTime; const NumberOfMinutes: Int64 = 1): TDateTime;', @_LapeIncMinute); - addGlobalFunc('function IncSecond(const Value: TDateTime; const NumberOfSeconds: Int64 = 1): TDateTime;', @_LapeIncSecond); - addGlobalFunc('function IncMilliSecond(const Value: TDateTime; const NumberOfMilliSeconds: Int64 = 1): TDateTime;', @_LapeIncMilliSecond); - - ImportingSection := 'Timing'; - addGlobalFunc('function GetTickCount: UInt64;', @_LapeGetTickCount); addGlobalFunc('procedure Sleep(MilliSeconds: UInt32);', @_LapeSleep); diff --git a/Source/script/simba.script_imports.pas b/Source/script/simba.script_imports.pas index 53c98e9ab..e5a29b765 100644 --- a/Source/script/simba.script_imports.pas +++ b/Source/script/simba.script_imports.pas @@ -20,7 +20,7 @@ implementation // Simba simba.import_base, simba.import_colormath,simba.import_matrix, simba.import_windowhandle, simba.import_quad, simba.import_box, simba.import_boxarray, simba.import_point, - simba.import_circle, simba.import_timing, simba.import_tpa, simba.import_atpa, + simba.import_circle, simba.import_datetime, simba.import_tpa, simba.import_atpa, simba.import_encoding, simba.import_file, simba.import_process, simba.import_internal, simba.import_target, simba.import_math, simba.import_misc, simba.import_slacktree, simba.import_string, simba.import_random, simba.import_debugimage, simba.import_web, simba.import_threading, @@ -62,7 +62,7 @@ procedure AddSimbaImports(Compiler: TSimbaScript_Compiler); ImportMatchTemplate(Compiler); ImportJSON(Compiler); - ImportTiming(Compiler); + ImportDateTime(Compiler); ImportTPA(Compiler); ImportATPA(Compiler); ImportEncoding(Compiler); diff --git a/Source/simba.datetime.pas b/Source/simba.datetime.pas index bb6be09a9..e659ba894 100644 --- a/Source/simba.datetime.pas +++ b/Source/simba.datetime.pas @@ -2,8 +2,6 @@ Author: Raymond van Venetiƫ and Merlijn Wajer Project: Simba (https://github.com/MerlijnWajer/Simba) License: GNU General Public License (https://www.gnu.org/licenses/gpl-3.0) - - Timing (in milliseconds) functions. } unit simba.datetime; @@ -12,7 +10,74 @@ interface uses - classes, sysutils; + Classes, SysUtils, + simba.base; + +type + TDateTimeHelper = type helper for TDateTime + private + function GetDate: TDateTime; + function GetTime: TDateTime; + function GetDay: Integer; + function GetHour: Integer; + function GetMillisecond: Integer; + function GetMinute: Integer; + function GetMonth: Integer; + function GetSecond: Integer; + function GetYear: Integer; + + procedure SetDate(AValue: TDateTime); + procedure SetTime(AValue: TDateTime); + procedure SetDay(AValue: Integer); + procedure SetHour(AValue: Integer); + procedure SetMillisecond(AValue: Integer); + procedure SetMinute(AValue: Integer); + procedure SetMonth(AValue: Integer); + procedure SetSecond(AValue: Integer); + procedure SetYear(AValue: Integer); + public + class function Create(AYear, AMonth, ADay, AHour, AMinute, ASecond, AMillisecond: Integer): TDateTime; static; overload; + class function Create(AYear, AMonth, ADay: Integer): TDateTime; static; overload; + class function Create(AHour, AMin, ASecond, AMillisecond: Integer): TDateTime; static; overload; + class function CreateFromUnix(UnixTime: Int64): TDateTime; static; + class function CreateFromString(Value: String): TDateTime; static; overload; + class function CreateFromString(Fmt, Value: String): TDateTime; static; overload; + + class function Now: TDateTime; static; + class function NowUTC: TDateTime; static; + + function ToUnix(IsUTC: Boolean = True): Int64; + function ToString(Fmt: String): String; + function ToString: String; + + function AddYears(Amount: Integer = 1): TDateTime; + function AddMonths(Amount: Integer = 1): TDateTime; + function AddDays(Amount: Integer = 1): TDateTime; + function AddHours(Amount: Int64 = 1): TDateTime; + function AddMinutes(Amount: Int64 = 1): TDateTime; + function AddSeconds(Amount: Int64 = 1): TDateTime; + function AddMilliseconds(Amount: Int64 = 1): TDateTime; + + function YearsBetween(Other: TDateTime): Integer; + function MonthsBetween(Other: TDateTime): Integer; + function WeeksBetween(Other: TDateTime): Integer; + function DaysBetween(Other: TDateTime): Integer; + function HoursBetween(Other: TDateTime): Int64; + function MinutesBetween(Other: TDateTime): Int64; + function SecondsBetween(Other: TDateTime): Int64; + function MilliSecondsBetween(Other: TDateTime): Int64; + + property Date: TDateTime read GetDate write SetDate; + property Time: TDateTime read GetTime write SetTime; + + property Year: Integer read GetYear write SetYear; + property Month: Integer read GetMonth write SetMonth; + property Day: Integer read GetDay write SetDay; + property Hour: Integer read GetHour write SetHour; + property Minute: Integer read GetMinute write SetMinute; + property Second: Integer read GetSecond write SetSecond; + property Millisecond: Integer read GetMillisecond write SetMillisecond; + end; 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; @@ -25,8 +90,239 @@ function HighResolutionTime: Double; implementation uses + DateUtils, simba.nativeinterface; +function TDateTimeHelper.GetDate: TDateTime; +begin + Result := DateOf(Self); +end; + +function TDateTimeHelper.GetDay: Integer; +begin + Result := DayOf(Self); +end; + +function TDateTimeHelper.GetHour: Integer; +begin + Result := HourOf(Self); +end; + +function TDateTimeHelper.GetMillisecond: Integer; +begin + Result := MilliSecondOf(Self); +end; + +function TDateTimeHelper.GetMinute: Integer; +begin + Result := MinuteOf(Self); +end; + +function TDateTimeHelper.GetMonth: Integer; +begin + Result := MonthOf(Self); +end; + +function TDateTimeHelper.GetSecond: Integer; +begin + Result := SecondOf(Self); +end; + +function TDateTimeHelper.GetTime: TDateTime; +begin + Result := TimeOf(Self); +end; + +function TDateTimeHelper.GetYear: Integer; +begin + Result := YearOf(Self); +end; + +procedure TDateTimeHelper.SetDate(AValue: TDateTime); +begin + ReplaceDate(Self, AValue); +end; + +procedure TDateTimeHelper.SetTime(AValue: TDateTime); +begin + ReplaceTime(Self, AValue); +end; + +procedure TDateTimeHelper.SetDay(AValue: Integer); +begin + Self := RecodeDay(Self, AValue); +end; + +procedure TDateTimeHelper.SetHour(AValue: Integer); +begin + Self := RecodeHour(Self, AValue); +end; + +procedure TDateTimeHelper.SetMillisecond(AValue: Integer); +begin + Self := RecodeMilliSecond(Self, AValue); +end; + +procedure TDateTimeHelper.SetMinute(AValue: Integer); +begin + Self := RecodeMinute(Self, AValue); +end; + +procedure TDateTimeHelper.SetMonth(AValue: Integer); +begin + Self := RecodeMonth(Self, AValue); +end; + +procedure TDateTimeHelper.SetSecond(AValue: Integer); +begin + Self := RecodeSecond(Self, AValue); +end; + +procedure TDateTimeHelper.SetYear(AValue: Integer); +begin + Self := RecodeYear(Self, AValue); +end; + +class function TDateTimeHelper.Create(AYear, AMonth, ADay, AHour, AMinute, ASecond, AMillisecond: Integer): TDateTime; +begin + Result := EncodeDateTime(AYear, AMonth, ADay, Ahour, AMinute, ASecond, AMillisecond); +end; + +class function TDateTimeHelper.Create(AYear, AMonth, ADay: Integer): TDateTime; +begin + Result := EncodeDate(AYear, AMonth, ADay); +end; + +class function TDateTimeHelper.Create(AHour, AMin, ASecond, AMillisecond: Integer): TDateTime; +begin + Result := EncodeTime(AHour, AMin, ASecond, AMillisecond); +end; + +class function TDateTimeHelper.CreateFromUnix(UnixTime: Int64): TDateTime; +begin + Result := UnixToDateTime(UnixTime); +end; + +class function TDateTimeHelper.CreateFromString(Value: String): TDateTime; +begin + Result := StrToDateTime(Value); +end; + +class function TDateTimeHelper.CreateFromString(Fmt, Value: String): TDateTime; +begin + case Fmt of + 'iso': + if not TryISOStrToDateTime(Value, Result) then + raise EConvertError.CreateFmt('Invalid ISO value ""', [Value]); + 'iso8601': + Result := ISO8601ToDate(Value); + 'unix': + Result := UnixToDateTime(StrToInt64(Value)); + else + Result := ScanDateTime(Fmt, Value); + end; +end; + +class function TDateTimeHelper.Now: TDateTime; +begin + Result := SysUtils.Now(); +end; + +class function TDateTimeHelper.NowUTC: TDateTime; +begin + Result := IncMinute(SysUtils.Now(), GetLocalTimeOffset()); +end; + +function TDateTimeHelper.ToUnix(IsUTC: Boolean): Int64; +begin + Result := DateTimeToUnix(Self, IsUTC); +end; + +function TDateTimeHelper.ToString(Fmt: String): String; +begin + Result := FormatDateTime(Fmt, Self); +end; + +function TDateTimeHelper.ToString: String; +begin + Result := DateToStr(Self); +end; + +function TDateTimeHelper.AddYears(Amount: Integer): TDateTime; +begin + Result := IncYear(Amount); +end; + +function TDateTimeHelper.AddMonths(Amount: Integer): TDateTime; +begin + Result := IncMonth(Amount); +end; + +function TDateTimeHelper.AddDays(Amount: Integer): TDateTime; +begin + Result := IncDay(Amount); +end; + +function TDateTimeHelper.AddHours(Amount: Int64): TDateTime; +begin + Result := IncHour(Amount); +end; + +function TDateTimeHelper.AddMinutes(Amount: Int64): TDateTime; +begin + Result := IncMinute(Amount); +end; + +function TDateTimeHelper.AddSeconds(Amount: Int64): TDateTime; +begin + Result := IncSecond(Amount); +end; + +function TDateTimeHelper.AddMilliseconds(Amount: Int64): TDateTime; +begin + Result := IncMilliSecond(Amount); +end; + +function TDateTimeHelper.YearsBetween(Other: TDateTime): Integer; +begin + Result := DateUtils.YearsBetween(Self, Other); +end; + +function TDateTimeHelper.MonthsBetween(Other: TDateTime): Integer; +begin + Result := DateUtils.MonthsBetween(Self, Other); +end; + +function TDateTimeHelper.WeeksBetween(Other: TDateTime): Integer; +begin + Result := DateUtils.WeeksBetween(Self, Other); +end; + +function TDateTimeHelper.DaysBetween(Other: TDateTime): Integer; +begin + Result := DateUtils.DaysBetween(Self, Other); +end; + +function TDateTimeHelper.HoursBetween(Other: TDateTime): Int64; +begin + Result := DateUtils.HoursBetween(Self, Other); +end; + +function TDateTimeHelper.MinutesBetween(Other: TDateTime): Int64; +begin + Result := DateUtils.MinutesBetween(Self, Other); +end; + +function TDateTimeHelper.SecondsBetween(Other: TDateTime): Int64; +begin + Result := DateUtils.SecondsBetween(Self, Other); +end; + +function TDateTimeHelper.MilliSecondsBetween(Other: TDateTime): Int64; +begin + Result := DateUtils.MilliSecondsBetween(Self, Other); +end; + 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) @@ -104,7 +400,7 @@ function FormatMilliseconds(Time: Double; Fmt: string): String; case p-q of 1: Include(Flags, fmtYearsNoPad); 2: Include(Flags, fmtYears); - else raise Exception.CreateFmt('FormatMilliseconds: Illegal format "%s"', [Fmt]); + else SimbaException('Illegal format "%s"', [Fmt]); end; end; 'M': begin @@ -112,7 +408,7 @@ function FormatMilliseconds(Time: Double; Fmt: string): String; case p-q of 1: Include(Flags, fmtMonthsNoPad); 2: Include(Flags, fmtMonths); - else raise Exception.CreateFmt('FormatMilliseconds: Illegal format "%s"', [Fmt]); + else SimbaException('Illegal format "%s"', [Fmt]); end; end; 'W': begin @@ -120,7 +416,7 @@ function FormatMilliseconds(Time: Double; Fmt: string): String; case p-q of 1: Include(Flags, fmtWeeksNoPad); 2: Include(Flags, fmtWeeks); - else raise Exception.CreateFmt('FormatMilliseconds: Illegal format "%s"', [Fmt]); + else SimbaException('Illegal format "%s"', [Fmt]); end; end; 'D': begin @@ -128,7 +424,7 @@ function FormatMilliseconds(Time: Double; Fmt: string): String; case p-q of 1: Include(Flags, fmtDaysNoPad); 2: Include(Flags, fmtDays); - else raise Exception.CreateFmt('FormatMilliseconds: Illegal format "%s"', [Fmt]); + else SimbaException('Illegal format "%s"', [Fmt]); end; end; 'h': begin @@ -136,7 +432,7 @@ function FormatMilliseconds(Time: Double; Fmt: string): String; case p-q of 1: Include(Flags, fmtHoursNoPad); 2: Include(Flags, fmtHours); - else raise Exception.CreateFmt('FormatMilliseconds: Illegal format "%s"', [Fmt]); + else SimbaException('Illegal format "%s"', [Fmt]); end; end; 'm': begin @@ -144,7 +440,7 @@ function FormatMilliseconds(Time: Double; Fmt: string): String; case p-q of 1: Include(Flags, fmtMinutesNoPad); 2: Include(Flags, fmtMinutes); - else raise Exception.CreateFmt('FormatMilliseconds: Illegal format "%s"', [Fmt]); + else SimbaException('Illegal format "%s"', [Fmt]); end; end; 's': begin @@ -152,7 +448,7 @@ function FormatMilliseconds(Time: Double; Fmt: string): String; case p-q of 1: Include(Flags, fmtSecondsNoPad); 2: Include(Flags, fmtSeconds); - else raise Exception.CreateFmt('FormatMilliseconds: Illegal format "%s"', [Fmt]); + else SimbaException('Illegal format "%s"', [Fmt]); end; end; 'u': begin @@ -160,7 +456,7 @@ function FormatMilliseconds(Time: Double; Fmt: string): String; case p-q of 1: Include(Flags, fmtMillisecondsNoPad); 2: Include(Flags, fmtMilliseconds); - else raise Exception.CreateFmt('FormatMilliseconds: Illegal format "%s"', [Fmt]); + else SimbaException('Illegal format "%s"', [Fmt]); end; end; else diff --git a/Source/simba.openssl.pas b/Source/simba.openssl.pas index 44db8699d..c623713d0 100644 --- a/Source/simba.openssl.pas +++ b/Source/simba.openssl.pas @@ -16,7 +16,7 @@ interface uses - classes, sysutils; + Classes, SysUtils; procedure ExtractOpenSSL; function LoadSSL(Debug: Boolean = False): Boolean; diff --git a/Tests/file.simba b/Tests/file.simba index 99a6f4ef0..bb34be05c 100644 --- a/Tests/file.simba +++ b/Tests/file.simba @@ -19,7 +19,7 @@ begin Assert(FileWrite(TESTFILE, 'Write')); Assert(FileRead(TESTFILE) = 'Write'); - Assert(MilliSecondsBetween(Now(), FileLastWriteTime(TESTFILE)) < 1000); + Assert(TDateTime.Now().MilliSecondsBetween(FileLastWriteTime(TESTFILE)) < 1000); Assert(FileSize(TESTFILE) = Length('Write')); Assert(FileWrite(TESTFILE, '0' * Round(0.25 * (1024 * 1024))));