.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;

 

 

你可能感兴趣的:(DateTime)