Smart questions
Smart answers
Smart people
Join Tek-Tips Forums
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • 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!

Join Tek-Tips
*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.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

how to get UTC time and date from local time?

CADTenchy (TechnicalUser) (OP)
10 Jul 08 12:20

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)

roo0047 (Programmer)
10 Jul 08 22:06
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
CADTenchy (TechnicalUser) (OP)
11 Jul 08 7:19

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)

roo0047 (Programmer)
11 Jul 08 19:47

Quote:

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!
 

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

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