RSS


[ Pobierz całość w formacie PDF ]
.Using the Time-Handling ClassesThe capability to store dates and times is a common requirement for many applications.You will probably also need to calculate elapsed times and time spans between storeddate and time values and be able to format those into user-readable text strings.MFC provides four classes to handle all the aspects of date and time manipulation andstorage.Originally, there were just two classes; CTime and CTimeSpan, which are basedon the UNIX time_t (4 byte long value) system (the number of elapsed seconds since1970).However, granularity of only one second and a limited range of dates between1970 and 2038 proved too restrictive for many applications.Hence, two new replace-ment classes, COleDateTime and COleDateTimeSpan, are now also supplied and shouldbe used in preference to CTime and CTimeSpan in newer applications.COleDateTime is based on an underlying DATE structure (which is actually just a doublevalue).This greater capacity of storage type lets COleDateTime cover a range of datesbetween January 1, 100, and December 31, 9999, and down to an approximate resolutionof 1 millisecond.The difference between two COleDateTime values can be representedand manipulated by the COleDateTimeSpan object.Because of the similarity between the CTime class and the newer COleDateTime class, thefollowing sections just describe COleDateTime, although many of the functions are iden-tical in the CTime versions. 034 31240-9 APP F 4/27/00 1:13 PM Page 719Using MFC s Helper Classes 719USING CTime WITH DATABASESYou might find it convenient to use CTime when using ODBC-based databases because theRFX recordset transfer macros know only how to handle CTime objects directly and don tknow how to handle COleDateTime objects without conversion.If you use DAO databases,COleDateTime can be used directly.Using the COleDateTime ClassCOleDateTime is connected with OLE in that it can be used in conjunction with theVARIANT structure, often used in OLE automation.Because of the wide range of date andtime storage systems, especially in OLE environments, COleDateTime must be capable ofconverting between all these various types.This support is reflected in its many con-structor forms, as shown in Table F.10.TABLE F.10.CONSTRUCTOR TYPES USED WITH COleDateTime.Constructor Definition DescriptionCOleDateTime() Creates an uninitialized COleDateTime objectCOleDateTime(const COleDateTime& Copies the values from anotherdatesrc) COleDateTime objectCOleDateTime(int nYear, int nMonth, Initializes the date and time from theint nDay, int nHour, values passedint nMinute, int nSecond)COleDateTime(const VARIANT& varSrc) Converts a date time from a VARIANT structureCOleDateTime(DATE dtSrc) Copies a date time from a DATE structureCOleDateTime(time_t timeSrc) Copies a date time from a UNIX-style time_t struc-tureCOleDateTime(WORD wDosDate) Copies a date time from the MS-DOS style valuesWORD and wDosTimeFCOleDateTime(const SYSTEMTIME& Copies a date time from a SYSTEMTIME structuresystimeSrc)COleDateTime(const FILETIME& Copies a date time from a FILETIME structurefiletimeSrc) 034 31240-9 APP F 4/27/00 1:13 PM Page 720720 Appendix FIf you ve constructed COleDateTime with a valid date time, the object will be markedwith a valid status flag (COleDateTime::valid).Otherwise, the status flag is invalid(COleDateTime::invalid).You can check this status by calling the GetStatus()member function to return the relevant flag value, or you can force the flag value bypassing it into the SetStatus() function.The status flag is also updated when you set date and time values to the object by callingthe SetDateTime() function.This function takes six integer parameters for the year(100 9999), month (1 12), day of the month (1 31), hour (0 23), minute (0 59), andsecond (0 59).You can also set just the date or time components by calling SetDate()passing just the year, month, and day of the month or by calling SetTime() passingonly the hour, minute, and second values.You can use the GetCurrentTime() static function to retrieve the current system timeand set it to a COleDateTime object using the = operator overload function like this:COleDateTime dtCurrent;dtCurrent = COleDateTime::GetCurrentTime();After running these lines, dtCurrent will be set to your machine s current system dateand time.The same values (in the same ranges) can be retrieved by the return value of GetYear(),GetMonth(), GetDay(), GetHour(), GetMinute(), or GetSecond().There are also theuseful derivative functions: GetDayOfWeek() and GetDayOfYear().GetDayOfWeek()returns the day of the week in the range 1 to 7 where 1 is Sunday.The GetDayOfYear()function returns a value in the range 1 to 366 starting at January 1.You can retrieve a displayable formatted CString by using the Format() function.Thisis probably one of the most useful COleDateTime functions because you can pass severalformatting codes to specify the exact format returned from Format(), as shown in TableF.11.These codes are passed as either a string or a string resource identifier, and severalindividual codes are strung together to add various aspects of the formatting.These values are also modified by the current locale settings.The locale preferences canaffect things such as the names of days and months, the ordering of MM/DD/YY repre-sentations, and the AM/PM indicators.TABLE F.11.FORMATTING CODES TO FORMAT THE COleDateTime TEXT OUTPUT [ Pobierz całość w formacie PDF ]
  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • wblaskucienia.xlx.pl