📄 apf.htm
字号:
<P>or neater still:</P>
<P>
<PRE>if (testStr == str)
</PRE>
<P>If you peruse your online documentation, you'll find that most of the other CString
member functions are equally easy to use.</P>
<P>
<H2><A NAME="Heading21"></A>The Time Classes</H2>
<P>If you've ever tried to manipulate time values returned from a computer, you'll
be pleased to learn about MFC's CTime and CTimeSpan classes, which represent absolute
times and elapsed times, respectively. The use of these classes is straightforward,
so there's no sample program for this section. However, the following sections get
you started with these handy classes. Before you start working with the time classes,
look over Table F.5, which lists the member functions of the CTime class, and Table
F.6, which lists the member functions of the CTimeSpan class.</P>
<P>
<H4>Table F.5  Member Functions of the CTime Class</H4>
<P>
<TABLE BORDER="1">
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT"><B>Function</B></TD>
<TD ALIGN="LEFT"><B>Description</B></TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">Format()</TD>
<TD ALIGN="LEFT">Constructs a string representing the time object's time.</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">FormatGmt()</TD>
<TD ALIGN="LEFT">Constructs a string representing the time object's GMT (or UTC) time. This is Greenwich
Mean Time.</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">GetCurrentTime()</TD>
<TD ALIGN="LEFT">Creates a CTime object for the current time.</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">GetDay()</TD>
<TD ALIGN="LEFT">Gets the time object's day as an integer.</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">GetDayOfWeek()</TD>
<TD ALIGN="LEFT">Gets the time object's day of the week, starting with 1 for Sunday.</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">GetGmtTm()</TD>
<TD ALIGN="LEFT">Gets a time object's second, minute, hour, day, month, year, day of the week, and
day of the year as a tm structure.</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">GetHour()</TD>
<TD ALIGN="LEFT">Gets the time object's hour as an integer.</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">GetLocalTm()</TD>
<TD ALIGN="LEFT">Gets a time object's local time, returning the second, minute, hour, day, month,
year, day of the week, and day of the year in a tm structure.</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">GetMinute()</TD>
<TD ALIGN="LEFT">Gets the time object's minutes as an integer.</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">GetMonth()</TD>
<TD ALIGN="LEFT">Gets the time object's month as an integer.</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">GetSecond()</TD>
<TD ALIGN="LEFT">Gets the time object's second as an integer.</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">GetTime()</TD>
<TD ALIGN="LEFT">Gets the time object's time as a time_t value.</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">GetYear()</TD>
<TD ALIGN="LEFT">Gets the time object's year as an integer.</TD>
</TR>
</TABLE>
<H4>Table F.6  Member Functions of the CTimeSpan Class</H4>
<P>
<TABLE BORDER="1">
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT"><B>Function</B></TD>
<TD ALIGN="LEFT"><B>Description</B></TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">Format()</TD>
<TD ALIGN="LEFT">Constructs a string representing the time-span object's time</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">GetDays()</TD>
<TD ALIGN="LEFT">Gets the time-span object's days</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">GetHours()</TD>
<TD ALIGN="LEFT">Gets the time-span object's hours for the current day</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">GetMinutes()</TD>
<TD ALIGN="LEFT">Gets the time-span object's minutes for the current hour</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">GetSeconds()</TD>
<TD ALIGN="LEFT">Gets the time-span object's seconds for the current minute</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">GetTotalHours()</TD>
<TD ALIGN="LEFT">Gets the time-span objects total hours</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">GetTotalMinutes()</TD>
<TD ALIGN="LEFT">Gets the time-span object's total minutes</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">GetTotalSeconds()</TD>
<TD ALIGN="LEFT">Gets the time-span object's total seconds</TD>
</TR>
</TABLE>
<H3><A NAME="Heading22"></A>Using a CTime Object</H3>
<P>Creating a CTime object for the current time is a simple matter of calling the
GetCurrentTime() function, like this:</P>
<P>
<PRE>CTime time = CTime::GetCurrentTime();
</PRE>
<P>Because GetCurrentTime() is a static member function of the CTime class, you can
call it without actually creating a CTime object. You do, however, have to include
the class's name as part of the function call, as shown in the preceding code. As
you can see, the function returns a CTime object. This object represents the current
time. If you wanted to display this time, you could call on the Format() member function,
like this:</P>
<P>
<PRE>CString str = time.Format("DATE: %A, %B %d, %Y");
</PRE>
<P>The Format() function takes as its single argument a format string that tells
the function how to create the string representing the time. The previous example
creates a string that looks like this:</P>
<P>
<PRE>DATE: Saturday, April 19, 1998
</PRE>
<P>The format string used with Format() is not unlike the format string used with
functions like the old DOS favorite, printf(), or the Windows conversion function
wsprintf(). That is, you specify the string's format by including literal characters
along with control characters. The literal characters, such as the "DATE:"
and the commas in the previous string example, are added to the string exactly as
you type them, whereas the format codes are replaced with the appropriate values.
For example, the %A in the previous code example will be replaced by the name of
the day, and the %B will be replaced by the name of the month. Although the format-string
concept is the same as that used with printf(), the Format() function has its own
set of format codes, which are listed in Table F.7.</P>
<P>
<H4>Table F.7  Format Codes for the Format() Function</H4>
<P>
<TABLE BORDER="1">
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT"><B>Code</B></TD>
<TD ALIGN="LEFT"><B>Description</B></TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">%a</TD>
<TD ALIGN="LEFT">Day name, abbreviated (such as Sat for Saturday)</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">%A</TD>
<TD ALIGN="LEFT">Day name, no abbreviation</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">%b</TD>
<TD ALIGN="LEFT">Month name, abbreviated (such as Mar for March)</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">%B</TD>
<TD ALIGN="LEFT">Month name, no abbreviation</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">%c</TD>
<TD ALIGN="LEFT">Localized date and time (for the U.S., that would be something like 03/17/98 12:15:34)</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">%d</TD>
<TD ALIGN="LEFT">Day of the month as a number (01-31)</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">%H</TD>
<TD ALIGN="LEFT">Hour in the 24-hour format (00-23)</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">%I</TD>
<TD ALIGN="LEFT">Hour in the normal 12-hour format (01-12)</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">%j</TD>
<TD ALIGN="LEFT">Day of the year as a number (001-366)</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">%m</TD>
<TD ALIGN="LEFT">Month as a number (01-12)</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">%M</TD>
<TD ALIGN="LEFT">Minute as a number (00-59)</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">%p</TD>
<TD ALIGN="LEFT">Localized a.m./p.m. indicator for 12-hour clock</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">%S</TD>
<TD ALIGN="LEFT">Second as a number (00-59)</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">%U</TD>
<TD ALIGN="LEFT">Week of the year as a number (00-51, considering Sunday to be the first day of the
week)</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">%w</TD>
<TD ALIGN="LEFT">Day of the week as a number (0-6, with Sunday being 0)</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">%W</TD>
<TD ALIGN="LEFT">Week of the year as a number (00-51, considering Monday to be the first day of the
week)</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">%x</TD>
<TD ALIGN="LEFT">Localized date representation</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">%X</TD>
<TD ALIGN="LEFT">Localized time representation</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">%y</TD>
<TD ALIGN="LEFT">Year without the century prefix as a number (00-99)</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">%Y</TD>
<TD ALIGN="LEFT">Year with the century prefix as a decimal number (such as 1998)</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">%z</TD>
<TD ALIGN="LEFT">Name of time zone, abbreviated</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">%Z</TD>
<TD ALIGN="LEFT">Name of time zone, not abbreviated</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">%%</TD>
<TD ALIGN="LEFT">Percent sign</TD>
</TR>
</TABLE>
</P>
<P>Other CTime member functions such as GetMinute(), GetYear(), and GetMonth() are
obvious in their use. However, you may like an example of using a function like GetLocalTm(),
which is what the following shows:</P>
<P>
<PRE>struct tm* timeStruct;
timeStruct = time.GetLocalTm();
</PRE>
<P>The first line of the previous code declares a pointer to a tm structure. (The
tm structure is defined by Visual C++ and shown in Listing F.14.) The second line
sets the pointer to the tm structure created by the call to GetLocalTm().</P>
<P>
<H4>Listing F.14  The tm Structure</H4>
<PRE>struct tm {
int tm_sec; /* seconds after the minute - [0,59] */
int tm_min; /* minutes after the hour - [0,59] */
int tm_hour; /* hours since midnight - [0,23] */
int tm_mday; /* day of the month - [1,31] */
int tm_mon; /* months since January - [0,11] */
int tm_year; /* years since 1900 */
int tm_wday; /* days since Sunday - [0,6] */
int tm_yday; /* days since January 1 - [0,365] */
int tm_isdst; /* daylight saving time flag */
</PRE>
<PRE> };
</PRE>
<BLOCKQUOTE>
<P>
<HR>
<STRONG>NOOTE:</STRONG> The CTime class features a number of overloaded constructors,
enabling you to create CTime objects in various ways and using various times. n
<HR>
</BLOCKQUOTE>
<H3><A NAME="Heading23"></A>Using a CTimeSpan Object</H3>
<P>A CTimeSpan object is nothing more complex than the difference between two times.
You can use CTime objects in conjunction with CTimeSpan objects to easily determine
the amount of time that's elapsed between two absolute times. To do this, first create
a CTime object for the current time. Then, when the time you're measuring has
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -