Reputation: 1314
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
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
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