📄 0,1410,26051,00.html
字号:
<HTML>
<HEAD>
<TITLE>Using the API to perform time/date arithmetic to find relative times.</TITLE>
</HEAD>
<BODY>
<A NAME="top"></A>
<table>
<TR>
<td>
<SPAN CLASS="title3"><b>Using the API to perform time/date arithmetic to find relative times.</b></SPAN>
<BLOCKQUOTE CLASS="abstract">
<p><B>Abstract:</B>Converting SYSTEMTIME structure to FILETIME and then ULARGE_INTEGER structure to obtain relative times.</p>
</BLOCKQUOTE><P>
</table>
<b>- Using the API to perform Time/Date arithmetic -</b>
<p><b><i>Background:</i></b>
<br><i>The Windows API recommends not performing arithmetic on the values
from the SYSTEMTIME structure to obtain relative times. Instead, it recommends
the following process:</i><i></i>
<p><i> · Convert the SYSTEMTIME structure to a
FILETIME structure.</i>
<br><i> · Copy the resulting FILETIME structure
to a ULARGE_INTEGER structure.</i>
<br><i> · Use normal 64-bit arithmetic on the
ULARGE_INTEGER value.</i>
<br>
<p>This technical document provides instructions for building a small application
which demonstrates the conversion and arithmetic.
<p><b><i>Step-by-step:</i></b>
<br> <b>1.</b> File | Close All
<br> <b>2.</b> File | New Application
<br> <b>3.</b> Place a three TButtons and a TMemo
on the form.
<br> <b>4.</b> Using the Object Inspector, change
the captions for each button to the following:
<br> (Button1)
Get first time
<br> (Button2)
Get second time
<br> (Button3)
Calculate difference
<p> <b>5.</b> Add the following private data to
Unit1.h:<b></b>
<p>
//---------------------------------------------------------------------------
<br>
// Stores second instance of time
<br>
SYSTEMTIME systemTime1;
<br>
FILETIME fileTime1;
<br>
ULARGE_INTEGER uLargeIntegerTime1;
<p> //
Stores second instance of time
<br>
SYSTEMTIME systemTime2;
<br>
FILETIME fileTime2;
<br>
ULARGE_INTEGER uLargeIntegerTime2;
<p> //
Stores results of time arithmetic
<br>
LARGE_INTEGER uLargeIntegerTimeRESULT;
<br>
FILETIME fileTimeRESULT;
<br>
SYSTEMTIME systemTimeRESULT;
<br>
//---------------------------------------------------------------------------
<p> <b>6.</b> Now, use the following source code
for corresponding TButtons in Unit1.cpp (then run it):
<p> //---------------------------------------------------------------------------
<br> void __fastcall TForm1::Button1Click(TObject *Sender)
<br> {
<br> // Gets the FIRST local time, stores
in SYSTEMTIME structure
<br> GetLocalTime( &systemTime1 );
<br> }
<br> //---------------------------------------------------------------------------
<p> void __fastcall TForm1::Button2Click(TObject *Sender)
<br> {
<br> // Gets the SECOND local time, stores
in SYSTEMTIME structure
<br> GetLocalTime( &systemTime2 );
<br> }
<br> //---------------------------------------------------------------------------
<p> void __fastcall TForm1::Button3Click(TObject *Sender)
<br> {
<br> // Used for testing the result of conversions
<br> bool result;
<p> // Conversion from SYSTEMTIME structure
to FILETIME structure
<br> result = SystemTimeToFileTime(&systemTime1,
&fileTime1);
<br> result = SystemTimeToFileTime(&systemTime2,
&fileTime2);
<p> // Checks if conversion failed
<br> if (!result) ShowMessage("First Conversion
from SYSTEMTIME to FILETIME failed.");
<br> if (!result) ShowMessage("Second Conversion
from SYSTEMTIME to FILETIME failed.");
<p> // Copies the FILETIME structure into
the ULARGER_INTEGER structure;
<br> // (64-bit value for arithmetic purposes)
<br> uLargeIntegerTime1.LowPart
= fileTime1.dwLowDateTime;
<br> uLargeIntegerTime1.HighPart
= fileTime1.dwHighDateTime;
<br> uLargeIntegerTime2.LowPart
= fileTime2.dwLowDateTime;
<br> uLargeIntegerTime2.HighPart
= fileTime2.dwHighDateTime;
<p> // Subtracts the lower and higher parts
of the ULARGE_INTEGER structure
<br> uLargeIntegerTimeRESULT.HighPart
= uLargeIntegerTime2.HighPart - uLargeIntegerTime1.HighPart;
<br> uLargeIntegerTimeRESULT.LowPart
= uLargeIntegerTime2.LowPart - uLargeIntegerTime1.LowPart;
<p> // Converts back to FILETIME structure
from ULARGE_INTEGER structure
<br> // Note: Now this value has the
difference, in milliseconds between the two time intervals
<br> fileTimeRESULT.dwLowDateTime
= uLargeIntegerTimeRESULT.LowPart;
<br> fileTimeRESULT.dwHighDateTime
= uLargeIntegerTimeRESULT.HighPart;
<p> // Coverts back to SYSTEMTIME structure
from FILETIME structure
<br> // *IMPORTANT*: This will add
the number of milliseconds to the following base date: 1/1/1601
<br> result = FileTimeToSystemTime( &fileTimeRESULT,
&systemTimeRESULT);
<p> // Checks if conversion failed
<br> if (!result) ShowMessage("Conversion
from FILETIME to SYSTEMTIME failed.");
<br>
<br> // Displays SYSTEMTIME structure
<br> Memo1->Lines->Add("SYSTEMTIME Base Date
= 1/1/1601" );
<br> Memo1->Lines->Add( "-------------------");
<br> Memo1->Lines->Add( "Year: " +AnsiString(systemTimeRESULT.wYear)
);
<br> Memo1->Lines->Add( "Month: " +AnsiString(systemTimeRESULT.wMonth)
);
<br> Memo1->Lines->Add( "DayOfWeek: " +AnsiString(systemTimeRESULT.wDayOfWeek)
);
<br> Memo1->Lines->Add( "Day: " +AnsiString(systemTimeRESULT.wDay)
);
<br> Memo1->Lines->Add( "Hour: " +AnsiString(systemTimeRESULT.wHour)
);
<br> Memo1->Lines->Add( "Minute: " +AnsiString(systemTimeRESULT.wMinute)
);
<br> Memo1->Lines->Add( "Second: " +AnsiString(systemTimeRESULT.wSecond)
);
<br> Memo1->Lines->Add( "Milliseconds: "
+AnsiString(systemTimeRESULT.wMilliseconds) );
<br> Memo1->Lines->Add( "-------------------");
<br> }
<br> //---------------------------------------------------------------------------
<br> /*
<br> Please be aware that although the code
demonstrates how to convert back into
<br> the SYSTEMTIME structure (for display
purposes), you will probably want to
<br> work with the value in the FILETIME
structure (after the arithmetic) because this
<br> value merely holds the difference in
milliseconds between the two time intervals.
<br> Once you take the extra step to convert
back into the SYSTEMTIME structure,
<br> it adds the difference in milliseconds
to the base date (1/1/1601).
<p> Also, if you choose to implement this
code, you will probably want to evaluate which
<br> of the two values is earlier than the
other so that you know which value to subtract from.
<br> */
<br> //---------------------------------------------------------------------------
<br> // Borland Developer Support
<br> //---------------------------------------------------------------------------
<br>
</body>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -