Mike at Bookup
Mike at Bookup

Reputation: 1314

Delphi Firemonkey get UTC time

How does one get the UTC time in Firemonkey?

I tried this code from another Stackoverflow answer but it appears GetSystemTime() is not available in FMX.

function NowUTC: TDateTime;
Var UTC: TSystemTime;
begin
  GetSystemTime(UTC);
  Result := SystemTimeToDateTime(UTC);
end;

Upvotes: 3

Views: 1334

Answers (2)

xjikka
xjikka

Reputation: 9

TTimeZone.Local.ToUniversalTime(Now) doesn't work during DST change on 30. October 3:00 >> 2:00. Beacause during changing, there is 2:00-3:00 time twice and to get correct UTC, You have to set ForceDaylight in function ToUniversalTime to true/false during transient time.

There is a TDateTime helper in DateUtils unit, which reflects this DST transient time on a UNIX (Android) system too and return the correct UTC time. On Unix are used functions gettimeofday and gmtime_r.

result:=TDateTime.NowUTC;

Upvotes: 0

Andreas Rejbrand
Andreas Rejbrand

Reputation: 108948

If you add DateUtils to the uses clause, you can use the TTimeZone class, its Local class property, and the ToUniversalTime method:

ShowMessage(DateTimeToStr(TTimeZone.Local.ToUniversalTime(Now)));

Upvotes: 9

Related Questions