Time Zone code translation from Windows to Linux in FreePascal
-
12-09-2019 - |
Question
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;
Solution
This guy has the answer: http://www.mail-archive.com/fpc-pascal@lists.freepascal.org/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}
OTHER TIPS
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.