* introduce NowUTC() and GetUniversalTime()

git-svn-id: trunk@47306 -
This commit is contained in:
ondrej 2020-11-04 09:34:08 +00:00
parent 7ad4694e4b
commit 934f2dc55d
5 changed files with 49 additions and 0 deletions

View File

@ -271,6 +271,17 @@ begin
result := systemTimeToDateTime(SystemTime);
end;
{ NowUTC returns the current UTC Date and Time if available on the OS. If not, local date is returned }
function NowUTC: TDateTime;
var
SystemTime: TSystemTime;
begin
if not GetUniversalTime(SystemTime) then
GetLocalTime(SystemTime);
result := systemTimeToDateTime(SystemTime);
end;
{ IncMonth increments DateTime with NumberOfMonths months,
NumberOfMonths can be less than zero }
@ -1529,6 +1540,11 @@ function GetLocalTimeOffset(const DateTime: TDateTime; const InputIsUTC: Boolean
begin
Result:=False;
end;
function GetUniversalTime(var SystemTime: TSystemTime): Boolean;
begin
Result:=False;
end;
{$ENDIF}
function GetLocalTimeOffset(const DateTime: TDateTime; const InputIsUTC: Boolean): Integer;

View File

@ -128,6 +128,7 @@ function DayOfWeek(DateTime: TDateTime): integer;
function Date: TDateTime;
function Time: TDateTime;
function Now: TDateTime;
function NowUTC: TDateTime;
function IncMonth(const DateTime: TDateTime; NumberOfMonths: integer = 1 ): TDateTime;
procedure IncAMonth(var Year, Month, Day: Word; NumberOfMonths: Integer = 1);
function IsLeapYear(Year: Word): boolean;
@ -195,6 +196,7 @@ function StrToDateTimeDef(const S: AnsiString; const Defvalue : TDateTime; const
function CurrentYear:Word;
{ FPC Extra }
Procedure GetLocalTime(var SystemTime: TSystemTime);
function GetUniversalTime(var SystemTime: TSystemTime): Boolean;
procedure ReplaceTime(var dati:TDateTime; NewTime : TDateTime); inline;
procedure ReplaceDate(var DateTime: TDateTime; const NewDate: TDateTime); inline;

View File

@ -1176,6 +1176,17 @@ begin
GetEpochTime:=fptime;
end;
Procedure DoGetUniversalDateTime(var year, month, day, hour, min, sec, msec, usec : word);
var
tz:timeval;
begin
fpgettimeofday(@tz,nil);
EpochToUniversal(tz.tv_sec,year,month,day,hour,min,sec);
msec:=tz.tv_usec div 1000;
usec:=tz.tv_usec mod 1000;
end;
// Now, adjusted to local time.
Procedure DoGetLocalDateTime(var year, month, day, hour, min, sec, msec, usec : word);
@ -1614,6 +1625,14 @@ begin
Flush(Output);
end;
function GetUniversalTime(var SystemTime: TSystemTime): Boolean;
var
usecs : Word;
begin
DoGetUniversalDateTime(SystemTime.Year, SystemTime.Month, SystemTime.Day,SystemTime.Hour, SystemTime.Minute, SystemTime.Second, SystemTime.MilliSecond, usecs);
Result:=True;
end;
function GetLocalTimeOffset: Integer;
begin

View File

@ -813,6 +813,12 @@ begin
windows.Getlocaltime(SystemTime);
end;
function GetUniversalTime(var SystemTime: TSystemTime): Boolean;
begin
windows.GetSystemTime(SystemTime);
Result:=True;
end;
function GetLocalTimeOffset: Integer;
var

View File

@ -444,6 +444,12 @@ begin
windows.Getlocaltime(SystemTime);
end;
function GetUniversalTime(var SystemTime: TSystemTime): Boolean;
begin
windows.GetSystemTime(SystemTime);
Result:=True;
end;
function GetLocalTimeOffset: Integer;
var
TZInfo: TTimeZoneInformation;