To write time in standard format to log files. To convert time (now () to character format "YYYY-MM-DD hh: mm: SS Zzz"
Write this function, Delphi system itself also with the Conversion Function formatdatetime ('yyyy-MM-DD hh: mm: SS zzz', now ())
You can also implement this function. Considering that this is a fixed-format conversion function for further optimization.
// Actual test results
Run 1,000,000 formatdatetime () ==> 2825 Ms
Sfformatdattime () ==> 545 Ms
// YYYY-MM-DD hh: mm: SS zzz
Procedure sfformatdatetime (outbuff: pchar; adatetime: tdatetime );
Const
Strday: String =
'000000' +
'000000' +
'000000' +
'123 ';
Str10: String = '000000 ';
VaR
Year, month, day, HH, mm, SS, ZZZ: word;
P: pchar;
I, J: integer;
Begin
P: = outbuff;
Decodedatetime (adatetime, year, month, day, HH, mm, SS, zzz );
// 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
P ^: = strday [(hh-1) * 2 + 1]; Inc (P );
P ^: = strday [(hh-1) * 2 + 2]; Inc (P );
P ^: = ':'; Inc (P );
// Mm
P ^: = strday [(mm-1) * 2 + 1]; Inc (P );
P ^: = strday [(mm-1) * 2 + 2]; Inc (P );
P ^: = ':'; Inc (P );
// SS
P ^: = strday [(SS-1) * 2 + 1]; Inc (P );
P ^: = strday [(SS-1) * 2 + 2]; Inc (P );
P ^: = #32; Inc (P );
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;
End;