diff options
author | ondrej <ondrej@3ad0048d-3df7-0310-abae-a5850022a9f2> | 2020-11-04 09:34:08 +0000 |
---|---|---|
committer | ondrej <ondrej@3ad0048d-3df7-0310-abae-a5850022a9f2> | 2020-11-04 09:34:08 +0000 |
commit | acec531a66ef707364df5457ce89510f4920dd2e (patch) | |
tree | 92936474bf745bd7fe7624c6d350ba058d8e4604 /rtl | |
parent | fabbd37b94ca0c1c2c9f1e20f53312232f0e0a32 (diff) | |
download | fpc-acec531a66ef707364df5457ce89510f4920dd2e.tar.gz |
* introduce NowUTC() and GetUniversalTime()
git-svn-id: https://svn.freepascal.org/svn/fpc/trunk@47306 3ad0048d-3df7-0310-abae-a5850022a9f2
Diffstat (limited to 'rtl')
-rw-r--r-- | rtl/objpas/sysutils/dati.inc | 16 | ||||
-rw-r--r-- | rtl/objpas/sysutils/datih.inc | 2 | ||||
-rw-r--r-- | rtl/unix/sysutils.pp | 19 | ||||
-rw-r--r-- | rtl/win/sysutils.pp | 6 | ||||
-rw-r--r-- | rtl/wince/sysutils.pp | 6 |
5 files changed, 49 insertions, 0 deletions
diff --git a/rtl/objpas/sysutils/dati.inc b/rtl/objpas/sysutils/dati.inc index 816f0feab7..fa3f1e13d7 100644 --- a/rtl/objpas/sysutils/dati.inc +++ b/rtl/objpas/sysutils/dati.inc @@ -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; diff --git a/rtl/objpas/sysutils/datih.inc b/rtl/objpas/sysutils/datih.inc index 8a25162274..46a199be19 100644 --- a/rtl/objpas/sysutils/datih.inc +++ b/rtl/objpas/sysutils/datih.inc @@ -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; diff --git a/rtl/unix/sysutils.pp b/rtl/unix/sysutils.pp index 256931fc8b..27cabc43ac 100644 --- a/rtl/unix/sysutils.pp +++ b/rtl/unix/sysutils.pp @@ -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 diff --git a/rtl/win/sysutils.pp b/rtl/win/sysutils.pp index c68aac4ff4..fe8c1dd787 100644 --- a/rtl/win/sysutils.pp +++ b/rtl/win/sysutils.pp @@ -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 diff --git a/rtl/wince/sysutils.pp b/rtl/wince/sysutils.pp index 09ce7c5257..80e1e05181 100644 --- a/rtl/wince/sysutils.pp +++ b/rtl/wince/sysutils.pp @@ -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; |