⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 udf.pas

📁 数据库封装原代码 1. 该代码可以免费使用, 该代码的名字暂时定为"哲别" 2. 如果你需要使用该代码, 请注明该代码的原来作者: Jacky Zhou 3. 如果你发现该代码有bug,可以自己修改
💻 PAS
字号:
unit UDF;


interface

uses
  Windows, StrUtils, Math, DateUtils, IdGlobal;

const  
  //string
  NOT_FIND = -1;
  NUMBER_NOTCACULATED = 0;
  ZERO_TIME = '00:00:00';

type
  TTime = record
    nTime, nHour, nMinute, nSecond : integer;
    strTime : string;
  end;
  PTTime = ^TTime;

  TAryStr = class
    ary : array of string;
    nCount : Integer;

    procedure clear();
  end;

  //file functions
  function GetAppPath() : String;

  //number functions
  function IsNumber(const str : String) : Boolean;

  //string functions
  function Seek(const p1 : PChar; const p2 : PChar; const nBegin : integer = 0) : Integer;
  function Decompose(const p : PChar; const strFlag : String; aryStr : TAryStr) : Boolean;
  function SubStrEx(const strSource : String; const strFlag : String; const bFront : boolean = false; const bFirst : boolean = false) : String;

  //os functions
  function SetPrivilege(const privilegeName: string; const enable: boolean): boolean;
  Procedure SystemShutdown;
  function GetTime() : string;
  function InitTime(ptTime : PTTime) : boolean;
  function GetIntervalTime(const strBeginTime, strEndTime : string) : TTime; overload;
  function GetIntervalTime(const dtBeginTime, dtEndTime : TDateTime) : TTime; overload;
  function AddTime(const strTime1, strTime2 : string) : TTime;


implementation

uses
  SysUtils;

//-----------------------------------------------------------------------------
//                                   clear
//-----------------------------------------------------------------------------
procedure TAryStr.clear();
begin
  self.nCount := 0;
  SetLength(self.ary, 0);
end;

//------------------------------------------------------------------------------
//                               GetAppPath
//------------------------------------------------------------------------------
function GetAppPath() : string;
var
  nPos : integer;
  tchAppPath : array[0..MAX_PATH - 1] of char;
  strAppPath : string;
begin
  //get app path
  if (GetModuleFileName(0, tchAppPath, MAX_PATH) = 0) then
  begin
    result := '';
    exit;
  end;

  strAppPath := tchAppPath;
  strAppPath := ReverseString(strAppPath);
  nPos := Pos('\', strAppPath);
  System.Delete(strAppPath, 1, nPos - 1);
  strAppPath := ReverseString(strAppPath);

  result := strAppPath;
end;

//-----------------------------------------------------------------------------
//                               IsNumber
//-----------------------------------------------------------------------------
function IsNumber(const str : String) : Boolean;
var
  i, nLen : Integer;
  cChar : Char;
begin
  nLen := Length(trim(str));
  if nLen = 0 then
  begin
    Result := false;
    Exit;
  end;

  for i := 1 to nLen do
  begin
    cChar := str[i];

    if cChar in ['0','1','2','3','4','5','6','7','8','9'] then
    else
    begin
      Result := false;
      Exit;
    end;
  end;

  Result := true;
end;

//-----------------------------------------------------------------------------
//                               Seek
//-----------------------------------------------------------------------------
function Seek(const p1 : PChar; const p2 : PChar; const nBegin : integer = 0) : Integer;
var
  i, j : Integer;
  nLen1, nLen2 : Integer;
begin
  nLen1 := Length(String(p1));
  nLen2 := Length(String(p2));

  i := nBegin;
  j := 0;
  while i < nLen1 do
  begin
    if p1[i] = p2[j] then
    begin
      if j = nLen2 -1 then
      begin
        Result := i - j;
        exit;
      end;

      i := i + 1;
      j := j + 1;
    end
    else
    begin
      i := i - j + 1;
      j := 0;
    end;
  end;

  Result := NOT_FIND;
end;

//-----------------------------------------------------------------------------
//                               Decompose
//-----------------------------------------------------------------------------
function Decompose(const p : PChar; const strFlag : String; aryStr : TAryStr) : Boolean;
var
  s : string;
  i, nLen, nCount : Integer;
begin
  nLen := length(string(p));

  i := 0;
  nCount := 0;
  while i < nLen do
  begin
    if (p[i] <> strFlag) and (i + 1 <> nLen) then
      s := s + p[i]
    else
    begin
      nCount := nCount + 1;
      SetLength(aryStr.ary, nCount);
      aryStr.nCount := nCount;

      if i + 1 <> nLen then
        aryStr.ary[nCount - 1] := s
      else
        aryStr.ary[nCount - 1] := s + p[i];

      s := '';
    end;

    i := i + 1;
  end;

  Result := true;
end;

//-----------------------------------------------------------------------------
//                            SubStrEx
//-----------------------------------------------------------------------------
function SubStrEx(const strSource : String; const strFlag : String; const bFront : boolean = false; const bFirst : boolean = false) : String;
var
  str : string;
  nPos : integer;
begin
  str := strSource;

  if (bFirst) then
  begin
    nPos := Pos(strFlag, str);

    if (bFront) then
      System.Delete(str, nPos, integer(strlen(PChar(str))) - nPos + 1)
    else
      System.Delete(str, 1, nPos);
  end
  else
  begin
    str := ReverseString(strSource);

    nPos := Pos(strFlag, str);

    if (bFront) then
      System.Delete(str, 1, nPos)
    else
      System.Delete(str, nPos, integer(strlen(PChar(str))) - nPos + 1);

    str := ReverseString(str);
  end;

  Result := str;
end;

//-----------------------------------------------------------------------------
//                                   SetPrivilege
//-----------------------------------------------------------------------------
function SetPrivilege(const privilegeName: string; const enable: boolean): boolean;
var
    tpPrev,
    tp         : TTokenPrivileges;
    token      : THandle;
    dwRetLen   : DWord;
begin
//  if GetOSVerInfo = 'WIN_9X' then
//  begin
//    result := True;
//    exit;
//  end;

  result := False;
  OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, token);

  tp.PrivilegeCount := 1;
  if LookupPrivilegeValue(nil, pchar(privilegeName), tp.Privileges[0].LUID) then
  begin
    if enable then
      tp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED
    else
      tp.Privileges[0].Attributes := 0;
    dwRetLen := 0;
    result := AdjustTokenPrivileges(token, False, tp, SizeOf(tpPrev), tpPrev, dwRetLen);
  end;

  CloseHandle(token);
end;


//-----------------------------------------------------------------------------
//                                   SystemShutdown
//-----------------------------------------------------------------------------
Procedure SystemShutdown;
begin
  SetPrivilege('SeSHUTDOWNPrivilege', true);
  ExitWindowsEx(EWX_SHUTDOWN,0); //正常关闭
end;

//-----------------------------------------------------------------------------
//                                   GetTime
//-----------------------------------------------------------------------------
function GetTime() : string;
begin
  Result := FormatDateTime('hh:mm:ss', Now);
end;

//-----------------------------------------------------------------------------
//                                   InitTime
//-----------------------------------------------------------------------------
function InitTime(ptTime : PTTime) : boolean;
begin
  ptTime^.nTime := 0;
  ptTime^.nHour := 0;
  ptTime^.nMinute := 0;
  ptTime^.nSecond := 0;
  ptTime^.strTime := ZERO_TIME;

  result := true;
end;

//-----------------------------------------------------------------------------
//                                   GetIntervalTime
//-----------------------------------------------------------------------------
//it only get time in 24 hours
function GetIntervalTime(const strBeginTime, strEndTime : string) : TTime;
var
  stuBeginTime, stuEndTime, stuIntervalTime : TTime;
  nResult, nRemainder : Word;
begin
  InitTime(@stuBeginTime);
  InitTime(@stuEndTime);
  InitTime(@stuIntervalTime);

  stuBeginTime.nHour := StrToInt(copy(strBeginTime, 1, 2));
  stuBeginTime.nMinute := StrToInt(copy(strBeginTime, 4, 2));
  stuBeginTime.nSecond := StrToInt(copy(strBeginTime, 7, 2));
  stuBeginTime.nTime := stuBeginTime.nHour * 3600 + stuBeginTime.nMinute * 60 + stuBeginTime.nSecond;

  stuEndTime.nHour := StrToInt(copy(strEndTime, 1, 2));
  stuEndTime.nMinute := StrToInt(copy(strEndTime, 4, 2));
  stuEndTime.nSecond := StrToInt(copy(strEndTime, 7, 2));
  if (stuEndTime.nHour < stuBeginTime.nHour) then
    stuEndTime.nHour := stuEndTime.nHour + 24;
  stuEndTime.nTime := stuEndTime.nHour * 3600 + stuEndTime.nMinute * 60 + stuEndTime.nSecond;

  stuIntervalTime.nTime := stuEndTime.nTime - stuBeginTime.nTime;
  DivMod(stuIntervalTime.nTime, 3600, nResult, nRemainder);
  stuIntervalTime.nHour := nResult;

  DivMod(nRemainder, 60, nResult, nRemainder);
  stuIntervalTime.nMinute := nResult;

  stuIntervalTime.nSecond := nRemainder;

  if (stuIntervalTime.nHour < 10) then
    stuIntervalTime.strTime := '0' + IntToStr(stuIntervalTime.nHour) + ':'
  else
    stuIntervalTime.strTime := IntToStr(stuIntervalTime.nHour) + ':';

  if (stuIntervalTime.nMinute < 10) then
    stuIntervalTime.strTime := stuIntervalTime.strTime + '0' + IntToStr(stuIntervalTime.nMinute) + ':'
  else
    stuIntervalTime.strTime := stuIntervalTime.strTime + IntToStr(stuIntervalTime.nMinute) + ':';

  if (stuIntervalTime.nSecond < 10) then
    stuIntervalTime.strTime := stuIntervalTime.strTime + '0' + IntToStr(stuIntervalTime.nSecond)
  else
    stuIntervalTime.strTime := stuIntervalTime.strTime + IntToStr(stuIntervalTime.nSecond);

  Result := stuIntervalTime;
end;

//-----------------------------------------------------------------------------
//                                   GetIntervalTime
//-----------------------------------------------------------------------------
function GetIntervalTime(const dtBeginTime, dtEndTime : TDateTime) : TTime;
var
  stuIntervalTime : TTime;
begin
  InitTime(@stuIntervalTime);

  stuIntervalTime.nTime := SecondsBetween(dtEndTime, dtBeginTime);
  stuIntervalTime.nHour := stuIntervalTime.nTime div 3600;
  stuIntervalTime.nMinute := (stuIntervalTime.nTime - stuIntervalTime.nHour * 3600) div 60;
  stuIntervalTime.nSecond := stuIntervalTime.nTime - stuIntervalTime.nHour * 3600 - stuIntervalTime.nMinute * 60;

  if (stuIntervalTime.nHour < 10) then
    stuIntervalTime.strTime := '0' + IntToStr(stuIntervalTime.nHour) + ':'
  else
    stuIntervalTime.strTime := IntToStr(stuIntervalTime.nHour) + ':';

  if (stuIntervalTime.nMinute < 10) then
    stuIntervalTime.strTime := stuIntervalTime.strTime + '0' + IntToStr(stuIntervalTime.nMinute) + ':'
  else
    stuIntervalTime.strTime := stuIntervalTime.strTime + IntToStr(stuIntervalTime.nMinute) + ':';

  if (stuIntervalTime.nSecond < 10) then
    stuIntervalTime.strTime := stuIntervalTime.strTime + '0' + IntToStr(stuIntervalTime.nSecond)
  else
    stuIntervalTime.strTime := stuIntervalTime.strTime + IntToStr(stuIntervalTime.nSecond);

  Result := stuIntervalTime;
end;

//-----------------------------------------------------------------------------
//                                   AddTime
//-----------------------------------------------------------------------------
function AddTime(const strTime1, strTime2 : string) : TTime;
var
  aryStr : TAryStr;
  stuTime1, stuTime2, stuTime : TTime;
  nResult, nRemainder : Word;
begin
  InitTime(@stuTime1);
  InitTime(@stuTime2);
  InitTime(@stuTime);

  //get time1
  aryStr := TAryStr.Create;
  aryStr.clear;
  Decompose(PChar(strTime1), ':', aryStr);
  if aryStr.nCount <> 3 then
    exit;
  stuTime1.nHour := StrToInt(aryStr.ary[0]);
  stuTime1.nMinute := StrToInt(aryStr.ary[1]);
  stuTime1.nSecond := StrToInt(aryStr.ary[2]);
  stuTime1.nTime := stuTime1.nHour * 3600 + stuTime1.nMinute * 60 + stuTime1.nSecond;

  //get time2
  aryStr.clear;
  Decompose(PChar(strTime2), ':', aryStr);
  if aryStr.nCount <> 3 then
    exit;
  stuTime2.nHour := StrToInt(aryStr.ary[0]);
  stuTime2.nMinute := StrToInt(aryStr.ary[1]);
  stuTime2.nSecond := StrToInt(aryStr.ary[2]);
  stuTime2.nTime := stuTime2.nHour * 3600 + stuTime2.nMinute * 60 + stuTime2.nSecond;

  //add time
  stuTime.nTime := stuTime2.nTime + stuTime1.nTime;
  DivMod(stuTime.nTime, 3600, nResult, nRemainder);
  stuTime.nHour := nResult;

  DivMod(nRemainder, 60, nResult, nRemainder);
  stuTime.nMinute := nResult;

  stuTime.nSecond := nRemainder;

  //format time
  if (stuTime.nHour < 10) then
    stuTime.strTime := '0' + IntToStr(stuTime.nHour) + ':'
  else
    stuTime.strTime := IntToStr(stuTime.nHour) + ':';

  if (stuTime.nMinute < 10) then
    stuTime.strTime := stuTime.strTime + '0' + IntToStr(stuTime.nMinute) + ':'
  else
    stuTime.strTime := stuTime.strTime + IntToStr(stuTime.nMinute) + ':';

  if (stuTime.nSecond < 10) then
    stuTime.strTime := stuTime.strTime + '0' + IntToStr(stuTime.nSecond)
  else
    stuTime.strTime := stuTime.strTime + IntToStr(stuTime.nSecond);

  //release
  aryStr.Free;

  Result := stuTime;
end;


end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -