0

我有这段代码在 Windows 下的 FreePascal 中工作,需要将它翻译到 Linux,但我完全迷失了 Time Zone Bias 值:

function DateTimeToInternetTime(const aDateTime: TDateTime): String;
{$IFDEF WIN32}
var
  LocalTimeZone: TTimeZoneInformation;
{$ENDIF ~WIN32}
begin
{$IFDEF WIN32}
  // eg. Sun, 06 Nov 1994 08:49:37 GMT  RFC 822, updated by 1123
  Result := FormatDateTime('ddd, dd mmm yyyy hh:nn:ss', aDateTime);
  // Get the Local Time Zone Bias and report as GMT +/-Bias
  GetTimeZoneInformation(LocalTimeZone);
  Result := Result + 'GMT ' + IntToStr(LocalTimeZone.Bias div 60);
{$ELSE}
  // !!!! Here I need the above code translated !!!!
  Result := 'Sat, 06 Jun 2009 18:00:00 GMT 0000';
{$ENDIF ~WIN32}
end;
4

2 回答 2

4

这家伙有答案: http: //www.mail-archive.com/fpc-pascal@lists.freepascal.org/msg08467.html

因此,您需要添加 uses 子句:

uses unix,sysutils,baseunix

保存时间/时区的变量:

 var
   timeval: TTimeVal;
   timezone: PTimeZone;

..并获得“西分钟”。

{$ELSE}
  Result := FormatDateTime('ddd, dd mmm yyyy hh:nn:ss', aDateTime);
  TimeZone := nil;
  fpGetTimeOfDay (@TimeVal, TimeZone);
  Result := Result + 'GMT ' + IntToStr(timezone^.tz_minuteswest div 60);
{$ENDIF ~WIN32}
于 2009-06-15T13:26:45.583 回答
0

我最近没有做很多帕斯卡,所以这些只是一个提示,而不是一个完整的答案。

但是请查看您的编译器如何调用和链接 c 代码。然后您可以使用类似于此 C 示例中的 time.h:

/* localtime example */
#include <stdio.h>
#include <time.h>

int main ()
{
  time_t rawtime;
  struct tm * timeinfo;

  time ( &rawtime );
  timeinfo = localtime ( &rawtime );
  printf ( "Current local time and date: %s", asctime (timeinfo) );

  return 0;
}

该程序将输出类似

  当前当地时间和日期:2009 年 6 月 6 日星期六 18:00:00

您可以使用 sprintf 而不是 printf 来“打印”到一个字符数组中,并使用 strftime 来给出一个格式字符串与 'ddd, dd mmm yyyy hh:nn:ss' 的相似程度(可能是 "%a, %d %b % Y %H:%M:%S") 并使用 'long int timezone' 全局变量而不是 'LocalTimeZone.Bias'。

我想主要的障碍是弄清楚如何调用 c 代码。也许您甚至可以直接从 pascal 中使用 time.h,我会对此进行调查。

于 2009-06-11T18:41:18.597 回答