【问题标题】:Is there any function that easy converts MBox date format to Delphi TDateTime format?是否有任何功能可以轻松将 MBox 日期格式转换为 Delphi TDateTime 格式?
【发布时间】:2021-10-08 20:51:56
【问题描述】:

MBox 文件,例如 Google Gmail 导出邮箱,包含各种类型的数据,其中包括我感兴趣的邮件消息的日期。消息的日期格式为:

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

我在 Delphi 中寻找一些现成的函数,我可以将日期字符串转换为 TDateTime。如果整个字符串无法解析到TDateTime,我只能尝试解析这部分:

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

但我承认用我的语言解析月份名称有点麻烦。如果它存在,我可以要求这样的功能吗?如果没有这种现成的功能,我会自己做(或者至少尝试一下)。

提前谢谢你!

编辑:解决方案

我的猜测是,这可能对任何人都没有用,但如果有人遇到类似问题,请在此处找到解决方案。您可能可以更优雅地编写它,但它确实有效。

// 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;

对于 'Date: Mon, 03 Jun 2019 15:32:25 +0200' mbox 日期字符串,它看起来像

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

它会回馈

03-06-2019 15:32:25

已经被识别,例如通过 Excel 并允许排序或其他操作。

目前我还没有在Delphi中找到任何现成的函数。

【问题讨论】:

  • 我不认为 Delphi 本身具有处理这两种格式的内置函数(StrToDate/Time() 不支持它们),但 Indy(与 Delphi 一起提供)支持这两种格式它的StrInternetToDateTime()GMTToLocalDateTime() 函数在其IdGlobalProtocols 单元中。
  • 它是 RFC2822 § 3.3 的 ARPA 互联网短信格式,最初是 RFC822 § 5.1,它不受电子邮件或 MBox 格式的约束。
  • 你可以试试my answer here中的函数——它可能适用于那种格式,虽然我没有用那种方式测试过。

标签: date datetime delphi date-conversion


【解决方案1】:

也叫RFC1123格式:

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

kbmmW 的日期时间功能支持该格式以及许多其他格式。

例如。

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;

此功能包含在 kbmMW 社区版中,它是免费的,可用于 Delphi 的各种版本(包括 10.4.2)。 当 Delphi 11 社区版发布时,kbmMW 社区版也将随之而来。请注意,kbmMW Community Edition 安装在所有 Delphi 变体中,包括 Pro、Ent 和 Architect。 kbmMW Community Edition 可免费使用,也可用于商业用途(在许可限制内)。

https://portal.components4developers.com注册后下载kbmMW CE

【讨论】:

  • 感谢您提供的解决方案。尽管我(同时)编写了一个简单的函数来为我解析此文本,但我也会尝试您的解决方案,但我不知道是否会因为附加组件而选择它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
  • 2014-07-29
  • 1970-01-01
  • 1970-01-01
  • 2014-02-25
  • 1970-01-01
相关资源
最近更新 更多