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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
|
#include <cstdlib>
#include <iostream>
#include <ctime>
#include <iomanip>
#include <string>
using namespace std;
class DateTime
{
public:
void output ();
void setTime(int ,int ,int);
string getdayOfTheWeek ();
DateTime ();
DateTime (const DateTime&);
DateTime(int ,int ,int );
DateTime (const tm*);
DateTime& operator = (const DateTime&);
DateTime operator + (const int &);
DateTime operator - (const int &);
DateTime operator ++ (int);
DateTime operator ++ ( );
DateTime operator -- (int);
DateTime operator -- ( );
long int operator - (DateTime);
friend ostream& operator << (ostream&, DateTime&);
private:
int year;
int day;
int month1;
string month;
int dow;
string dayOfTheWeek;
int hour;
int min;
int sec;
};
DateTime::DateTime ()
{
time_t t = time(0);
struct tm* src = localtime (&t);
string mnth[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
string wkday[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
year = src->tm_year + 1900;
day = src->tm_mday;
month = mnth [month1];
month1 = src->tm_mon;
dow= src->tm_wday;
dayOfTheWeek = wkday [dow];
hour = src->tm_hour;
min = src->tm_min;
sec = src->tm_sec;
}
DateTime::DateTime (const DateTime& copy)
{
year = copy.year;
day = copy.day;
month = copy.month;
dayOfTheWeek = copy.dayOfTheWeek;
hour = copy.hour;
min = copy.min;
sec = copy.sec;
}
DateTime::DateTime (const tm* copy)
{
string mnth[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
string wkday[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
year = copy-> tm_year+ 1900;
day = copy->tm_mday;
month = mnth [copy->tm_mon];
dayOfTheWeek = wkday [copy->tm_wday];
hour = copy->tm_hour;
min = copy->tm_min;
sec = copy->tm_sec;
}
DateTime:: DateTime(int inputmonth1, int inputday ,int inputyear )
{
if ((inputmonth1<12 && inputmonth1>=0)&&(inputday<12 && inputday>=0)&&(inputyear<12 && inputyear>=0))
{
time_t adjust1= time (0);
struct tm * adjust;
inputyear = adjust->tm_year + 1900;
inputday = adjust->tm_mday;
inputmonth1 = adjust->tm_mon;
mktime (adjust);
adjust= localtime(&adjust1);
year=inputyear;
day=inputday;
month1=inputmonth1;
dow=adjust->tm_wday;
hour=adjust->tm_hour;
min=adjust->tm_min;
sec=adjust->tm_sec;
}
else
{
time_t t = time(0);
struct tm* src = localtime (&t);
year = src->tm_year + 1900;
day = src->tm_mday;;
month1 = src->tm_mon;
dow= src->tm_wday;
hour = src->tm_hour;
min = src->tm_min;
sec = src->tm_sec;
}
}
void DateTime::setTime(int hr,int mn,int sc)
{
if ((hr<24 && hr>0) && (mn<60 && mn>0) && (sc < 60 && sc>0))
{
time_t set= time (0);
struct tm * setter;
hr=setter->tm_hour;
mn=setter->tm_min;
sc=setter->tm_sec;
mktime (setter);
setter= localtime (&set);
}
else
{
time_t t = time(0);
struct tm* src = localtime (&t);
hour = src->tm_hour;
min = src->tm_min;
sec = src->tm_sec;
}
}
string DateTime:: getdayOfTheWeek ( )
{
return dayOfTheWeek;
}
void DateTime::output ()
{
cout << this->dayOfTheWeek << ". "
<< this->month << ". "
<< this->day << ", "
<< this->year << " "
<< this->hour << ":" << this->min << ":" << this->sec << " "
<< endl;
}
DateTime& DateTime::operator = (const DateTime& same)
{
year = same.year;
day = same.day;
month = same.month;
dayOfTheWeek = same.dayOfTheWeek;
hour = same.hour;
min = same.min;
sec = same.sec;
return *this;
}
DateTime DateTime:: operator + (const int& operand)
{
time_t tNew = time (0);
struct tm * convert;
this->year = convert->tm_year + 1900;
this->day = convert->tm_mday;
this->month1 = convert->tm_mon; //convert string to int
this->dow = convert->tm_wday;//convert string to int
this->hour = convert->tm_hour;
this->min = convert->tm_min;
this->sec = convert->tm_sec;
tNew = mktime (convert) + (operand * 86400);
convert = localtime (&tNew);
DateTime tomorrow (convert);
return tomorrow;
}
DateTime DateTime:: operator ++ ( )
{
time_t tNew=time (0);
struct tm * convert1;
this->year = convert1->tm_year + 1900;
this->day = convert1->tm_mday;
this->month1 = convert1->tm_mon;
this->dow = convert1->tm_wday;
this->hour = convert1->tm_hour;
this->min = convert1->tm_min;
this->sec = convert1->tm_sec;
tNew= mktime (convert1);
convert1 = localtime (&tNew);
DateTime tomorrow2 (convert1);
return tomorrow2;
}
DateTime DateTime:: operator ++ (int )
{
time_t tNew= time (0);
struct tm * convert2;
this->year = convert2->tm_year + 1900;
this->day = convert2->tm_mday;
this->month1 = convert2->tm_mon; //convert string to int
this->dow = convert2->tm_wday;
this->hour = convert2->tm_hour;
this->min = convert2->tm_min;
this->sec = convert2->tm_sec;
tNew= mktime (convert2) + 86400;
convert2 = localtime (&tNew);
DateTime tomorrow3 (convert2);
return tomorrow3;
}
DateTime DateTime:: operator - (const int& operand)
{
time_t tNew= time (0);
struct tm * convert;
this->year = convert->tm_year + 1900;
this->day = convert->tm_mday;
this->month1 = convert->tm_mon; //convert string to int
this->dow = convert->tm_wday;
this->hour = convert->tm_hour;
this->min = convert->tm_min;
this->sec = convert->tm_sec;
tNew = mktime (convert) - (operand * 86400);
convert = localtime (&tNew);
DateTime yesterday (convert);
return yesterday;
}
DateTime DateTime:: operator -- ( )
{
time_t tNew=time (0);
struct tm * convert1;
this->year = convert1->tm_year + 1900;
this->day = convert1->tm_mday;
this->month1 = convert1->tm_mon; //convert string to int
this->dow = convert1->tm_wday;
this->hour = convert1->tm_hour;
this->min = convert1->tm_min;
this->sec = convert1->tm_sec;
tNew= mktime (convert1);
convert1 = localtime (&tNew);
DateTime yesterday2 (convert1);
return yesterday2;
}
DateTime DateTime:: operator -- (int )
{
time_t tNew=time (0);
struct tm * convert2;
this->year = convert2->tm_year + 1900;
this->day = convert2->tm_mday;
this->month1 = convert2->tm_mon; //convert string to int
this->dow = convert2->tm_wday;
this->hour = convert2->tm_hour;
this->min = convert2->tm_min;
this->sec = convert2->tm_sec;
tNew= mktime (convert2) -86400;
convert2 = localtime (&tNew);
DateTime yesterday3 (convert2);
return yesterday3;
}
long int DateTime:: operator -(DateTime operand)
{
time_t current =time (0);
time_t change = time (0);
struct tm * convert;
this->year= convert->tm_year-1900;
this->day = convert->tm_mday;
this->month1 = convert->tm_mon;
this->dow = convert->tm_wday;
this->hour = convert->tm_hour;
this->min = convert->tm_min;
this->sec = convert->tm_sec;
convert= localtime (¤t);
struct tm * convertoperand;
operand.year= convertoperand->tm_year-1900;
operand.day = convertoperand->tm_mday;
operand.month1= convertoperand->tm_mon;
operand.dow = convertoperand->tm_wday;
operand.hour = convertoperand->tm_hour;
operand.min = convertoperand->tm_min;
operand.sec = convertoperand->tm_sec;
convertoperand= localtime(&change);
current= mktime(convert);
change= mktime (convertoperand);
long int Difference= difftime (current,change);
return Difference;
}
ostream& operator << (ostream& strm, DateTime& dttm)
{
strm << dttm.dayOfTheWeek << ". "
<< dttm.month << ". "
<< dttm.day << ", "
<< dttm.year << " "
<< dttm.hour << ":" << dttm.min << ":" << dttm.sec << " "
<< endl;
return strm;
}
|