X-Git-Url: http://www.lcore.org/git/lcore.git/blobdiff_plain/6cb6b7ede2d178e03fa817bc28474c175f5a93b9..42a61c59a81b03130f61e805474198eada033cd8:/httpserver_20080306/btime.pas?ds=sidebyside diff --git a/httpserver_20080306/btime.pas b/httpserver_20080306/btime.pas deleted file mode 100755 index 127839e..0000000 --- a/httpserver_20080306/btime.pas +++ /dev/null @@ -1,362 +0,0 @@ -{ Copyright (C) 2005 Bas Steendijk and Peter Green - For conditions of distribution and use, see copyright notice in zlib_license.txt - which is included in the package - ----------------------------------------------------------------------------- } -{ -this unit returns unix timestamp with seconds and microseconds (as float) -works on windows/delphi, and on freepascal on unix. -} - -unit btime; - -interface - -type - float=extended; - -var - timezone:integer; - timezonestr:string; - irctime,unixtime:integer; - tickcount:integer; - settimebias:integer; - qpcjump:float; {can be read out and reset for debug purpose} - performancecountfreq:extended; - -function irctimefloat:float; -function irctimeint:integer; - -function unixtimefloat:float; -function unixtimeint:integer; - -function wintimefloat:float; - -procedure settime(newtime:integer); -procedure gettimezone; -procedure timehandler; -procedure init; - -function timestring(i:integer):string; -function timestrshort(i:integer):string; - -function oletounixfloat(t:float):float; -function oletounix(t:tdatetime):integer; -function unixtoole(i:integer):tdatetime; - -var - timefloatbias:float; - lastunixtimefloat:float=0; - -implementation - -{$ifdef fpc} - {$mode delphi} -{$endif} - -uses - {$ifdef UNIX} - {$ifdef VER1_0} - linux, - {$else} - baseunix,unix,unixutil, {needed for 2.0.2} - {$endif} - {$else} - windows, - {$endif} - sysutils; - - {$include unixstuff.inc} - - -const - daysdifference=25569; - -function oletounixfloat(t:float):float; -begin - t := (t - daysdifference) * 86400; - result := t; -end; - -function oletounix(t:tdatetime):integer; -begin - result := trunc(oletounixfloat(t)); -end; - -function unixtoole(i:integer):tdatetime; -begin - result := ((i)/86400)+daysdifference; -end; - -{$ifdef unix} -{-----------------------------------------*nix/freepascal code to read time } - -function unixtimefloat:float; -var - tv:ttimeval; -begin - gettimeofday(tv); - result := tv.tv_sec+(tv.tv_usec/1000000); -end; - -function wintimefloat:extended; -begin - result := unixtimefloat; -end; - -function unixtimeint:integer; -var - tv:ttimeval; -begin - gettimeofday(tv); - result := tv.tv_sec; -end; - -{$else} {delphi 3} -{------------------------------ windows/delphi code to read time} - -{ free pascals tsystemtime is incomaptible with windows api calls - so we declare it ourselves - plugwash -} -{$ifdef fpc} -type - TSystemTime = record - wYear: Word; - wMonth: Word; - wDayOfWeek: Word; - wDay: Word; - wHour: Word; - wMinute: Word; - wSecond: Word; - wMilliseconds: Word; - end; - {$endif} -function Date_utc: extended; -var - SystemTime: TSystemTime; -begin - {$ifdef fpc} - GetsystemTime(@SystemTime); - {$else} - GetsystemTime(SystemTime); - {$endif} - with SystemTime do Result := EncodeDate(wYear, wMonth, wDay); -end; - -function Time_utc: extended; -var - SystemTime: TSystemTime; -begin - {$ifdef fpc} - GetsystemTime(@SystemTime); - {$else} - GetsystemTime(SystemTime); - {$endif} - with SystemTime do - Result := EncodeTime(wHour, wMinute, wSecond, wMilliSeconds); -end; - -function Now_utc: extended; -begin - Result := round(Date_utc) + Time_utc; -end; - -const - highdwordconst=4294967296.0; - -function wintimefloat:extended; -var - p:packed record - lowpart:longint; - highpart:longint - end; - p2:tlargeinteger absolute p; - e:extended; -begin - if performancecountfreq = 0 then begin - QueryPerformancefrequency(p2); - e := p.lowpart; - if e < 0 then e := e + highdwordconst; - performancecountfreq := ((p.highpart*highdwordconst)+e); - end; - queryperformancecounter(p2); - e := p.lowpart; - if e < 0 then e := e + highdwordconst; - result := ((p.highpart*highdwordconst)+e)/performancecountfreq; -end; - -var - classpriority,threadpriority:integer; - -procedure settc; -var - hprocess,hthread:integer; -begin - hProcess := GetCurrentProcess; - hThread := GetCurrentThread; - - ClassPriority := GetPriorityClass(hProcess); - ThreadPriority := GetThreadPriority(hThread); - - SetPriorityClass(hProcess, REALTIME_PRIORITY_CLASS); - SetThreadPriority(hThread, THREAD_PRIORITY_TIME_CRITICAL); -end; - -procedure unsettc; -var - hprocess,hthread:integer; -begin - hProcess := GetCurrentProcess; - hThread := GetCurrentThread; - - SetPriorityClass(hProcess, ClassPriority); - SetThreadPriority(hThread, ThreadPriority); -end; - -function unixtimefloat:float; -var - f,g,h:float; -begin - if timefloatbias = 0 then begin - settc; - f := now_utc; - repeat g := now_utc; h := wintimefloat until g > f; - timefloatbias := oletounixfloat(g)-h; - unsettc; - end; - result := wintimefloat+timefloatbias; - - { - workaround for QPC jumps - (approach 2: always check "hi res" QPC unixtime against the "guaranteed" systemtime one) - } - f := result-(oletounixfloat(now_utc)); - if abs(f) > 0.02 then begin - f := timefloatbias; - timefloatbias := 0; - result := unixtimefloat; - qpcjump := qpcjump + f - timefloatbias; - end; - - if (result <= lastunixtimefloat) then result := lastunixtimefloat + 0.0000001; - lastunixtimefloat := result; -end; - -function unixtimeint:integer; -begin - result := trunc(unixtimefloat); -end; - -{$endif} -{-----------------------------------------------end of platform specific} - -function irctimefloat:float; -begin - result := unixtimefloat+settimebias; -end; - -function irctimeint:integer; -begin - result := unixtimeint+settimebias; -end; - - -procedure settime(newtime:integer); -var - a:integer; -begin - a := irctimeint-settimebias; - if newtime = 0 then settimebias := 0 else settimebias := newtime-a; - - irctime := irctimeint; -end; - -procedure timehandler; -begin - if unixtime = 0 then init; - unixtime := unixtimeint; - irctime := irctimeint; - if unixtime and 63 = 0 then begin - {update everything, apply timezone changes, clock changes, etc} - gettimezone; - timefloatbias := 0; - unixtime := unixtimeint; - irctime := irctimeint; - end; -end; - - -procedure gettimezone; -var - {$ifdef UNIX} - {$ifndef ver1_9_4} - {$ifndef ver1_0} - {$define above194} - {$endif} - {$endif} - {$ifndef above194} - hh,mm,ss:word; - {$endif} - {$endif} - l:integer; -begin - {$ifdef UNIX} - {$ifdef above194} - timezone := tzseconds; - {$else} - gettime(hh,mm,ss); - timezone := (longint(hh) * 3600 + mm * 60 + ss) - (unixtimeint mod 86400); - {$endif} - {$else} - timezone := round((now-now_utc)*86400); - {$endif} - - while timezone > 43200 do dec(timezone,86400); - while timezone < -43200 do inc(timezone,86400); - - if timezone >= 0 then timezonestr := '+' else timezonestr := '-'; - l := abs(timezone) div 60; - timezonestr := timezonestr + char(l div 600 mod 10+48)+char(l div 60 mod 10+48)+':'+char(l div 10 mod 6+48)+char(l mod 10+48); -end; - -function timestrshort(i:integer):string; -const - weekday:array[0..6] of string[4]=('Thu','Fri','Sat','Sun','Mon','Tue','Wed'); - month:array[0..11] of string[4]=('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'); -var - y,m,d,h,min,sec,ms:word; - t:tdatetime; -begin - t := unixtoole(i+timezone); - decodedate(t,y,m,d); - decodetime(t,h,min,sec,ms); - result := weekday[(i+timezone) div 86400 mod 7]+' '+month[m-1]+' '+inttostr(d)+' '+ - inttostr(h div 10)+inttostr(h mod 10)+':'+inttostr(min div 10)+inttostr(min mod 10)+':'+inttostr(sec div 10)+inttostr(sec mod 10)+' '+ - inttostr(y); -end; - -function timestring(i:integer):string; -const - weekday:array[0..6] of string[10]=('Thursday','Friday','Saturday','Sunday','Monday','Tuesday','Wednesday'); - month:array[0..11] of string[10]=('January','February','March','April','May','June','July','August','September','October','November','December'); -var - y,m,d,h,min,sec,ms:word; - t:tdatetime; -begin - t := unixtoole(i+timezone); - decodedate(t,y,m,d); - decodetime(t,h,min,sec,ms); - result := weekday[(i+timezone) div 86400 mod 7]+' '+month[m-1]+' '+inttostr(d)+' '+inttostr(y)+' -- '+ - inttostr(h div 10)+inttostr(h mod 10)+':'+inttostr(min div 10)+inttostr(min mod 10)+':'+inttostr(sec div 10)+inttostr(sec mod 10)+' '+ - timezonestr; -end; - -procedure init; -begin - qpcjump := 0; - settimebias := 0; - gettimezone; - unixtime := unixtimeint; - irctime := irctimeint; -end; - -end.