Skip to content

Instantly share code, notes, and snippets.

@DelphiWorlds
Last active December 25, 2021 00:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DelphiWorlds/d5cfd45ece852c0a1f6a5577bc475ea1 to your computer and use it in GitHub Desktop.
Save DelphiWorlds/d5cfd45ece852c0a1f6a5577bc475ea1 to your computer and use it in GitHub Desktop.
Convert an RSS/Atom date to datetime
uses
System.SysUtils, System.DateUtils, System.StrUtils;
// examples:
// Wed, 3 Nov 2021 18:20:21 +1030
// 21 Dec 2021 19:04:06 GMT
function FeedDateToDateTime(const AFeedDate: string): TDateTime;
var
LParts: TArray<string>;
LISO8601, LDay, LMonth: string;
LFirst: Integer;
LFormatSettings: TFormatSettings;
begin
LFormatSettings := TFormatSettings.Create('en-US');
LParts := AFeedDate.Split([' ']);
LFirst := 0;
if AFeedDate.IndexOf(',') > -1 then
LFirst := 1;
LDay := LParts[LFirst].PadLeft(2, '0');
LMonth := (IndexText(LParts[LFirst + 1], LFormatSettings.ShortMonthNames) + 1).ToString;
LISO8601 := Format('%s-%s-%sT%s', [LParts[LFirst + 2], LMonth, LDay, LParts[LFirst + 3]]);
Result := ISO8601ToDate(LISO8601);
if (Length(LParts) > 4) and LParts[4].Equals('GMT') then
Result := TTimeZone.Local.ToLocalTime(Result);
end;
@DelphiWorlds
Copy link
Author

Updated to cater for different locales

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment