Files
MycLib/Src/AST/Myc.Ast.RTL.DateTime.pas
T
Michael Schimmel 1c6a6fe5a0 RTL refactoring
2026-01-13 17:01:33 +01:00

60 lines
1.8 KiB
ObjectPascal

unit Myc.Ast.RTL.DateTime;
interface
uses
System.SysUtils,
System.DateUtils,
Myc.Data.Scalar,
Myc.Data.Value,
Myc.Ast.Attributes;
type
TRtlDateTimeFunctions = record
public
[TRtlExport('now', Impure)]
[AstDoc('Returns the current system timestamp.')]
[AstSignature('() -> datetime')]
class function Now(const Args: TArray<TDataValue>): TDataValue; static;
[TRtlExport('date', Impure)]
[AstDoc('Returns the current date or constructs a date from Y, M, D.')]
[AstSignature('() -> datetime')]
[AstSignature('(number, number, number) -> datetime')]
class function Date(const Args: TArray<TDataValue>): TDataValue; static;
end;
implementation
{ TRtlDateTimeFunctions }
class function TRtlDateTimeFunctions.Now(const Args: TArray<TDataValue>): TDataValue;
begin
Result := TScalar.FromDateTime(System.SysUtils.Now);
end;
class function TRtlDateTimeFunctions.Date(const Args: TArray<TDataValue>): TDataValue;
function GetInt(const V: TDataValue; ArgIndex: Integer): Word;
begin
if V.AsScalar.Kind <> TScalar.TKind.Ordinal then
raise EArgumentException.CreateFmt('Date argument %d must be an Integer.', [ArgIndex]);
Result := V.AsScalar.Value.AsInt64;
end;
begin
if Length(Args) = 3 then
begin
var y := GetInt(Args[0], 1);
var m := GetInt(Args[1], 2);
var d := GetInt(Args[2], 3);
Result := TScalar.FromDateTime(EncodeDate(y, m, d));
end
else if Length(Args) = 0 then
Result := TScalar.FromDateTime(System.SysUtils.Date)
else
raise EArgumentException.Create('Date expects 0 or 3 arguments (Year, Month, Day).');
end;
end.