Question

I create a Delphi XE5 DataSnap Rest server (using FireDac components).

All working ok so far except RECEIVING TDATETIME as parameter.

After some research I understand that this Type of Data is not usable in DataSnap (at least direct).

So I want to make a basic function like this:

InsertCustomer(aID : Integer; aDateOfBirth : TDateTime) : Integer;

How can I make this function because I cannot use the TDateTime...

My idea (request) is to put something similar(identic) with the field that is exported from TDataSet.

when a column TDateTime from a TDataSet is exported is like this... ["2013-10-10 10:47:40.0"]

Was it helpful?

Solution

There is no standard way to transfer dates and times using either url parameters or JSON. Keep in mind that TDateTime is actually a double behind the scenes, so defining the method like in your example, the argument should be given as a floating point number in the url.

If you want to accept a date as an ISO8601 string, you have to define the argument as a string type and manually do the conversion. Here is one function I use in my own code for exactly this type of problem.

uses
  Soap.XSBuiltIns;

function ISODateStrToDate(DateStr: string): TDateTime;
var
  xsDate: TXSDate;
begin
  xsDate := TXSDate.Create;
  try
    xsDate.XSToNative(DateStr);
    Exit(xsDate.AsDate);
  finally
    xsDate.Free;
  end;
end;

This function throws away the time portion of the ISO8601 string. I don't need it in most cases.

If you need the time portion, you can possibly use

function XMLTimeToDateTime(const XMLDateTime: InvString; AsUTCTime: Boolean = False): TDateTime;

this is from the same Soap.XSBuiltIns unit, however BE WARNED! This function only works correctly for full ISO8601 strings which includes full time zone information (the +01:00 or -05:00 ending part). For your example ISO8601 datetime it will act as if you have given the time in UTC time and it will try to convert it to a TDateTime value in local time.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top