Last active
December 25, 2021 00:46
-
-
Save DelphiWorlds/d5cfd45ece852c0a1f6a5577bc475ea1 to your computer and use it in GitHub Desktop.
Convert an RSS/Atom date to datetime
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated to cater for different locales