Interbase/Firebird Date&Time Function For UDF

type
  TIBDate = Integer;
  TIBTime = Longword;
  TIBTimeStamp = packed record
    Date: TIBDate;
    Time: TIBTime;
  end;

procedure IBDecodeDate(IBDate: TIBDate; var Year, Month, Day: SmallInt);
var
  Century, Y, M, D: integer;
begin
  IBDate := IBDate - (1721119 - 2400001);
  Century := (4 * IBDate - 1) div 146097;
  IBDate := 4 * IBDate - 1 - 146097 * Century;
  D := IBDate div 4;
  IBDate := (4 * D + 3) div 1461;
  D := 4 * D + 3 - 1461 * IBDate;
  D := (D + 4) div 4;
  M := (5 * D - 3) div 153;
  D := 5 * D - 3 - 153 * M;
  D := (D + 5) div 5;
  Y := 100 * Century + IBDate;
  if M < 10 then
    M := M + 3
  else begin
    M := M - 9;
    Y := Y + 1;
  end;
  Year := Y;
  Month := M;
  Day := D;
end;

function IBEncodeDate(Year, Month, Day: SmallInt): TIBDate;
var
  Century, ShortYear: integer;
begin
  if Month > 2 then
    Month := Month - 3
  else begin
    Month := Month + 9;
    Year := Year - 1;
  end;
  Century := Year div 100;
  ShortYear := Year - 100 * Century;
  Result :=
    (146097 * Century) div 4 +
    (1461 * ShortYear) div 4 +
    (153 * Month + 2) div 5 + Day + 1721119 - 2400001;
end;

procedure IBDecodeTime(IBTime: TIBTime; var Hour, Min, Sec, Msec: word);
const
  ISC_TIME_SECONDS_PRECISION = 10000;
  SECONDS_PER_MINUTE = 60;
  SECONDS_PER_HOUR = 3600;
//  SECONDS_PER_DAY = 86400;
var
  TotalSeconds: Longword;
begin
  TotalSeconds := IBTime div ISC_TIME_SECONDS_PRECISION;
  Msec := IBTime mod ISC_TIME_SECONDS_PRECISION;
  Hour := TotalSeconds div SECONDS_PER_HOUR;
  Min := (TotalSeconds div SECONDS_PER_MINUTE) mod SECONDS_PER_MINUTE;
  Sec := TotalSeconds mod SECONDS_PER_MINUTE;
end;

function IBEncodeTime(Hour, Min, Sec, Msec: word): TIBTime;
const
  ISC_TIME_SECONDS_PRECISION = 10000;
  SECONDS_PER_MINUTE = 60;
  SECONDS_PER_HOUR = 3600;
//  SECONDS_PER_DAY = 86400;
begin
  Result := (Hour * SECONDS_PER_HOUR + Min * SECONDS_PER_MINUTE + Sec)
    * ISC_TIME_SECONDS_PRECISION + Msec;
end;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值