Convert GMT to datetime format in seconds-Delphi source code.

Source: Internet
Author: User

Recently, when writing a PE analysis tool, you need to convert the timedatestamp field value to the datetime format, which is the source code of Delphi.

// Converts the number of seconds in GMT to the date and time format.
Function getgmtdatetime (value: int64): tdatetime;
VaR
Year, month, day: word;
Hour, Min, SEC, msec: word;
Iyear, iday: word;
Ihour, Imin, ISEC: word;
RINT, I: integer;
Tempdate, temptime: tdatetime;
Begin
// GMT is calculated from January 1, January 1, 1970, so it is used as the initial value.
Year: = 1970; month: = 1; Day: = 1;
Hour: = 0; min: = 0; sec: = 0; msec: = 0;
// Calculate the year when the file is created
Iyear: = value Div (365*24*60*60 );
Year: = year + iyear;
// How many days does the computing file have besides creating the entire year?
Iday: = (value Mod (365*24*60*60) Div (24*60*60 );
// Calculate the number of copies per year in a leap year
RINT: = 0;
For I: = 1970 to year-1 do
Begin
If (I mod 4) = 0 then
RINT: = RINT + 1;
End;

// Computing File Creation Time (when)
Ihour: = (value Mod (365*24*60*60) mod (24*60*60) Div (60*60 );
Hour: = hour + ihour;
// Calculate the file creation time (in minutes)
Imin: = (value Mod (365*24*60*60) mod (24*60*60) mod (60*60) Div 60;
Min: = min + Imin;
// Computing File Creation Time (several seconds)
ISEC: = (value Mod (365*24*60*60) mod (24*60*60) mod (60*60) mod 60;
SEC: = sec + ISEC;
// Merge Date and Time
Tempdate: = encodedate (year, month, day );
Temptime: = encodetime (hour, Min, SEC, msec );
// Because there are 29 days in February of a leap year and 365 days in a year of a leap year, and days in a year of the same year
// It is calculated by 365. Therefore, we need to subtract one day from the leap year.
// The final return value merges the date and time into the tdatetime type.
Result: = (tempdate + iday-RINT) + temptime;
End;

Procedure tform1.button1click (Sender: tobject );
VaR
Temp: tdatetime;
Begin
// 708992537 is the number of seconds
Temp: = getgmtdatetime (708992537 );
// Retrieve the date
Edit1.text: = datetostr (temp );
// Fetch time
Edit2.text: = timetostr (temp );
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.