views:

718

answers:

2

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;

Thanks.

Edit: I've added [lazyweb] since it's really one of those things... Edit: Since it's lazyweb, I might as well reward the person :)

A: 

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.

Tobias
This can be an answer. I'll investigate this under FreePascal and then accept or demote your answer.
Gustavo Carreno
+4  A: 

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}
russau
Thanks mate, you're a star. You have the loot ;)
Gustavo Carreno
woo hoo! The online reference is here: http://www.freepascal.org/docs-html/rtl/unix/fpgettimeofday.html
russau
So he gets the loot for linking to my answer?
Marco van de Voort
believe it or not - i've asked for a feature to split the loot with anyone who 'owns' a link: http://stackoverflow.uservoice.com/pages/1722-general/suggestions/194360-allow-users-to-claim-a-blog
russau