From 4600e3eb2f686d1842bd7a46f10de292a92a6d40 Mon Sep 17 00:00:00 2001 From: sekelsenmat Date: Sun, 6 Nov 2011 19:31:31 +0000 Subject: [PATCH] Moves GetTickCount to LazUtils and adds NowUTC to LazUtils git-svn-id: trunk@33381 - --- .gitattributes | 1 + components/lazutils/lazutf8sysutils.pas | 122 ++++++++++++++++++++++++ components/lazutils/lazutils.lpk | 14 ++- components/lazutils/lazutils.pas | 2 +- lcl/lclintf.pas | 33 +------ 5 files changed, 134 insertions(+), 38 deletions(-) create mode 100644 components/lazutils/lazutf8sysutils.pas diff --git a/.gitattributes b/.gitattributes index 3bbadc77ee..a296a8d2c2 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1761,6 +1761,7 @@ components/lazutils/lazfileutils.pas svneol=native#text/plain components/lazutils/lazutf16.pas svneol=native#text/pascal components/lazutils/lazutf8.pas svneol=native#text/plain components/lazutils/lazutf8classes.pas svneol=native#text/pascal +components/lazutils/lazutf8sysutils.pas svneol=native#text/plain components/lazutils/lazutils.lpk svneol=native#text/plain components/lazutils/lazutils.pas svneol=native#text/plain components/lazutils/lazutilsstrconsts.pas svneol=native#text/pascal diff --git a/components/lazutils/lazutf8sysutils.pas b/components/lazutils/lazutf8sysutils.pas new file mode 100644 index 0000000000..9491c6aaa9 --- /dev/null +++ b/components/lazutils/lazutf8sysutils.pas @@ -0,0 +1,122 @@ +unit lazutf8sysutils; + +{$mode objfpc}{$H+} + +interface + +uses + SysUtils; + +function NowUTC: TDateTime; +function GetTickCount: Int64; + +implementation + +uses + Classes, + {$ifdef Windows} + Windows + {$else} + Unix, BaseUnix, UnixUtil + {$endif} + ; + +// ToDo: Move the code to 1 include file per platform +{$IFDEF WINDOWS} +function NowUTC: TDateTime; +var + SystemTime: TSystemTime; +begin + windows.GetSystemTime(SystemTime); + result := systemTimeToDateTime(SystemTime); +end; + +// GetTickCount64 is better, but we need to check the Windows version to use it +function GetTickCount: Int64; +begin + Result := Windows.GetTickCount(); +end; + +{$else} +{$ifdef UNIX} +Const +{Date Translation} + C1970=2440588; + D0 = 1461; + D1 = 146097; + D2 =1721119; + +Procedure JulianToGregorian(JulianDN:LongInt;Var Year,Month,Day:Word); +Var + YYear,XYear,Temp,TempMonth : LongInt; +Begin + Temp:=((JulianDN-D2) shl 2)-1; + JulianDN:=Temp Div D1; + XYear:=(Temp Mod D1) or 3; + YYear:=(XYear Div D0); + Temp:=((((XYear mod D0)+4) shr 2)*5)-3; + Day:=((Temp Mod 153)+5) Div 5; + TempMonth:=Temp Div 153; + If TempMonth>=10 Then + Begin + inc(YYear); + dec(TempMonth,12); + End; + inc(TempMonth,3); + Month := TempMonth; + Year:=YYear+(JulianDN*100); +end; + + + +Procedure EpochToLocal(epoch:longint;var year,month,day,hour,minute,second:Word); +{ + Transforms Epoch time into local time (hour, minute,seconds) +} +Var + DateNum: LongInt; +Begin + inc(Epoch,TZSeconds); + Datenum:=(Epoch Div 86400) + c1970; + JulianToGregorian(DateNum,Year,Month,day); + Epoch:=Abs(Epoch Mod 86400); + Hour:=Epoch Div 3600; + Epoch:=Epoch Mod 3600; + Minute:=Epoch Div 60; + Second:=Epoch Mod 60; +End; + +function NowUTC: TDateTime; +var + tz:timeval; + SystemTime: TSystemTime; +begin + fpgettimeofday(@tz,nil); + EpochToLocal(tz.tv_sec,SystemTime.year,SystemTime.month,SystemTime.day,SystemTime.hour,SystemTime.Minute,SystemTime.Second); + SystemTime.MilliSecond:=tz.tv_usec div 1000; + result := systemTimeToDateTime(SystemTime); +end; + +function GetTickCount: Int64; +var + tp: TTimeVal; +begin + fpgettimeofday(@tp, nil); + Result := (tp.tv_sec * 1000) + (tp.tv_usec div 1000); +end; +{$else} +// Not Windows and not UNIX, so just write the most trivial code until we have something besser: +function NowUTC: TDateTime; +begin + Result := Now; +end; + +function GetTickCount: Int64; +begin + Result := Trunc(Now * 24 * 60 * 60 * 1000); +end; +{$endif} +{$endif} + +end. + diff --git a/components/lazutils/lazutils.lpk b/components/lazutils/lazutils.lpk index cfb23a3042..617e467dae 100644 --- a/components/lazutils/lazutils.lpk +++ b/components/lazutils/lazutils.lpk @@ -4,16 +4,10 @@ - + - - - - - - @@ -25,7 +19,7 @@ - + @@ -138,6 +132,10 @@ + + + + diff --git a/components/lazutils/lazutils.pas b/components/lazutils/lazutils.pas index 3f7d4b95d4..edc896dc9b 100644 --- a/components/lazutils/lazutils.pas +++ b/components/lazutils/lazutils.pas @@ -11,7 +11,7 @@ uses Laz_XMLCfg, Laz_XMLRead, Laz_XMLStreaming, Laz_XMLWrite, LazFileUtils, LazFileCache, LUResStrings, LazUTF8, LazDbgLog, paswstring, FileUtil, lazutf8classes, Masks, LazUtilsStrConsts, LConvEncoding, lazutf16, - LazarusPackageIntf; + lazutf8sysutils, LazarusPackageIntf; implementation diff --git a/lcl/lclintf.pas b/lcl/lclintf.pas index 171868ee4f..3e830a7602 100644 --- a/lcl/lclintf.pas +++ b/lcl/lclintf.pas @@ -53,7 +53,7 @@ uses {$IFDEF UNIX}Unix, {$ENDIF} {$IFDEF Darwin}MacOSAll, {$ENDIF} Types, Math, Classes, SysUtils, LCLType, LCLProc, GraphType, InterfaceBase, - LResources, FileUtil, UTF8Process, Maps, LMessages; + LResources, FileUtil, UTF8Process, Maps, LMessages, lazutf8sysutils; {$ifdef Trace} {$ASSERTIONS ON} @@ -71,18 +71,7 @@ function PredefinedClipboardFormat( function MsgKeyDataToShiftState(KeyData: PtrInt): TShiftState; - -{$IFDEF WINDOWS} - -{$IFDEF MSWindows} -function GetTickCount:DWORD; stdcall; external 'kernel32.dll' name 'GetTickCount'; -{$ELSE} -function GetTickCount:DWORD; stdcall; external KernelDLL name 'GetTickCount'; -{$ENDIF} - -{$ELSE} -function GetTickCount: DWord; -{$ENDIF} +function GetTickCount(): Int64; {$IFDEF DebugLCL} function GetTickStep: DWord; @@ -147,24 +136,10 @@ begin end; end; - -{$IFNDEF WINDOWS} -function GetTickCount: DWord; -{$IFDEF UNIX} -var - tp: TTimeVal; -{$ENDIF} +function GetTickCount(): Int64; begin - {$IFDEF UNIX} - if fpgettimeofday(@tp, nil) = 0 then - Result := DWord((tp.tv_sec * 1000) + (tp.tv_usec div 1000)) - else - Result := DWord(Trunc(Now * 24 * 60 * 60 * 1000)); - {$ELSE} - Result := DWord(Trunc(Now * 24 * 60 * 60 * 1000)); - {$ENDIF} + Result := lazutf8sysutils.GetTickCount(); end; -{$ENDIF} {$IFDEF DebugLCL} var