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 204 205
|
#ifndef DATE_UTILS_H
#define DATE_UTILS_H
#include <string>
using std::string;
namespace DateUtils
{
class CDate
{
public:
CDate();
CDate(int day, int month, int year);
static bool IsLeapYear(int year);
bool IsLeapYear();
int DaysInMonth();
int DayOfYear();
int GetDay() { return m_Day;}
int GetMonth() {return m_Month;}
int GetYear() { return m_Year;}
int DaysBetween(const CDate& other);
void NextDay();
void PreviousDay();
void SubtractDays(unsigned int num_days);
void AddDays(unsigned int);
bool operator==(CDate& other);
bool operator!=(CDate& other);
bool operator<(CDate& other);
string ToString();
private:
int m_Day,
m_Month,
m_Year;
};
}
#endif
DateUtils.cpp
[code]
#include "Dateutils.h"
#include <windows.h>
#include <ctime>
namespace DateUtils
{
CDate::CDate()
{
SYSTEMTIME st;
::GetSystemTime(&st);
m_Day = (int)st.wDay;
m_Month = (int)st.wMonth;
m_Year = (int)st.wYear;
}
CDate::CDate(int day, int month, int year)
{
m_Day = day;
m_Month = month;
m_Year = year;
}
int CDate::DayOfYear()
{
static int MonthDays[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
return MonthDays[m_Month-1] + m_Day + ((m_Month > 2) && IsLeapYear());
}
int CDate::DaysInMonth()
{
int days[] = {-1, 31,28,31,30,31,30,31,31,30,31,30,31};
if (IsLeapYear())
{
days[2] = 29;
}
return days[m_Month];
}
bool CDate::IsLeapYear()
{
return CDate::IsLeapYear(m_Year);
}
bool CDate::IsLeapYear(int year)
{
return (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0));
}
int CDate::DaysBetween(const CDate& other)
{
const int seconds_per_day = 60*60*24;
tm date1 = {0}, date2 = {0};
time_t t1, t2;
date1.tm_year = m_Year - 1900;
date1.tm_mon= m_Month-1;
date1.tm_mday = m_Day;
date2.tm_year = other.m_Year - 1900;
date2.tm_mon= other.m_Month-1;
date2.tm_mday = other.m_Day;
t1 = mktime(&date1);
t2 = mktime(&date2);
return (int)(difftime(t2, t1) / seconds_per_day);
}
void CDate::NextDay()
{
m_Day++;
if (m_Day > DaysInMonth())
{
m_Day = 1;
m_Month++;
}
if (m_Month > 12)
{
m_Month = 1;
m_Year++;
}
}
void CDate::AddDays(unsigned int num_days)
{
for (int i= 0; i < num_days; i++)
{
NextDay();
}
}
void CDate::PreviousDay()
{
m_Day--;
if (m_Day < 1)
{
m_Month--;
if (m_Month < 1)
{
m_Month = 12;
m_Year--;
}
m_Day = DaysInMonth();
}
}
void CDate::SubtractDays(unsigned int num_days)
{}
bool CDate::operator==(CDate& other)
{
return m_Day == other.m_Day &&
m_Month == other.m_Month &&
m_Year == other.m_Year;
}
bool CDate::operator!=(CDate& other)
{
return !(*this == other);
}
bool CDate::operator<(CDate& other)
{
if (m_Year > other.m_Year)
return false;
if (m_Year < other.m_Year)
return true;
if (m_Month > other.m_Month)
return false;
if (m_Month < other.m_Month)
return true;
if (m_Day > other.m_Day)
return false;
if (m_Day < other.m_Day)
return true;
return false;
}
string CDate::ToString()
{
char buffer[64] = "";
char format[] = "%u/%u/%u";
sprintf(buffer,format, m_Day, m_Month, m_Year);
return string(buffer);
}
}
|