delphi GUID单元


本文整理自网络,侵删。

 
unit uGUID;


{采用的开源算法 https://github.com/martinusso/ulid 加已改造成32位有序GUID}
interface

uses
  DateUtils, SysUtils, Windows;
  //有序的32位id
  function GetULID(QDataTime:TDateTime):string;
  function EncodeTime(Time:Int64):string;
  function EncodeRandom: string;
implementation

const
  ENCODING: array[0..31] of string = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                                      'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K',
                                      'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'X',
                                      'Y', 'Z'); // Crockford's Base32
  ENCODING_LENGTH = Length(ENCODING);

function GetULID(QDataTime:TDateTime): string;
var vInt64:Int64;
begin
  vInt64 := DateUtils.MilliSecondsBetween(QDataTime, UnixDateDelta);
  Result := EncodeTime(vInt64) + EncodeRandom;
end;

function EncodeRandom: string;
const
  ENCODED_RANDOM_LENGTH = 22;
var
  I: Word;
  Rand: Integer;
begin
  Result := '';
  for I := ENCODED_RANDOM_LENGTH downto 1 do
  begin
    Rand := Trunc(ENCODING_LENGTH * Random);
    Result := ENCODING[Rand] + Result;
  end;
end;

function EncodeTime(Time: Int64): string;
const
  ENCODED_TIME_LENGTH = 10;
var
  I: Word;
  M: Integer;
begin
  Result := '';
  for I := ENCODED_TIME_LENGTH downto 1 do
  begin
    M := (Time mod ENCODING_LENGTH);
    Result := ENCODING[M] + Result;
    Time := Trunc((Time - M) / ENCODING_LENGTH);
  end;
end;

end.

相关阅读 >>

Delphi sql server备份脚本

Delphi 编写系统服务

如何在Delphi中禁用关于“返回值...可能未定义”的警告?

Delphi 封装frame到dll文件

Delphi 删除memo某一行

Delphi 更改消息对话框中的按钮标题

Delphi中多线程分析详解

Delphi 获取打开的记事本中的内容

Delphi读取webbrowse中的图片显示在image中

Delphi dateutils.ispm - 判断时间是否是下午

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...