delphi DateTimeToStr函数专用优化版


本文整理自网络,侵删。

 为了在日志文件中写入标准格式的时间。要将时间(Now() 转换到字符格式 " YYYY-MM-DD hh:mm:ss zzz"

就编写了此函数,delphi系统自身也带了转换函数 formatDateTime('YYYY-MM-DD hh:mm:ss zzz',Now())

也可以实现此功能。考虑到我这个是固定格式的转换函数,作进一步的优化。

//实际测试效果

 运行 1,000,000 次 FormatDateTime()===>2825ms

                                  sfNowToBuf() ==>545

procedure sfNowToBuf(const OutBuf:PChar;BufSize:Integer);

const
   strDay:string =
    '010203040506070809101112131415161718192021222324252627282930' +
    '313233343536373839404142434445464748495051525354555657585960' +
    '6162636465666768697071727374757677787980'  +
    '81828384858687888990919293949596979899';
   str10:string = '0123456789';
var
  Year,Month,Day,HH,MM,SS,ZZZ:WORD;
  P:PChar;
  I,J:Integer;
  SystemTime: TSystemTime;
  lvBuf:array[0..22] of char;
begin
  if BufSize <= 0 then
    Exit;

  P := @lvBuf[0];// OutBuff;
  for I := 0 to BufSize - 1 do P[I] := '0';

  GetLocalTime(SystemTime);
   Year  := SystemTime.wYear;
   Month := SystemTime.wMonth;
   Day   := SystemTime.wDay;
   HH    := SystemTime.wHour;
   MM    := SystemTime.wMinute;
   SS    := SystemTime.wSecond;
   ZZZ   := SystemTime.wMilliseconds;

   (*  2012-11-04 17:59
     ZZZ := 0;
     HH  := 0;
     MM  := 0;
     SS := 0;
   *)

    //Year
    I := Year div 1000;
    J := Year mod 1000;
    P^ := str10[I + 1];Inc(P);
    I := J div 100;
    P^ := str10[I + 1];Inc(P);
    I := J mod 100;
    if I > 0 then
    begin
      P^ := strDay[(I - 1) * 2 + 1];Inc(P);
      P^ := strDay[(I - 1) * 2 + 2];Inc(P);
      P^ := '-';Inc(P);
    end
    else begin
       P^ := '0';Inc(P);
       P^ := '0';Inc(P);
      P^ := '-';Inc(P);
   end;

     //Month

    P^ := strDay[(Month - 1) * 2 + 1];Inc(P);
    P^ := strDay[(Month - 1) * 2 + 2];Inc(P);
    P^ := '-';Inc(P);
  

   //Day
     P^ := strDay[(Day - 1) * 2 + 1];Inc(P);
     P^ := strDay[(Day - 1) * 2 + 2];Inc(P);
     P^ := #32;Inc(P);

  //HH
     if HH > 0 then
     begin
       P^ := strDay[(HH - 1) * 2 + 1];Inc(P);
       P^ := strDay[(HH - 1) * 2 + 2];Inc(P);
     end
     else begin
       P^ := #48;Inc(P);
       P^ := #48;Inc(P);
     end;
     P^ := ':';Inc(P);

    //MM
     if MM > 0 then
     begin
       P^ := strDay[(MM - 1) * 2 + 1];Inc(P);
       P^ := strDay[(MM - 1) * 2 + 2];Inc(P);
     end
     else begin
       P^ := #48;Inc(P);
       P^ := #48;Inc(P);
     end;
     P^ := ':';Inc(P);

    //SS
     if SS > 0 then
     begin
      P^ := strDay[(SS - 1) * 2 + 1];Inc(P);
      P^ := strDay[(SS - 1) * 2 + 2];Inc(P);
     end
     else begin
       P^ := #48;Inc(P);
       P^ := #48;Inc(P);
     end;
     P^ := #32;Inc(P);

     //ZZZ
    Year  := ZZZ div 100;
    Month := ZZZ mod 100;
    P^ := str10[Year + 1];Inc(P);
    if Month > 0 then
    begin
       P^ := strDay[(Month - 1) * 2 + 1];Inc(P);
      P^ := strDay[(Month - 1) * 2 + 2];
    end
    else begin
      P^ := '0';Inc(P);
      P^ := '0';
    end;

  if BufSize >23 then BufSize := 23;
  P := OutBuf;
  for I := 0 to BufSize - 1 do P[I] :=  lvBuf[I]
end;

相关阅读 >>

Delphi xe7实现手机上获取wifi信息的程序

Delphi 如何将access数据库后缀名accdb改为mdb

Delphi的一些开发技巧和方法

Delphi中生成控件的两种方法

Delphi中窗体的事件

Delphi 检查父进程

Delphi检查网络连接状态3种方式

Delphi 如何判断可见字符 unicode

Delphi环境下基于spcomm控件开发串口通讯报文字节丢失的问题解决

Delphi 字符串前后翻转

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



打赏

取消

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

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

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

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

评论

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