1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
| ///////////////////////////////////////////////////////////////////
//
// Implementation of the CDate class .cpp
//
////////////////////////////////////////////////////////////////////
#include "TEnumerations.h"
#include "CDate.h"
////////////////////////////////////////////////////////////////////
int g_oLEAP[]={31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int g_oNOLEAP[]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
////////////////////////////////////////////////////////////////////
CDate::~CDate()
{
}
////////////////////////////////////////////////////////////////////
CDate::CDate()
{
m_lDay = 1;
m_eMonth = Jan;
m_lYear = 1900;
}
////////////////////////////////////////////////////////////////////
bool CDate::calcindex()
{
long l_lDays = 0;
long l_lLeap = (long)((m_lYear - 1900)/4);
for(int i = 0; i < (long)m_eMonth-1; i++)
{
if(m_bIsLeap)
{
l_lDays += g_oLEAP[i];
}
else
{
l_lDays += g_oNOLEAP[i];
}
}
m_lIndex = (m_lYear - 1900)*365 + l_lLeap + (long)l_lDays + (long) m_lDay ;
if (!m_bIsLeap)
{
m_lIndex = m_lIndex + 1;
}
return true;
}
////////////////////////////////////////////////////////////////////
CDate::CDate(const int& p_lDay, const EMonth& p_eMonth, const long& p_lYear)
{
m_lDay = p_lDay;
m_eMonth = p_eMonth;
m_lYear = p_lYear ;
m_bIsLeap = (p_lYear%4 == 0);
m_lIndex_ = m_lYear*10000+(long)m_eMonth*100+m_lDay;
if(!calcindex())
{
}
}
////////////////////////////////////////////////////////////////////
CDate::CDate(const long& p_lIndex, bool p_bExcel)
{
int i=0;
long l_lValo;
long l_lLeap;
long l_lDays = 0;
m_bExcel = p_bExcel;
if(p_bExcel)
{
m_lIndex = p_lIndex;
m_lYear = 1900 + (long)(p_lIndex/365);
m_bIsLeap = (m_lYear % 4 == 0);
l_lLeap = (long)((m_lYear - 1900)/4);
if(m_bIsLeap)
{
l_lValo = m_lIndex - (m_lYear - 1900)*365-l_lLeap;
}
else
{
l_lValo = m_lIndex - (m_lYear - 1900)*365-l_lLeap-1;
}
while(l_lDays < l_lValo)
{
if(m_bIsLeap)
{
l_lDays += g_oLEAP[i];
}
else
{
l_lDays += g_oNOLEAP[i];
}
i=i+1;
}
i=i-1;
l_lDays = l_lDays - g_oLEAP[i];
m_lDay = l_lValo - l_lDays;
m_eMonth = (EMonth)(i+1);
m_lIndex_ = m_lYear*10000+(long)m_eMonth*100+m_lDay;
}
else
{
m_lYear = p_lIndex-((long) p_lIndex/10000)*10000;
m_bIsLeap = (m_lYear % 4 == 0);
m_lDay = (long)p_lIndex/1000000;
m_eMonth = (EMonth)((p_lIndex - m_lDay*1000000-m_lYear)/10000);
m_lIndex_ = p_lIndex;
if(!calcindex())
{
}
}
}
////////////////////////////////////////////////////////////////////
CDate::CDate(const CDate& p_dDate)
{
m_lDay = p_dDate.m_lDay ;
m_eMonth = p_dDate.m_eMonth ;
m_lYear = p_dDate.m_lYear ;
m_lIndex_ = p_dDate.m_lIndex_ ;
m_lIndex = p_dDate.m_lIndex ;
m_bIsLeap = p_dDate.m_bIsLeap ;
m_bExcel = p_dDate.m_bExcel ;
}
////////////////////////////////////////////////////////////////////
CDate CDate::add(const EPeriod &p_ePeriod, const long &p_lPeriod, bool p_bForward)
{
long l_lIndex;
long l_lMonth;
EMonth l_eMonth;
long l_lYear ;
switch(p_ePeriod)
{
case Days:
l_lIndex = m_lIndex + (p_bForward ? p_lPeriod : -p_lPeriod);
return CDate(l_lIndex);
break;
case Months:
l_lMonth = p_lPeriod%12;
l_eMonth =(EMonth) (((long)m_eMonth + 12 + (p_bForward ? l_lMonth : -l_lMonth)) % 12) ;
l_lYear = m_lYear + (p_bForward ? (long)(p_lPeriod/12) : -(long)(p_lPeriod/12));
return CDate(m_lDay, l_eMonth, l_lYear);
break;
case Weeks:
l_lIndex = m_lIndex + (p_bForward ? 7*p_lPeriod : -7*p_lPeriod);
return CDate(l_lIndex);
break;
case Years:
l_lYear = m_lYear + (p_bForward ? p_lPeriod : -p_lPeriod);
return CDate(m_lDay, m_eMonth, l_lYear);
break;
}
}
////////////////////////////////////////////////////////////////////
int CDate::day()
{
return m_lDay;
}
////////////////////////////////////////////////////////////////////
EMonth CDate::month()
{
return m_eMonth;
}
////////////////////////////////////////////////////////////////////
long CDate::year()
{
return m_lYear;
}
////////////////////////////////////////////////////////////////////
long CDate::index()
{
return m_lIndex;
}
////////////////////////////////////////////////////////////////////
bool CDate::isLeap()
{
return m_bIsLeap;
}
////////////////////////////////////////////////////////////////////
long CDate::index_()
{
return m_lIndex_;
}
////////////////////////////////////////////////////////////////////
CDate& CDate::operator =(const CDate &p_dDate)
{
m_lDay = p_dDate.m_lDay ;
m_eMonth = p_dDate.m_eMonth ;
m_lYear = p_dDate.m_lYear ;
m_lIndex_ = p_dDate.m_lIndex_ ;
m_lIndex = p_dDate.m_lIndex ;
m_bIsLeap = p_dDate.m_bIsLeap ;
m_bExcel = p_dDate.m_bExcel ;
return *this;
} |
Partager