【问题标题】:Time Zone code translation from Windows to Linux in FreePascal在 FreePascal 中从 Windows 到 Linux 的时区代码转换
【发布时间】:2015-01-09 03:02:40
【问题描述】:

我有这段代码可以在 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;

【问题讨论】:

    标签: linux windows freepascal


    【解决方案1】:

    这家伙有答案: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}
    

    【讨论】:

    【解决方案2】:

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

    但请查看您的编译器如何调用和链接 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,我会对此进行调查。

    【讨论】:

    • 这可以是一个答案。我将在 FreePascal 下对此进行调查,然后接受或降级您的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-20
    • 2018-09-10
    • 1970-01-01
    • 2011-02-24
    • 2012-08-11
    • 2012-11-15
    相关资源
    最近更新 更多