⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 0,1410,26051,00.html

📁 C++builder学习资料C++builder
💻 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>&nbsp;&nbsp;&nbsp; &middot; Convert the SYSTEMTIME structure to a  

FILETIME structure.</i>  

<br><i>&nbsp;&nbsp;&nbsp; &middot; Copy the resulting FILETIME structure  

to a ULARGE_INTEGER structure.</i>  

<br><i>&nbsp;&nbsp;&nbsp; &middot; Use normal 64-bit arithmetic on the  

ULARGE_INTEGER value.</i>  

<br>&nbsp;  

<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>&nbsp;&nbsp;&nbsp; <b>1.</b>&nbsp; File&nbsp; |&nbsp; Close All  

<br>&nbsp;&nbsp;&nbsp; <b>2.</b>&nbsp; File&nbsp; |&nbsp; New Application  

<br>&nbsp;&nbsp;&nbsp; <b>3.</b>&nbsp; Place a three TButtons and a TMemo  

on the form.  

<br>&nbsp;&nbsp;&nbsp; <b>4.</b>&nbsp; Using the Object Inspector, change  

the captions for each button to the following:  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (Button1)&nbsp;&nbsp;&nbsp;  

Get first time  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (Button2)&nbsp;&nbsp;&nbsp;  

Get second time  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (Button3)&nbsp;&nbsp;&nbsp;  

Calculate difference  

<p>&nbsp;&nbsp;&nbsp; <b>5.</b>&nbsp; Add the following private data to  

Unit1.h:<b></b>  

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   

  

//---------------------------------------------------------------------------  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  

// Stores second instance of time  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  

SYSTEMTIME systemTime1;  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  

FILETIME fileTime1;  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  

ULARGE_INTEGER uLargeIntegerTime1;  

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //  

Stores second instance of time  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  

SYSTEMTIME systemTime2;  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  

FILETIME fileTime2;  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  

ULARGE_INTEGER uLargeIntegerTime2;  

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //  

Stores results of time arithmetic  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  

LARGE_INTEGER uLargeIntegerTimeRESULT;  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  

FILETIME fileTimeRESULT;  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  

SYSTEMTIME systemTimeRESULT;  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   

  

//---------------------------------------------------------------------------  

<p>&nbsp;&nbsp;&nbsp; <b>6.</b>&nbsp; Now, use the following source code  

for corresponding TButtons in Unit1.cpp (then run it):  

<p>&nbsp;&nbsp;&nbsp; //---------------------------------------------------------------------------  

<br>&nbsp;&nbsp;&nbsp; void __fastcall TForm1::Button1Click(TObject *Sender)  

<br>&nbsp;&nbsp;&nbsp; &#123;  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Gets the FIRST local time, stores  

in SYSTEMTIME structure  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GetLocalTime( &amp;systemTime1 );  

<br>&nbsp;&nbsp;&nbsp; &#125;  

<br>&nbsp;&nbsp;&nbsp; //---------------------------------------------------------------------------  

<p>&nbsp;&nbsp;&nbsp; void __fastcall TForm1::Button2Click(TObject *Sender)  

<br>&nbsp;&nbsp;&nbsp; &#123;  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Gets the SECOND local time, stores  

in SYSTEMTIME structure  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GetLocalTime( &amp;systemTime2 );  

<br>&nbsp;&nbsp;&nbsp; &#125;  

<br>&nbsp;&nbsp;&nbsp; //---------------------------------------------------------------------------  

<p>&nbsp;&nbsp;&nbsp; void __fastcall TForm1::Button3Click(TObject *Sender)  

<br>&nbsp;&nbsp;&nbsp; &#123;  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Used for testing the result of conversions  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bool result;  

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Conversion from SYSTEMTIME structure  

to FILETIME structure  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; result = SystemTimeToFileTime(&amp;systemTime1,  

&amp;fileTime1);  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; result = SystemTimeToFileTime(&amp;systemTime2,  

&amp;fileTime2);  

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Checks if conversion failed  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!result) ShowMessage("First Conversion  

from SYSTEMTIME to FILETIME failed.");  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!result) ShowMessage("Second Conversion  

from SYSTEMTIME to FILETIME failed.");  

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Copies the FILETIME structure into  

the ULARGER_INTEGER structure;  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // (64-bit value for arithmetic purposes)  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; uLargeIntegerTime1.LowPart&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  

=&nbsp;&nbsp; fileTime1.dwLowDateTime;  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; uLargeIntegerTime1.HighPart&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  

=&nbsp;&nbsp; fileTime1.dwHighDateTime;  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; uLargeIntegerTime2.LowPart&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  

=&nbsp;&nbsp; fileTime2.dwLowDateTime;  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; uLargeIntegerTime2.HighPart&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  

=&nbsp;&nbsp; fileTime2.dwHighDateTime;  

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Subtracts the lower and higher parts  

of the ULARGE_INTEGER structure  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; uLargeIntegerTimeRESULT.HighPart&nbsp;  

=&nbsp;&nbsp; uLargeIntegerTime2.HighPart - uLargeIntegerTime1.HighPart;  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; uLargeIntegerTimeRESULT.LowPart&nbsp;&nbsp;  

=&nbsp;&nbsp; uLargeIntegerTime2.LowPart&nbsp; - uLargeIntegerTime1.LowPart;  

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Converts back to FILETIME structure  

from ULARGE_INTEGER structure  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Note:&nbsp; Now this value has the  

difference, in milliseconds between the two time intervals  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fileTimeRESULT.dwLowDateTime&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  

=&nbsp;&nbsp; uLargeIntegerTimeRESULT.LowPart;  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fileTimeRESULT.dwHighDateTime&nbsp;&nbsp;&nbsp;&nbsp;  

=&nbsp;&nbsp; uLargeIntegerTimeRESULT.HighPart;  

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Coverts back to SYSTEMTIME structure  

from FILETIME structure  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // *IMPORTANT*:&nbsp; This will add  

the number of milliseconds to the following base date: 1/1/1601  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; result = FileTimeToSystemTime( &amp;fileTimeRESULT,  

&amp;systemTimeRESULT);  

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Checks if conversion failed  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!result) ShowMessage("Conversion  

from FILETIME to SYSTEMTIME failed.");  

<br>&nbsp;  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Displays SYSTEMTIME structure  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Memo1->Lines->Add("SYSTEMTIME Base Date  

= 1/1/1601" );  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Memo1->Lines->Add( "-------------------");  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Memo1->Lines->Add( "Year: " +AnsiString(systemTimeRESULT.wYear)  

);  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Memo1->Lines->Add( "Month: " +AnsiString(systemTimeRESULT.wMonth)  

);  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Memo1->Lines->Add( "DayOfWeek: " +AnsiString(systemTimeRESULT.wDayOfWeek)  

);  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Memo1->Lines->Add( "Day: " +AnsiString(systemTimeRESULT.wDay)  

);  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Memo1->Lines->Add( "Hour: " +AnsiString(systemTimeRESULT.wHour)  

);  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Memo1->Lines->Add( "Minute: " +AnsiString(systemTimeRESULT.wMinute)  

);  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Memo1->Lines->Add( "Second: " +AnsiString(systemTimeRESULT.wSecond)  

);  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Memo1->Lines->Add( "Milliseconds: "  

+AnsiString(systemTimeRESULT.wMilliseconds) );  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Memo1->Lines->Add( "-------------------");  

<br>&nbsp;&nbsp;&nbsp; &#125;  

<br>&nbsp;&nbsp;&nbsp; //---------------------------------------------------------------------------  

<br>&nbsp;&nbsp;&nbsp; /*  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Please be aware that although the code  

demonstrates how to convert back into  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; the SYSTEMTIME structure (for display  

purposes), you will probably want to  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; work with the value in the FILETIME  

structure (after the arithmetic) because this  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; value merely holds the difference in  

milliseconds between the two time intervals.  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Once you take the extra step to convert  

back into the SYSTEMTIME structure,  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; it adds the difference in milliseconds  

to the base date (1/1/1601).  

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Also, if you choose to implement this  

code, you will probably want to evaluate which  

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; of the two values is earlier than the  

other so that you know which value to subtract from.  

<br>&nbsp;&nbsp;&nbsp; */  

<br>&nbsp;&nbsp;&nbsp; //---------------------------------------------------------------------------  

<br>&nbsp;&nbsp;&nbsp; //&nbsp; Borland Developer Support  

<br>&nbsp;&nbsp;&nbsp; //---------------------------------------------------------------------------  

<br>&nbsp;  

</body>  

   

</HTML>  

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -