Datetimetostr function dedicated optimized edition (YYYY-MM-DD hh: mm: ss zzz)

Source: Internet
Author: User

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;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.