从Windows时区代码转换到Linux在FreePascal的
-
12-09-2019 - |
题
我有这样的代码,在FreePascal的Windows环境下工作,并需要将其转换为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;
解决方案
此人有答案: HTTP:/ /www.mail-archive.com/fpc-pascal@lists.freepascal.org/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}
其他提示
我没有做很多的帕斯卡最近,所以这只是一个提示,而不是一个完整的答案。
但检查出你的编译器如何调用和链接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“),并使用 '长整型时区' 的全局变量,而不是 'LocalTimeZone.Bias'
。我想主要障碍是要弄清楚如何调用C代码。也许你甚至可以直接从帕斯卡使用time.h中,我将调查这一点。
不隶属于 StackOverflow