Determining the current date/time in the UK

Richard2000
Richard2000 used Ask the Experts™
on
Hi,

I have a CGI program that writes a line to a log file each time it is run.  On each line I want to record (amongst other things) the current date/time in the UK when the CGI program was run.

The problem is that I am in the UK and my web hosting company is in Canada, so there is a time difference.  When I use the Now function, the current date/time in Canada is returned, which at present is 5 hours behind the current UK date/time.

I therefore have tried using this code to get the current UK date/time:

UKDateTime := Now + EncodeTime(5, 0, 0, 0);

This seems to work, but there are a couple of issues:

* If I change my web hosting company to a different provider in a different time zone, this will no longer work unless I change my program.

* More importantly, in the UK the clocks are put forward a hour in March and put back a hour in October.  If Canada doesn't do this (or they do anything else similar throughout the year), then the UK date/time that I calculate may be wrong.

So I would like to know - what is the best way for a CGI program to obtain the current date/time in the UK (regardless of what country it is being run in)?

Thanks in Advance,

Richard
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
could you just get your cgi to ask a uk time server? this would save doing it yourself, and as the cgi runs on a web server it'll always be connected

Commented:
The best thing to do is to use is GMT time, which can be obtained by running the function GetSystemTime() from Delphi (which bypasses regional settings and gets the GMT time from the system)

Commented:
assuming the system bios time is set to GMT, which most people will not, they'll use local time
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Commented:
I've used this example of my CGI application to set date and time independently of country zone:
You can use GetTimeZoneInformation function to get current bias, then you can substract it from current time

procedure TwaSet.SetTime(ADateTime: TDateTime);
var
  UniTime: SystemTime;
  TimeZone: Time_Zone_Information;
  Year, Month, Day, Hour, Min, Sec, MS: Word;
begin
  GetTimeZoneInformation(TimeZone);
  ADateTime:= ADateTime + (TimeZone.Bias / 24) / 60;
  DecodeDate(ADateTime, Year, Month, Day);
  DecodeTime(ADateTime, Hour, Min, Sec, MS);
  UniTime.wYear:= Year;
  UniTime.wMonth:= Month;
  UniTime.wDay:= Day;
  UniTime.wHour:= Hour;
  UniTime.wMinute:= Min;
  UniTime.wSecond:= Sec;
  UniTime.wMilliseconds:= MS;
  SetSystemTime(UniTime)
end;

Motaz

Author

Commented:
Hi,

Thanks for your comments and code.  A few questions:

* Does TimeZone.Bias return both negative and positive values?
* Does the above code take daylight saving time into account?
* Is Universal Time the same as UK time?

Thanks in Advance,

Richard

Commented:
1. Yes, it returns negative if time zone is + GMT, and it returns posative if Time zone is - GMT.
2. about Daylight time, please read help about TIME_ZONE_INFORMATION structure, see DaylightBias
3. Yes, it is GMT.
You can use below example to display bias of each country,
while testing this code you can change system TimeZone country and see the difference.

var
  TimeZone: TIME_ZONE_INFORMATION;
begin
  GetTimeZoneInformation(TimeZone);
  Label1.Caption:= IntToStr(TimeZone.Bias);

Motaz

Author

Commented:
Hi,

Thanks for your help!

Best Regards,

Richard

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial