#1 2015-03-12 11:10:00

itSDS
Member
From: Germany
Registered: 2014-04-24
Posts: 516

Wrong DateTimeToIso8601 Calculation in SynCrossPlatformSpecific

Hi Arnaud today i found a bug in SCPS.pas

In FormatDateTime you use yyyy-mm-ddThh:nn:ss  where the T would be expanded like t to Clocktime in ShortTimeFormat

Patch:

function DateTimeToIso8601(Value: TDateTime): string;
begin // e.g. YYYY-MM-DD Thh:mm:ss or YYYY-MM-DDThh:mm:ss
  if Value<=0 then
    result := '' else
  if frac(Value)=0 then
    result := FormatDateTime('yyyy-mm-dd',Value) else
  if trunc(Value)=0 then
    result := 'T' + FormatDateTime('hh:nn:ss',Value) else
    result := FormatDateTime('yyyy-mm-dd',Value) + 'T' + FormatDateTime('hh:nn:ss',Value);;
end;

Rad Studio 12.1 Santorini

Offline

#2 2015-03-12 11:14:45

itSDS
Member
From: Germany
Registered: 2014-04-24
Posts: 516

Re: Wrong DateTimeToIso8601 Calculation in SynCrossPlatformSpecific

patch for SynCrossPlatformJSON:

function DateTimeToIso8601(Value: TDateTime): string;
begin // e.g. YYYY-MM-DD Thh:mm:ss or YYYY-MM-DDThh:mm:ss
  if Value=0 then
    result := '' else
  if frac(Value)=0 then
    result := FormatDateTime('yyyy"-"mm"-"dd',Value) else
  if trunc(Value)=0 then
    result := '"T' + FormatDateTime('"hh":"nn":"ss',Value) else
    result := FormatDateTime('yyyy"-"mm"-"dd"',Value) + 'T' + FormatDateTime('"hh":"nn":"ss',Value);
end;

Rad Studio 12.1 Santorini

Offline

#3 2015-03-12 11:19:44

itSDS
Member
From: Germany
Registered: 2014-04-24
Posts: 516

Re: Wrong DateTimeToIso8601 Calculation in SynCrossPlatformSpecific

I found one place with FormatDateTime in mormot.pas where i don't know if customformat can contain 't'.
If this could be the case the output will be malformatted.

Last edited by itSDS (2015-03-12 11:50:49)


Rad Studio 12.1 Santorini

Offline

#4 2015-03-12 13:08:23

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,662
Website

Re: Wrong DateTimeToIso8601 Calculation in SynCrossPlatformSpecific

Are you talking about TSQLTable.ExpandAsString?
In this method, the format is the raw value, to be customized in the end user application as expected by the UI.
So I guess it is safe as such.

About SynCrossPlatformSpecific.pas, this function is specific to SMS, and in SMS RTL, the FormatDateTime() function does not have any process about 'T' so there should be no problem.

But for SynCrossPlatformJSON.pas, I guess that since we use "T" in the format string, it will be written directly (thanks to the double quotes), so there is no issue with the current implementation, isn't it?

Offline

#5 2015-03-12 14:41:44

itSDS
Member
From: Germany
Registered: 2014-04-24
Posts: 516

Re: Wrong DateTimeToIso8601 Calculation in SynCrossPlatformSpecific

1. yes i made a textsearch for FormatDateTime and there it is used.

2. OK - I modified SynCrossPlatformSpecific.pas to use DateTimeToIso8601 for windows - that's the reason i get that error.
But this function is needed cause it is in wrapper generated code for a record using TDateTime as field.

3. I do not know that. But if "mm" is transformed then "T" would also be transformed.


Rad Studio 12.1 Santorini

Offline

#6 2015-03-12 16:05:58

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,662
Website

Re: Wrong DateTimeToIso8601 Calculation in SynCrossPlatformSpecific

T is inside the quotes whereas mm is outside the quotes....

Offline

#7 2015-03-12 17:29:06

itSDS
Member
From: Germany
Registered: 2014-04-24
Posts: 516

Re: Wrong DateTimeToIso8601 Calculation in SynCrossPlatformSpecific

hm... you're right wink

I just checked it and a T in "T" will be printed as T

You only have to add the " to the t in "SCPS".pas

Last edited by itSDS (2015-03-12 17:47:36)


Rad Studio 12.1 Santorini

Offline

#8 2015-03-12 17:38:02

itSDS
Member
From: Germany
Registered: 2014-04-24
Posts: 516

Re: Wrong DateTimeToIso8601 Calculation in SynCrossPlatformSpecific

But nevertheless there is a problem with SynCrossPlatformSpecific. Let me explain:

I have a interfacefunction with a record as parameter.

RExample = packed record
   textvalue : String;
   datevalue : TDateTime;
....
end;

Now i create my crossplatformwrapper and it contains this 2 function:

function Variant2RExample(_variant: variant): RExample;
begin
  result.textvalue := _variant.textvalue;
  result.datevalue := Iso8601ToDateTime(_variant.datevalue);
end;

function RExample2Variant(const _record: RExample): variant;
var
    res: TJSONVariantData;
begin
  res.Init;
  res.SetPath('textvalue',_record.textvalue);
  res.SetPath('datevalue',DateTimeToIso8601(_record.datevalue));
  result := variant(res);
end;

If i compile this with your version the both Date - Function are missing -> Compiler Error.
==> We need both function also for Windows (and Firemonkey)

I modified my Version and can compile. I send it via email to you.


Rad Studio 12.1 Santorini

Offline

#9 2015-03-12 20:31:41

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,662
Website

Re: Wrong DateTimeToIso8601 Calculation in SynCrossPlatformSpecific

I do not understand exactly.

AFAIK Iso8601ToDateTime and DateTimeToIso8601 functions are defined in SynCrossPlatformJSON.pas, which is part of the generated mORMotClient.pas unit.

From what you sent to me, it sounds like if your SynCrossPlatform*.pas units and the *.mustache templates may be a bit outdated.
Are you using the latest revision?

Offline

#10 2015-03-12 21:34:29

itSDS
Member
From: Germany
Registered: 2014-04-24
Posts: 516

Re: Wrong DateTimeToIso8601 Calculation in SynCrossPlatformSpecific

Tyvm arnaud - now i understand.
Looking for the date functions i found the first occurence in SynCrossPlatformSpecific.pas and thougth you forgot to declare it for Windows. Didn't take a look in SynCrossPlatformJSON.

I have changed the units now and it works fine again - without my "Patch"


Rad Studio 12.1 Santorini

Offline

Board footer

Powered by FluxBB