TimeEx.h
上传用户:maryhy001
上传日期:2007-05-02
资源大小:2317k
文件大小:7k
- #ifndef _TIMEEX_H
- #define _TIMEEX_H
- //#include "AdoLib.h"
- #include <math.h> //Math library
- #include <assert.h> //assert
- #include <windows.h> //windows type defined here
- //max date or time string size
- #define MAX_DATE_SIZE 16
- #define MAX_TIME_SIZE 16
- #define VALIN(VAL, Min, Max) ((VAL>=Min && VAL<=Max)?true:false)
- typedef enum WEEKDAY{ WD_MONDAY = 0, WD_TUESDAY = 1, WD_WEDNESDAY = 2,
- WD_THURSDAY = 3, WD_FRIDAY = 4, WD_SATURDAY = 5,
- WD_SUNDAY = 6, WD_ERROR = 7
- };
- /*
- * CTimeEx class --- the eFTP system's base time class
- */
- class CDateEx
- {
- public:
- CDateEx(){
- this->NowDate();
- }
- CDateEx(CDateEx &dtDate){
- m_unYear = dtDate.Year();
- m_unMonth = dtDate.Month();
- m_unDay = dtDate.Day();
- }
-
- //if the arguments all are zero(0), then class use the current time
- CDateEx(UINT unYear, UINT unMonth, UINT unDay){
- setValue(unYear, unMonth, unDay);
- }
-
- //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- //modify the year, month and day value
- void setValue(UINT unYear, UINT unMonth, UINT unDay){
- //store the old value
- UINT uny = 0,
- unm = 0,
- und = 0;
-
- if( !VALIN(unYear, 1789, 3000) ||
- !VALIN(unMonth, 1, 12)){
- throw "invalid arguments...";
- }
- else{
- m_unYear = unYear;
- m_unMonth = unMonth;
- m_unDay = 1;
- }
-
- int nMax = 30;
- if(!IsLargeMonth(m_unMonth)){
- if(IsLeapYear()){
- if(2 == m_unMonth){
- nMax = 29;
- }
- }
- else{
- if(2 == m_unMonth){
- nMax = 28;
- }
- }
- }
- else{
- nMax = 31;
- }
- if(!VALIN(unDay, 1, nMax)){
- //restore the old value
- this->m_unYear = uny;
- this->m_unMonth = unm;
- this->m_unDay = und;
-
- //throw the exception
- throw "invalid arguments...";
- }
- else{
- m_unDay = unDay;
- }
- }
- //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- //operator "="
- CDateEx operator=(CDateEx &dtDate);
- //operator "=="
- bool operator==(CDateEx &dtDate);
- //operator "!="
- bool operator!=(CDateEx &dtDate);
- //operator ">"
- bool operator>(CDateEx &dtDate);
- //operator ">="
- bool operator>=(CDateEx &dtDate);
- //operator "<"
- bool operator<(CDateEx &dtDate);
- //operator "<="
- bool operator<=(CDateEx &dtDate);
- //operator "-"
- int operator-(CDateEx &dtDate);
-
- //operator "++"(add one day)
- CDateEx operator++(int);
- //operator "--"(sub one day)
- CDateEx operator--(int);
-
- //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- virtual ~CDateEx(){
- }
- //get the year, month and day value
- UINT Year(){
- return m_unYear;
- }
- UINT Month(){
- return m_unMonth;
- }
- UINT Day(){
- return m_unDay;
- }
-
- //determine the year is leap year
- bool IsLeapYear(){
- return (m_unYear % 4 == 0) && (m_unYear % 100 != 0) || (m_unYear % 400 == 0);
- }
- //get current date
- void NowDate();
- //get the date string text
- void AsDString(LPSTR lpzDate, bool bFixedFmt = true);
- protected:
- //year, month and day members
- UINT m_unYear,
- m_unMonth,
- m_unDay;
- //determine the month whether is large month
- bool IsLargeMonth(UINT unMonth){
- if( unMonth == 1 || unMonth == 3 || unMonth == 5 ||
- unMonth == 7 || unMonth == 8 || unMonth == 10 || unMonth == 12){
- return true;
- }
- return false;
- }
- };
- /*
- * CTimeEx class --- now the class only support 0~24 system time
- doesn't support AM~PM system time
- */
- class CTimeEx
- {
- public:
- //constructor and destructor
- CTimeEx(){
- this->NowTime();
- }
- CTimeEx(CTimeEx &dtTime){
- m_unHour = dtTime.Hour();
- m_unMinu = dtTime.Minute();
- m_unSecn = dtTime.Second();
- }
-
- //constructor another
- CTimeEx(UINT unHour, UINT unMinute, UINT unSecond){
- setValue(unHour, unMinute, unSecond);
- }
- //set hour, minute and second value
- void setValue(UINT unHour, UINT unMinute, UINT unSecond){
- if( !VALIN(unHour, 0, 23) ||
- !VALIN(unMinute, 0, 59) ||
- !VALIN(unSecond, 0, 59))
- throw "parameters is invalid...";
- m_unHour = unHour;
- m_unMinu = unMinute;
- m_unSecn = unSecond;
- }
- //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- //operator "="
- CTimeEx operator=(CTimeEx &dtTime);
-
- //operator "=="
- bool operator==(CTimeEx &dtTime);
-
- //operator "!="
- bool operator!=(CTimeEx &dtTime);
-
- //operator ">"
- bool operator>(CTimeEx &dtTime);
-
- //operator ">="
- bool operator>=(CTimeEx &dtTime);
-
- //operator "<"
- bool operator<(CTimeEx &dtTime);
-
- //operator "<="
- bool operator<=(CTimeEx &dtTime);
-
- //operator "-"
- DWORD operator-(CTimeEx &dtTime);
-
- //operator "++"(add one second)
- CTimeEx operator++(int);
-
- //operator "--"(sub one second)
- CTimeEx operator--(int);
-
- //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- //get hour, minute and second value
- UINT Hour(){
- return m_unHour;
- }
- UINT Minute(){
- return m_unMinu;
- }
- UINT Second(){
- return m_unSecn;
- }
- //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- virtual ~CTimeEx(){
- }
- //get current time
- void NowTime();
- //get the time string text
- void AsTString(LPSTR lpzTime, bool bFixedFmt = true);
- public:
- //!!!Reserve info
- enum TIMEFMT{TF_AM = 0, TF_PM};
- protected:
- //hour, minute and second members
- UINT m_unHour,
- m_unMinu,
- m_unSecn;
- };
- /*
- * CDateTimeEx class --- support the operation of datetime data type
- */
- class CDateTimeEx: public CDateEx,
- public CTimeEx
- {
- public:
- //constructor and destructor
- CDateTimeEx();
- //updown inherited static cast change
- CDateTimeEx(CDateEx &dtDate);
- CDateTimeEx(CTimeEx &dtTime);
- //SYSTEMTIME change to CDateTimeEx
- CDateTimeEx(const SYSTEMTIME stTime);
- //FILETIME change to CDateTimeEx
- CDateTimeEx(const FILETIME ftTime);
- //DATE change to CDateTimeEx
- CDateTimeEx(const DATE dtSrc);
- //"struct tm" change to CDateTimeEx
- CDateTimeEx(struct tm &tmDest);
- //#########################################
- //operator "=" --- system time
- CDateTimeEx& operator=(const SYSTEMTIME stTime);
- //operator "=" --- file time
- CDateTimeEx& operator=(const FILETIME ftTime);
- //get OleDate
- bool getOleDate(struct tm *tmTime);
- //day of week
- WEEKDAY DayOfWeek();
- //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
-
- //get the now datetime info
- void Now();
- //get the datetime string text
- void AsDTString(LPSTR lpzDate, bool bFixedFmt = true);
- //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- virtual ~CDateTimeEx(){
- }
- private:
- void equalSysTime(const SYSTEMTIME stTime);
- void equalFileTime(const FILETIME ftTime);
- //get "struct tm" from DATE structor
- bool DTFromOleDate(DATE dtSrc, struct tm& tmDest);
- //get DATE structor form "struct tm"
- bool OleDateFromDT(
- WORD wYear, WORD wMonth, WORD wDay,
- WORD wHour, WORD wMinute, WORD wSecond,
- DATE& dtDest);
- };
- #endif //_TIMEEX_H