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
| #include "Chrono.h"
namespace Chrono {
// définition des fonctions membres:
//------------------------------------------------------------------------------
Date::Date(int yy, Month mm, int dd)
: y(yy), m(mm), d(dd)
{
if (!is_date(yy,mm,dd)) throw Invalid();
}
//------------------------------------------------------------------------------
const Date& default_date()
{
static const Date dd(2001,Date::jan,1); // start of 21st century
return dd;
}
//------------------------------------------------------------------------------
Date::Date()
:y(default_date().year()),
m(default_date().month()),
d(default_date().day())
{
}
//------------------------------------------------------------------------------
void Date:: add_day(int n)
{
// ...
if (n<0) error("add_day(): can't handle negative n");
while (days_in_month(y,m)<n) {
add_month(1);
n -= days_in_month(y,m);
}
d += n;
}
//------------------------------------------------------------------------------
void Date::add_month(int n)
{
// ...
if (n<0) error("add_month(): cnot implemented");
}
//------------------------------------------------------------------------------
void Date::add_year(int n)
{
if (m==feb && d==29 && !leapyear(y+n)) { // beware of leap years!
m = mar; // use March 1 instead of February 29
d = 1;
}
y+=n;
}
//------------------------------------------------------------------------------
// fonctions d'assistance:
bool is_date(int y, Date::Month m, int d)
{
// assume that y is valid
if (d<=0) return false; // d doit être positif
int days_in_month=31; // la plupart des mois ont 31 jours
switch (m) {
case Date::feb: // the nombre de jours de Février varie
days_in_month = (leapyear(y))?29:28;
break;
case Date::apr: case Date::jun: case Date::sep: case Date::nov:
days_in_month = 30; // les autres mois ont 30 jours
break;
}
if (days_in_month<d) return false;
if (m<Date::jan ||Date::dec<m) return false;
return true;
}
//------------------------------------------------------------------------------
bool leapyear(int y)
{
return !((y%4 != 0) || (y%100 == 0 && y%400 != 0));
}
//------------------------------------------------------------------------------
bool operator==(const Date& a, const Date& b)
{
return a.year()==b.year()
&& a.month()==b.month()
&& a.day()==b.day();
}
//------------------------------------------------------------------------------
bool operator!=(const Date& a, const Date& b)
{
return !(a==b);
}
//------------------------------------------------------------------------------
ostream& operator<<(ostream& os, const Date& d)
{
return os << '(' << d.year()
<< ',' << d.month()
<< ',' << d.day()
<< ')';
}
//------------------------------------------------------------------------------
istream& operator>>(istream& is, Date& dd)
{
int y, m, d;
char ch1, ch2, ch3, ch4;
is >> ch1 >> y >> ch2 >> m >> ch3 >> d >> ch4;
if (!is) return is;
if (ch1!='(' || ch2!=',' || ch3!=',' || ch4!=')') { // oops: erreur de format
is.clear(ios_base::failbit); // positionne le bit indiquant l'échec
return is;
}
dd = Date(y,Date::Month(m),d); // modifie dd
return is;
}
//------------------------------------------------------------------------------
enum Day {
sunday,monday,tuesday,wednesday,thursday,friday,saturday
};
Day day_of_week(const Date& d)
//...
{
int x = first_day + linear_day(d);
return Day(x%7);
}
//------------------------------------------------------------------------------
Date next_Sunday(const Date& d)
{
// ...
Day dd = day_of_week(d);
Date ns = d;
ns.add_day(7-dd);
return ns;
}
//------------------------------------------------------------------------------
Date next_weekday(const Date& d)
{
// ...
Day dd = day_of_week(d);
int n = 1;
switch(dd) {
case friday: // skip Saturday and Sunday
n = 3;
break;
case saturday: // Skip Sunday
n = 2;
break;
}
return d+n;
}
//------------------------------------------------------------------------------
} // Chrono |
Partager