So today I'm talking with you about timing ;D
It's not that easy what it looks like, but it is sometimes really useful.
First, for the following example, you need a Button and a Memo on your Form.
I have put this example in the onClick Event from the Button.
If you understand it, you don't have to use Button's nor Memo's.
Code:
procedure TForm1.Button1Click(Sender: TObject);
var today: TDateTime;
FileTime,ft,FileTime2: TFileTime;
systime,own: TSystemTime;
unixTime: int64;
begin
GetSystemTime(systime); //Get the System Time. i will come back to this later. But i want the actually one (so we can compare it later to the "today" variable)
//GetSystemTime gives us the actually Systemtime (Date & Time) in UTC/GMT
// SystemTime is splitted in >>Date<< (with: Year, Month, Day) and >>Time<< (with: Hour, Minute, Second AND Milli Second)
// So if we want to create our own Systemtime, we have to fill this specification!
//today := Time; //Gives us the actualy Time... (It's to less information)
//today := Date; //Gives us the actualy Date... (Better, but still to less information)
today :=now; //Gives us everything, so we have the actualy Time & Date!!! (We got the Time in gmt + x; x is the Timezone you are in!)
// (We have the Time which you see on your Computer)
//If you want to do your own Date & Time, there are some possibilities, for example:
//today := StrToDateTime('11.11.2012 12:55:31'); //maybe on an american System, it should be: StrToDateTime('11/11/2012 12:55:31');
//ShowMessage(DateTimeToStr(today));
//Ok, now let us split this in our own SystemTime: (an other possibility is, to decode your own Date & Time in here)
DecodeDate(today, own.wYear, own.wMonth, own.wDay);
DecodeTime(today, own.wHour, own.wMinute, own.wSecond, own.wMilliSeconds);
//We would get the Same Effect with the actually time:
// today := now;
// DateTimeToSystemTime(today, own); //In "own" we got the actually Date & Time (but with the gmt+x, not the normal gmt Time)
//With this method we don't have to fill an other TSystemTime variable, it will filled automatically.
//But remember: With this method, we ONLY have the actually Time just with gmt+x
//We still have to use LocalFileTimeToFileTime and convert the Time to the file time based on the Coordinated Universal Time (UTC)
//SystemTimeToFileTime(own, ft); //showmessage('filetime:'+Inttostr(Int64(ft)));
//LocalFileTimeToFileTime(ft, FileTime); //The LocalFileTimeToFileTime function converts a local file time to a file time based on the Coordinated Universal Time (UTC).
SystemTimeToFileTime(own, ft); //Convert our SystemTime to FileTime, as the conversion Method wouldn't say it ;D (We converted it to Windows Filetime)
LocalFileTimeToFileTime(ft, FileTime); //The LocalFileTimeToFileTime function converts a local file time to a file time based on the Coordinated Universal Time (UTC).
Form1.Memo1.lines.add('filetime-1:'+Inttostr(Int64(filetime)));
//Ok, did you remember our "GetSystemTime(systime);" at the beginning? All the stuff above is not necessary, if you only want
//the actually System Time and didn't want to change something...
//I just made it at the beginning, because so we can compare it. If i would do it now, the time would maybe different - don't forget, we also take the Milliseconds!
SystemTimeToFileTime(systime, FileTime2);
Form1.Memo1.lines.add('filetime-2:'+Inttostr(Int64(FileTime2)));
Form1.Memo1.lines.add('The filetime''s should be identically!');
//So now let us transform it to Unix Time:
unixTime := round((int64(filetime) - int64(116444736000000000)) / 10000000);
// 11644473600 == 1. Januar 1901 00:00:00
//If you don't understand it, then look on wikipedia for "Unix time"
//I couldn't Post you the Link, because of the insufficient of my Postcount ~.~
Form1.Memo1.lines.add('Unix Time(-Stamp) is: '+ IntToStr(unixTime));
end;