C++每日练笔之日期类(基类)

在我的命名空间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;
}


















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值