Is there a Delphi RTL function that can convert the ISO 8601 base date format to TDate?

ISO 8601 describes a so-called basic date format that does not use a dash:

20140507 is a valid representation of a more readable 2014-05-07.

Is there a Delphi RTL function that can interpret this basic format and convert it to a TDateTime value?

I tried

function TryIso2Date(const _s: string; out _Date: TDateTime): Boolean;
var
  Settings: TFormatSettings;
begin
  Settings := GetUserDefaultLocaleSettings;
  Settings.DateSeparator := #0;
  Settings.ShortDateFormat := 'yyyymmdd';
  Result := TryStrToDate(_s, Date, Settings);
end;

TryIso2Date('20140507', dt);

but this did not work, because DateSeparator could not be found in the string.

The only solution I have come across so far (other than writing parsing code) is to add the missing hyphens before calling TryStrToDate:

function TryIso2Date(const _s: string; out _Date: TDateTime): Boolean;
var
  Settings: TFormatSettings;
  s: string;
begin
  Settings := GetUserDefaultLocaleSettings;
  Settings.DateSeparator := #0;
  Settings.ShortDateFormat := 'yyyy-mm-dd';
  s := Copy(_s,1,4) + '-' + Copy(_s, 5,2) + '-' + Copy(_s, 7);
  Result := TryStrToDate(_s, Date, Settings);
end;

TryIso2Date('20140507', dt);

It works, but it seems rather awkward.

This is Delphi XE6, so it should have the highest possible RTL.

+4
1

Copy, , . :

function TryIso8601BasicToDate(const Str: string; out Date: TDateTime): Boolean;
var
  Year, Month, Day: Integer;
begin
  Assert(Length(Str)=8);
  Result := TryStrToInt(Copy(Str, 1, 4), Year);
  if not Result then
    exit;
  Result := TryStrToInt(Copy(Str, 5, 2), Month);
  if not Result then
    exit;
  Result := TryStrToInt(Copy(Str, 7, 2), Day);
  if not Result then
    exit;
  Result := TryEncodeDate(Year, Month, Day, Date);
end;
+2

Source: https://habr.com/ru/post/1543766/


All Articles