Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / pas2js / 2019-11

DateTimeToIso8601 issue

2 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

warleyalex

mORMot server computes a nounce client and uses the NowToIso8601 method to compute the nonce, so
apparently, this method in pas2js returns this format: NowToIso8601(); //--> 2019-11-2509:5809:58:05

but we want this expected result:
2019-11-25T09:58:09

I could find a way to get this format, hum I had to edit The SysUtils unit | the function FormatDateTime and modify to:

            'T': if Count = 1 then
	//	   StoreFormat(ShortTimeFormat, Nesting+1, True)
               StoreString('T')
                 else
	          StoreFormat(LongTimeFormat, Nesting+1, True);
[code]
function NowToIso8601: string;
begin
  result := DateTimeToIso8601(Now);
end;
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{,DateTimeZone.UTC}) else
  if trunc(Value)=0 then
    result := FormatDateTime('Thh:nn:ss',Value{,DateTimeZone.UTC}) else
    result :=
FormatDateTime('yyyy-mm-ddThh:nn:ss',Value{,DateTimeZone.UTC});
end;
[/code]
Michael Van Canneyt
to warleyalex
warleyalex wrote 16 lines
mORMot server computes a nounce client and uses the NowToIso8601 method to compute the nonce, so apparently, this method in pas2js returns this format: NowToIso8601(); //--> 2019-11-2509:5809:58:05 but we want this expected result: 2019-11-25T09:58:09 I could find a way to get this format, hum I had to edit The SysUtils unit | the function FormatDateTime and modify to: 'T': if Count = 1 then // StoreFormat(ShortTimeFormat, Nesting+1, True) StoreString('T') else StoreFormat(LongTimeFormat, Nesting+1, True);

That's wrong.

Show quoted text 16 lines
[code] function NowToIso8601: string; begin result := DateTimeToIso8601(Now); end; 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{,DateTimeZone.UTC}) else if trunc(Value)=0 then result := FormatDateTime('Thh:nn:ss',Value{,DateTimeZone.UTC}) else result := FormatDateTime('yyyy-mm-ddThh:nn:ss',Value{,DateTimeZone.UTC});

You must use quotes if you want a literal T character.

FormatDateTime('yyyy-mm-dd"T"hh:nn:ss',Value{,DateTimeZone.UTC});

But you don't need to implement this.

The dateutils unit contains DateTimeToRFC3339.
For all practical purposes, this is the same as ISO8601, so you can use that. I use it in all my code that needs a 'standard' date time.

Michael.