Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to get UTC time and date from local time?

Status
Not open for further replies.

CADTenchy

Technical User
Dec 12, 2007
237
GB

I'm falling over with this probably easy task.

I'm trying to get from the computer time, a time and date that is in UTC.

Time in format hh:mm (also in hh:mm:ss too)
Date in format yymmdd

I've tried adding ZoneInfo.Bias and ZoneInfo.DaylightBias to my time, but that gets some odd time that counts backwards.

Looking deeper, it seems I can use a function TzSpecificLocalTimeToSystemTime, but I don't get how to use that.
(SystemTime gives UTC all over world, yes?)

Would some kind soul put me out of my misery?


Steve (Delphi 2007 & XP)
 
Open "Windows SDK" in Delphi Help. (aka "Win32 Developer's Reference")

In the index, browse to "System Time". That page has several links. One is the structure itself, "SYSTEMTIME".

I have a demo app somewhere in my archives that shows all you can do. If I can find it, I'll post it for you. In the mean-time (pun intended), that should keep you busy.

Chicago - Does anybody really know what time it is?
Roo
 

I had found that in help, but I do have trouble interpreting the help into practical code with some of these functions.

I can't seem to see how to translate the info presented
Code:
typedef struct _SYSTEMTIME {  WORD wYear;  WORD wMonth;  WORD wDayOfWeek;  WORD wDay;  WORD wHour;  WORD wMinute;  WORD wSecond;  WORD wMilliseconds;
} SYSTEMTIME,  *PSYSTEMTIME;
into something usable like this:
Code:
procedure TMainForm.TimeZoneClick(Sender: TObject);
var
  MyTime : TDateTime;
  UTC: TSystemTime;
begin
  MyTime := Time;
  GetSystemTime(UTC);
  with Memo1.Lines do
  begin
    Add('PC time: '+TimeToStr(MyTime));
    Add('UTC: '+IntToStr(UTC.wHour)+':'+IntToStr(UTC.wMinute)+':'+IntToStr(UTC.wSecond));
    Add('Date: '+IntToStr(UTC.wYear)+':'+IntToStr(UTC.wMonth)+':'+IntToStr(UTC.wDay));
  end;
end;

(I found something like the above to adapt to suit me with another go at google today using some different search terms.)

Steve (Delphi 2007 & XP)
 
I can't seem to see how to translate the info presented... CODE OMITTED ...into something usable like this:... CODE OMITTED
Before explaining, let me ask this: Did your version of Delphi 7 include the RTL source code?

If "yes", right-click on "TSystemTime" in your code and select "Find Declaration".

If not, here is a what you'd see: (from ..\\delphi7\source\rtl\Win\Windows.pas
Code:
type
  { System time is represented with the following structure: }
  PSystemTime = ^TSystemTime;
  _SYSTEMTIME = record
    wYear: Word;
    wMonth: Word;
    wDayOfWeek: Word;
    wDay: Word;
    wHour: Word;
    wMinute: Word;
    wSecond: Word;
    wMilliseconds: Word;
  end;
  {$EXTERNALSYM _SYSTEMTIME}
  TSystemTime = _SYSTEMTIME;
  SYSTEMTIME = _SYSTEMTIME;
  {$EXTERNALSYM SYSTEMTIME}
As you can see, Delphi has done it for you. Most all of Windows.pas is a wrapper around the Windows 32 API.

So, your code
Code:
  GetSystemTime(UTC);
fills all the UTC record fields as the strucure shows. Use the DateTimeToSystemTime and SystemTimeToDateTime functions to convert between TSystemTime values and TDateTime values.

HTH

Roo
Delphi Rules!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top