Главная страница | назад





Article #27890: Working around time zone issues with Web Broker

Question:

Why are my short term cookies not working in WebBroker?

Answer:

When using Cookies in webbroker, it assumes all times are in GMT for cookies so setting a cookie to expire in 2 minutes will not work unless if your timezone is GMT.

Here is a workaround.

uses
{$IFDEF MSWINDOWS}Windows,{$ENDIF}
{$IFDEF LINUX}Libc,{$ENDIF}
DateUtils;
resourcestring
RSFailedTimeZoneInfo = 'Failed attempting to retrieve time zone
information.';
function OffsetFromUTC: TDateTime;
{$IFDEF LINUX}
var
T: TTime_T;
TV: TTimeVal;
UT: TUnixTime;
begin
gettimeofday(TV, nil);
T := TV.tv_sec;
localtime_r(@T, UT);
// __tm_gmtoff is the bias in seconds from the UTC to the current time.
// so I multiply by -1 to compensate for this.
Result := -1*(UT.__tm_gmtoff / 60 / 60 / 24);
end;
{$ENDIF}
{$IFDEF MSWINDOWS}
var
iBias: Integer;
tmez: TTimeZoneInformation;
begin
// Copied from IdGlobal.pas
case GetTimeZoneInformation(tmez) of
TIME_ZONE_ID_INVALID:
raise EFailedToRetreiveTimeZoneInfo.Create(RSFailedTimeZoneInfo);
TIME_ZONE_ID_UNKNOWN :
iBias := tmez.Bias;
TIME_ZONE_ID_DAYLIGHT :
iBias := tmez.Bias + tmez.DaylightBias;
TIME_ZONE_ID_STANDARD :
iBias := tmez.Bias + tmez.StandardBias;
else
raise EFailedToRetreiveTimeZoneInfo.Create(RSFailedTimeZoneInfo);
end;
{We use ABS because EncodeTime will only accept positve values}
Result := EncodeTime(Abs(iBias) div 60, Abs(iBias) mod 60, 0, 0);
end;
{$ENDIF}
Use it as: Expires := Now + (however long in TDateTimeFormat)+OffsetFromUTC;

such as: Expires := IncMinute(Now, DefaultTimeout) + OffsetFromUTC;

Last Modified: 16-OCT-01