Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to convert local TDateTime to GMT/UTC Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
15-Aug-03
Category
Algorithm
Language
Delphi 2.x
Views
117
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Tomas Rutkauskas

Does anybody know of any API or component that will convert a local datetime into 
GMT/UTC, using the Windows settings?

Answer:

Solve 1:

1   function NowUTC: TDateTime;
2   var
3     system_datetime: TSystemTime;
4   begin
5     GetSystemTime(system_datetime);
6     Result := SystemTimeToDateTime(system_datetime);
7   end;



Solve 2:

8   { ... }
9   const
10    MinsPerDay = 24 * 60;
11  
12  function GetGMTBias: Integer;
13  var
14    info: TTimeZoneInformation;
15    Mode: DWord;
16  begin
17    Mode := GetTimeZoneInformation(info);
18    Result := info.Bias;
19    case Mode of
20      TIME_ZONE_ID_INVALID:
21        RaiseLastOSError;
22      TIME_ZONE_ID_STANDARD:
23        Result := Result + info.StandardBias;
24      TIME_ZONE_ID_DAYLIGHT:
25        Result := Result + info.DaylightBias;
26    end;
27  end;
28  
29  function GMTNow: TDateTime;
30  begin
31    Result := LocaleToGMT(Now);
32  end;
33  
34  function LocaleToGMT(const Value: TDateTime): TDateTime;
35  begin
36    Result := Value + (GetGMTBias / MinsPerDay);
37  end;
38  
39  function GMTToLocale(const Value: TDateTime): TDateTime;
40  begin
41    Result := Value - (GetGMTBias / MinsPerDay);
42  end;



Solve 3:
43  
44  function MakeUTCTime(DateTime: TDateTime): TDateTime;
45  var
46    TZI: TTimeZoneInformation;
47  begin
48    case GetTimeZoneInformation(TZI) of
49      TIME_ZONE_ID_STANDARD:
50        begin
51          Result := DateTime + (TZI.Bias / 60 / 24);
52        end;
53      TIME_ZONE_ID_DAYLIGHT:
54        begin
55          Result := DateTime + (TZI.Bias / 60 / 24) + TZI.DaylightBias;
56        end
57    else
58      raise
59        Exception.Create('Error converting to UTC Time. Time zone could not be 
60  determined.'
61    end;
62  end;


It's probably worth pointing out that this function will only work if the source of the TDateTime being converted was the system clock on the local machine (rather than, say, a field in a database or disk file) and if the timezone hasn't changed (e.g. between daylight saving and standard time) since the date was recorded.

			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC