0

MBox files, like Google Gmail export mailbox contains various types of data, among other things, the date of the mail message that interests me. The date of the message is in the format:

DayOfWeek, dd monthname yyyy hh:mm:ss +timezone
Mon, 03 Jun 2019 15:32:25 +0200

I was looking for some ready-made function in Delphi that I could translate a date string into TDateTime. If the whole string is impossible to parse to TDateTime, I can only try parse this part:

dd monthname yyyy hh:mm:ss
03 Jun 2019 15:32:25

but I admit that it is a bit of a hassle in my language to parse the month name. Could I ask for such a function if it exists? If such a ready-made function does not exist, I will make my own (or at least try).

Thank you in advance!

EDIT: SOLUTION

My guess is that this may not be useful to anyone, but if someone will have a similar problem, have a solution here. You can probably write it more elegantly, but it works.

// You can make your own formats here
const months : array[1..12,1..2] of string =
                        (('-01-',' Jan '),
                        ('-02-',' Feb '),
                        ('-03-',' Mar '),
                        ('-04-',' Apr '),
                        ('-05-',' May '),
                        ('-06-',' Jun '),
                        ('-07-',' Jul '),
                        ('-08-',' Aug '),
                        ('-09-',' Sep '),
                        ('-10-',' Oct '),
                        ('-11-',' Nov '),
                        ('-12-',' Dec '));

function ChangeDateFormat(input: String): String;
var i: Integer;
begin
  // day name and timezone is not needed, so we cut it
  delete(input,1,Pos(',',input)+1);
  delete(input,Pos('+',input)-1, Length(input));

   for i := 1 to 12 do
      input := StringReplace(input,
      months[i,2],months[i,1],
      [rfReplaceAll, rfIgnoreCase]);
   result := Trim(input);
end;

For 'Date: Mon, 03 Jun 2019 15:32:25 +0200' mbox date string it looks like

WriteLn(ChangeDateFormat('Date: Mon, 03 Jun 2019 15:32:25 +0200'));

it gives back

03-06-2019 15:32:25

which is already recognized e.g. by Excel and allows sorting or other operations.

At the moment I haven't found any ready-made functions in Delphi.

kwadratens
  • 187
  • 15
  • I don't think Delphi itself has a built-in function to handle either of those formats (they are not supported by `StrToDate/Time()`), but Indy (which ships with Delphi) supports both formats in its `StrInternetToDateTime()` and `GMTToLocalDateTime()` functions in its `IdGlobalProtocols` unit. – Remy Lebeau Oct 08 '21 at 21:10
  • 1
    It's the ARPA internet text messages format as per [RFC2822 § 3.3](https://datatracker.ietf.org/doc/html/rfc2822#section-3.3), originally [RFC822 § 5.1](https://datatracker.ietf.org/doc/html/rfc822#section-5.1) and it's not bound to emails or the MBox format. – AmigoJack Oct 08 '21 at 21:20
  • You can try the function in [my answer here](https://stackoverflow.com/a/3789628/62576) - it may work for that format, although I haven't tested it that way. – Ken White Oct 08 '21 at 21:21

1 Answers1

1

It is also called RFC1123 format:

https://www.ietf.org/rfc/rfc1123.txt

kbmMW's date time features supports that and many other formats.

Eg.

var
  dt:TkbmMWDateTime;
  ndt:TDateTime;
begin
   dt.RFC1123DateTime:='Mon, 03 Jun 2019 15:32:25 +0200';
   ndt:=dt.Local; // ndt will contain the local time variant.
                  // If you want to get the UTC time variant use
                  // ndt:=dt.UTC
end;

This feature is included in kbmMW Community Edition, which is free and is available for various versions (incl. 10.4.2) of Delphi. When Delphi 11 Community Edition is released, kbmMW Community Edition will follow. Notice that kbmMW Community Edition installs in all Delphi variants, also Pro, Ent and Architect. kbmMW Community Edition is free to use, also for commercial use (within license limits).

Download kbmMW CE after registering at https://portal.components4developers.com

Kim Madsen
  • 242
  • 2
  • 2
  • Thank You for that solution. Although I wrote (in the meantime) a simple function that parses this text for me, I'll also try your solution but I don't know if I will choose it because of the additional component. – kwadratens Oct 11 '21 at 08:17