C++毎日ペンを練習する日付類(ベース類)

7167 ワード

       TYCppStdLib               。
            。
      :
CDate
CTime
CDateAndTime
CMyDate
CMyTime
CMyDateAndTime

            ,           。
                  。
           ,            。

      CDate (  ,      ,   )。
/*- ==========================================================
*          :CDate.h
*         :   
*         :1.0.0.2595
*         :2012-04-23
*         :2012-05-01
*         :   (  )
*         :         YuanPeirong 
*         :Windows 7(x64) SP1        
*        :  Visual Studio 2010 SP1(     )
                MinGW 2011118
                Visual C++ 6.0 SP6(     )
- ==========================================================*/

#ifndef CDate_H_TYCppStdLib
#define CDate_H_TYCppStdLib

#ifdef CDate_DLL_API
#else
#define CDate_DLL_API _declspec(dllimport)
#endif

#include <string>
#include "Windows.h"

using namespace std;

namespace TYCppStdLib
{   
    class CDate_DLL_API CDate
    {
    public:
        CDate();
        virtual ~CDate();
        
        virtual int GetYear();   //    
        virtual int GetMonth();  //    
        virtual int GetDay();    //    
        
        virtual bool SetYear(int year);    //    
        virtual bool SetMonth(int month);  //    
        virtual bool SetDay(int day);      //    
        
        //     
        virtual void GetDate(int &year,int &month,int &day);
        //virtual string GetDate(const string strDivide="-");
        //virtual void GetDate(char *dates,char *divide="-");
        //virtual void GetDate(CMyDate &ob);
        
        //     
        virtual bool SetDate(int year,int month,int day); 
        //virtual bool SetDate(char *dates);
        
        //         
        virtual bool DateIsValid(int year,int month,int day);
        //     
        virtual bool IsLeapYear(int year=0);  
        //       
        virtual int GetDaysOfYear(int year=0); 
        //        
        virtual int GetDaysOfMonth(int year=0,int month=0);
        //         
        virtual int GetDaysOver(int year,int month,int day);
    };
}

#endif
/*- ==========================================================
*          :CDate.cpp
*         :   
*         :1.0.0.2595
*         :2012-04-23
*         :2012-05-01
*         :   (  )
*         :         YuanPeirong 
*         :Windows 7(x64) SP1        
*        :  Visual Studio 2010 SP1(     )
                MinGW 2011118
                Visual C++ 6.0 SP6(     )
- ==========================================================*/

#ifndef CDate_DLL_ForAPI
#define CDate_DLL_ForAPI

#define CDate_DLL_API _declspec(dllexport)

#endif

#include "../../Include/DateAndTime/CDate.h"
//#include "stdio.h"
// #include <cstdio>
//#include "string.h"
// #include <cstring>
// #include <cstdlib>
//using std::sprintf;
// using std::strcat;
// using std::strcpy;

/*     typedef struct _SYSTEMTIME { // st 
    WORD wYear; 
    WORD wMonth; 
    WORD wDayOfWeek; 
    WORD wDay; 
    WORD wHour; 
    WORD wMinute; 
    WORD wSecond; 
    WORD wMilliseconds; 
    } SYSTEMTIME; */

//    
TYCppStdLib::CDate::CDate()
{
    //   
}

//    
TYCppStdLib::CDate::~CDate()
{
    //   
}

//    
int TYCppStdLib::CDate::GetYear()
{
    SYSTEMTIME st;
    GetLocalTime(&st);
    return st.wYear;
}
 
//      
int TYCppStdLib::CDate::GetMonth()
{
    SYSTEMTIME st;
    GetLocalTime(&st);
    return st.wMonth;
}
 
//     
int TYCppStdLib::CDate::GetDay()
{
    SYSTEMTIME st;
    GetLocalTime(&st);
    return st.wDay;
}

//    
bool TYCppStdLib::CDate::SetYear(int year)
{
    SYSTEMTIME st;
    if(year<1970)
        return false;
    GetLocalTime(&st);
    st.wYear=year;
    if(SetLocalTime(&st))
        return true;
    return false;
}

//        
bool TYCppStdLib::CDate::SetMonth(int month)
{
    SYSTEMTIME st;
    if(month<1 || month>12)
        return false;
    GetLocalTime(&st);
    st.wMonth=month;
    if(SetLocalTime(&st))
        return true;
    return false;
}
  
//    
bool TYCppStdLib::CDate::SetDay(int day)
{
    SYSTEMTIME st;
    int t;
    GetLocalTime(&st);
    t=GetDaysOfMonth();
    if(day<1 || day>t)
        return false;
    st.wDay=day;
    if(SetLocalTime(&st))
        return true;
    return false;
}
  
//       
void TYCppStdLib::CDate::GetDate(int &year,int &month,int &day)
{
    year=GetYear();
    month=GetMonth();
    day=GetDay();
}
  
 // void TYCppStdLib::CDate::GetDate(char *dates,char *divide="-")
// {
    // int year, month, day;
    // char *cyear, *cmonth ,*cday;
    // year=GetYear();
    // month=GetMonth();
    // day=GetDay();
    
     // strcpy(dates,year,4,RIGHTSIDE,'0',divide);
    // strcat(dates,month,2,RIGHTSIDE,'0',divide);
    // strcat(dates,day,2,RIGHTSIDE,'0'); 
    
     // sprintf(cyear, "%d", year);
    // sprintf(cmonth, "%d", month);
    // sprintf(cday, "%d", day);

    // itoa(year, cyear, 10);
    // itoa(month, cmonth, 10);
    // itoa(day, cday, 10);
    
    // strcpy(dates, cyear);
    // strcat(dates, divide);
    // strcat(dates, month);
    // strcat(dates, divide);
    // strcat(dates, day);
// }

// void TYCppStdLib::CDate::GetDate(CDate &ob)
// {

// } 
    
//       
bool TYCppStdLib::CDate::SetDate(int year,int month,int day)
{
    if(!SetYear(year))
        return false;
    if(!SetMonth(month))
        return false;
    return SetDay(day);
}
 
// bool TYCppStdLib::CDate::SetDate(char *dates)
// {

// }

//                          
bool TYCppStdLib::CDateAndTime::DateIsValid(int year,int month,int day)
{
    if(year<0 || day<1)
        return false;
    if(month<1 || month>12)
        return false;
    int MaxDay=GetDaysOfMonth(year, month);
    if(day>MaxDay)
        return false;
    return true;
}

//     
bool TYCppStdLib::CDateAndTime::IsLeapYear(int year)
{
    if(year<0)
        return false;
    if(!year)
    {
        SYSTEMTIME st;
        GetLocalTime(&st);
        year=st.wYear;
    }
    if((year%4==0 && year%100!=0) || year%400==0)
        return true;
    return false;
}

//       
int TYCppStdLib::CDateAndTime::GetDaysOfYear(int year)
{
    SYSTEMTIME st;
    GetLocalTime(&st);
    if(!year)
        year=st.wYear;
    if(year<0)
        return 0;//return false;
    if(IsLeapYear(year))
        return 366;
    else
        return 365;
}

//        
int TYCppStdLib::CDateAndTime::GetDaysOfMonth(int year,int month)
{
    SYSTEMTIME st;
    GetLocalTime(&st);
    int day, leap;
    if(!month)
        month=st.wMonth;
    if(!year)
        year=st.wYear;
    if(year<1970)
        return 0;//return false;
    if (month<1 || month>12)
        return 0;//return false;
    if(IsLeapYear(year))
        leap=1;
    else
        leap=0;
    day=31;     //  31 
    switch (month)
    {
        case 4:
        case 6:
        case 9:
        case 11:
            day=30;
            break;
        case 2:
            day=28+leap;
            break;
    }
    return day;
}