×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

System Information

How do I get a time zone specific time? by Glenn9999
Posted: 1 May 20

I happened to run into a case where I needed to do something that was dependent on the specific time in a specific time zone. Now something like this might be standard function in newer Delphis, but it didn't appear in either Delphi 3 or Turbo Delphi 2006. I ended up coming up with this to do the job: A "Now" function that will give me a time-zone dependent response. It may have issues, but for what I used it for, it seems to be working consistently.

CODE

function NowTZ(Bias: Integer): TDateTime;
// make a Now function that works on time zone.
// "Bias" is a UTC "minus" value.  For example, CST is -6, EST is -5.
var
  TZ: TTimeZoneInformation;
  UTCTime, LocalTime: TSystemTime;
begin
  GetSystemTime(UTCTime);
  TZ.Bias := 0;

// set time zone biases for daylight savings time.  This works for most of the US.
// DST ends at First Sunday in November at 2am
  TZ.StandardBias:= Abs(Bias*60);
  TZ.StandardDate.wYear:= 0;
  TZ.StandardDate.wMonth:= 11; // November
  TZ.StandardDate.wDay:= 1; // First
  TZ.StandardDate.wDayOfWeek:= 0; // Sunday
  TZ.StandardDate.wHour:= 2;
  TZ.StandardDate.wMinute:= 0;
  TZ.StandardDate.wSecond:= 0;
  TZ.StandardDate.wMilliseconds:= 0;

  // DST starts at Second Sunday in March at 2am
  TZ.DaylightBias:= Abs((Bias+1)*60);
  TZ.DaylightDate.wYear:= 0;
  TZ.DaylightDate.wMonth:= 3; // March
  TZ.DaylightDate.wDay:= 2; // Second
  TZ.DaylightDate.wDayOfWeek:= 0; // Sunday
  TZ.DaylightDate.wHour:= 2;
  TZ.DaylightDate.wMinute:= 0;
  TZ.DaylightDate.wSecond:= 0;
  TZ.DaylightDate.wMilliseconds:= 0;

  SystemTimeToTzSpecificLocalTime(@TZ, UTCTime, LocalTime);
  Result := SystemTimeToDateTime(LocalTime);
end; 

Calling it is very simple...if I want to know what the time is in the Eastern Time Zone:

CODE

NowBias := NowTZ(-5); 

Back to Embarcadero: Delphi FAQ Index
Back to Embarcadero: Delphi Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close