unit Delivery.Web.Helpers;

interface

function CustomStrToDateTime(const ADatePart, ATimePart: string): TDateTime;

implementation

uses
  System.SysUtils,
  System.RegularExpressions;

function CustomStrToDateTime(const ADatePart, ATimePart: string): TDateTime;
const
  StrDatePattern = '(?<YEAR>\d{4})\-(?<MONTH>\d{2})\-(?<DAY>\d{2})';
  StrTimePattern = '(?<HOUR>\d{1,2})\:(?<MINS>\d{1,2})';
begin
  var LDateMatch := TRegEx.Match(ADatePart, StrDatePattern);
  if not LDateMatch.Success then
    raise Exception.Create('Data in formato non valido: ' + ADatePart);
  var LTimeMatch := TRegEx.Match(ATimePart, StrTimePattern);
  if not LTimeMatch.Success then
    raise Exception.Create('Ora in formato non valido: ' + ATimePart);
  var LDate := EncodeDate(
    StrToInt(LDateMatch.Groups['YEAR'].Value),
    StrToInt(LDateMatch.Groups['MONTH'].Value),
    StrToInt(LDateMatch.Groups['DAY'].Value)
  );
  var LTime := EncodeTime(
    StrToInt(LTimeMatch.Groups['HOUR'].Value),
    StrToInt(LTimeMatch.Groups['MINS'].Value),
    0, 0
  );
  Result := LDate + LTime;
end;

end.
