[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2056
  • Last Modified:

TDateTime saved as UTC to local time- not so easy?

Hello,
 let me explain my problem on examples.

A. we have first date and time let's say:

12/02/06 15:45

as you can see this date/ time is in winter time

so in:

UK it is 15:45
Germany it is 16:45

B. we have second date and time let's say:

12/07/06 15:45

as you can see this date/ time is in summer time

so in:

UK it is 16:45
Germany it is 17:45

Problem:

I just can't force my software to display these dates as I mentioned above.
All I can achieve with :

DateTimeToStr is UTC time
DateTimeToLocalDateTime from JclDateTime returns results depending on the time winter/ summer I have on my computer.

Thank you very much in advance

Jack :-)

0
Actia
Asked:
Actia
  • 4
  • 3
2 Solutions
 
2266180Commented:
usually, timing issues are solved in the following manner:
- a protocol is defined (choosen) that states that dates are in: GMT/UTC
- according to that protocol, a softeware client will always output that date in correct way by menas:
1) if it si UTC, just convert the date to user local time (from the OS)
2) if GMT, then the protocol also states that the server time zone is known. so the client will convert it to utc and from utc to local or directly.

Most such protocols I've seen use UTC.

after this protocol is decided and taken into account, you will get correct dates on your pc
0
 
ActiaAuthor Commented:
1) if it si UTC, just convert the date to user local time (from the OS)

that's what I do not know how to do :-)

Jack
0
 
2266180Commented:
oh :) sorry, guess I didn't udnerstand your problem :D
you can look at this article:
http://www.delphi3000.com/articles/article_2704.asp?SK=ftp 
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
atul_parmarCommented:
Try this.

var
  fst : TFormatSettings;
begin
  GetLocaleFormatSettings(GetSystemDefaultLCID, fst);
  ShowMessage(DateToStr(Now, fst));
end;
0
 
ActiaAuthor Commented:
oh :) sorry, guess I didn't udnerstand your problem :D
you can look at this article:
http://www.delphi3000.com/articles/article_2704.asp?SK=ftp 

*** it helped me a lot :-)

May I ask for an opposite function- something like:

SystemTimeToUTC? :-)

Jack
0
 
2266180Commented:
here are both functions, just to put them, together for future use

function UTCToSystemTime(UTC : TDateTime) : TDateTime;
var TimeZoneInf : _TIME_ZONE_INFORMATION;
    UTCTime,LocalTime: TSystemTime;
begin
  if GetTimeZoneInformation(TimeZoneInf) < $FFFFFFFF then
  begin
    DatetimetoSystemTime(UTC,UTCTime);
    if SystemTimeToTzSpecificLocalTime(@TimeZoneInf,UTCTime,LocalTime) then
    begin
     result := SystemTimeToDateTime(LocalTime);
    end else result := UTC;
  end else result := UTC;
end;

function TzSpecificLocalTimeToSystemTime(lpTimeZoneInformation: PTimeZoneInformation;
  var lpLocalTime, lpUniversalTime: TSystemTime): BOOL; stdcall; external kernel32 name 'TzSpecificLocalTimeToSystemTime';
{$EXTERNALSYM TzSpecificLocalTimeToSystemTime}

function SystemTimeToUTC(Sys : TDateTime):TDateTime;
var TimeZoneInf : _TIME_ZONE_INFORMATION;
    SysTime,LocalTime: TSystemTime;
begin
  if GetTimeZoneInformation(TimeZoneInf) < $FFFFFFFF then
  begin
    DatetimetoSystemTime(Sys, SysTime);
    if TzSpecificLocalTimeToSystemTime(@TimeZoneInf,SysTime,LocalTime) then
      result:=SystemTimeToDateTime(LocalTime)
    else result:=Sys;
  end else result:=Sys;
end;

0
 
ActiaAuthor Commented:
function TzSpecificLocalTimeToSystemTime(lpTimeZoneInformation: PTimeZoneInformation;
  var lpLocalTime, lpUniversalTime: TSystemTime): BOOL; stdcall; external kernel32 name 'TzSpecificLocalTimeToSystemTime';
{$EXTERNALSYM TzSpecificLocalTimeToSystemTime}

function SystemTimeToUTC(Sys : TDateTime):TDateTime;
var TimeZoneInf : _TIME_ZONE_INFORMATION;
    SysTime,LocalTime: TSystemTime;
begin
  if GetTimeZoneInformation(TimeZoneInf) < $FFFFFFFF then
  begin
    DatetimetoSystemTime(Sys, SysTime);
    if TzSpecificLocalTimeToSystemTime(@TimeZoneInf,SysTime,LocalTime) then
      result:=SystemTimeToDateTime(LocalTime)
    else result:=Sys;
  end else result:=Sys;
end;


***

above work fine :-) but only with XP.

Does not work with Win2000 :-(
(kernel32.dll does not have such a function)

i do not know if it works with Win98 SE- but probably not.

What to do now? :-)

Jack

0
 
2266180Commented:
hm ... if you know german, maybe there is some good info here: http://www.delphipraxis.net/topic51599.html
you will have to correct the last function to
function LocalDateTimeToUTC(aLocal : TDateTime) : TDateTime;
begin
  Result := IncMinute(aLocal, GetBiasForDate(aLocal) * -1);
end;

I tested it and it works on my xp box.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now