FreePascal 中从 Windows 到 Linux 的时区代码翻译

发布于 2024-07-22 15:06:17 字数 693 浏览 3 评论 0原文

我有这段代码可以在 Windows 下的 FreePascal 中运行,需要将其转换为 Linux,但我完全迷失了时区偏差值:

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;

I have this code that works in FreePascal under Windows and need to translate it to Linux but I'm completely lost on the Time Zone Bias value:

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不乱于心 2024-07-29 15:06:17

这个人有答案:http://www.mail-archive.com/fpc-pascal@lists.freepascal.org/msg08467.html" rel="nofollow noreferrer"> mail-archive.com/[电子邮件受保护]/msg08467.html

所以你会想要添加使用子句:

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}

This guy has the answer: http://www.mail-archive.com/[email protected]/msg08467.html

So you'll want to add the uses clause:

uses unix,sysutils,baseunix

variables to hold the time / timezone:

 var
   timeval: TTimeVal;
   timezone: PTimeZone;

..and get the 'minutes west'.

{$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}
等风来 2024-07-29 15:06:17

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

但请检查您的编译器如何调用和链接 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;
}

此程序将输出类似的内容

  Current local time and date: Sat Jun 06 18:00:00 2009

您可以使用 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 ,我会对此进行调查。

I haven't done a lot of pascal lately, so these is just a hint, rather than a complete answer.

But check out your compiler how to call and link c-code. Then you can use time.h similar as in this C-example:

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

This program will output something like

  Current local time and date: Sat Jun 06 18:00:00 2009

You can use sprintf instead of printf to "print" into an array of characters, and strftime to give a format string how similar to 'ddd, dd mmm yyyy hh:nn:ss' (probably "%a, %d %b %Y %H:%M:%S") and use the 'long int timezone' global variable instead of 'LocalTimeZone.Bias'.

I guess the main hurdle is to figure out how to call the c-code. Maybe you can even use time.h directly from pascal, I would investigate that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文