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
|
#include <iostream>
#include <math.h>
#include <string>
using namespace std;
const double mnum = 60.0, hnum = 3600.0, Dnum = 24.0 * 3600.0,
Mnum = 30.0 * 24.0 * 3600.0, Ynum = 360.0 * 24.0 * 3600.0;
class DateTime {
public:
//Variables
int s, m, h, D, M, Y;
double sign;
//Constructors and destructors
DateTime(int nh=0, int nm = 0, int ns = 0, int nD = 0, int nM = 0, int nY = 0)
{ sign = 1.0; h = nh; m = nm; s = ns; D = nD; M = nM; Y = nY; }
~DateTime() {}
//Functions
void set_date(int nD=0, int nM=0, int nY=0) { D = nD; M = nM; Y = nY; }
void set_time(int nh=0, int nm=0, int ns=0) { s = ns; m = nm; h = nh; }
void print_all() const;
//Conversions
double seconds() const; void iseconds(double);
double minutes() const { return (this->seconds()) / mnum; };
double hours() const { return (this->seconds()) / hnum; };
double days() const { return (this->seconds()) / Dnum; };
double months() const { return (this->seconds()) / Mnum; };
double years() const { return (this->seconds()) / Ynum; };
//Operations
DateTime operator+ (const DateTime& dt);
DateTime operator- (const DateTime& dt);
};
void DateTime::iseconds(double ovs) {
if (ovs < 0) { sign = -1.0; ovs = ovs*sign; }
double nY, nM, nD, nh, nm, ns;
nY = floor(ovs / Ynum); ovs = ovs - nY*Ynum;
nM = floor(ovs / Mnum); ovs = ovs - nM*Mnum;
nD = floor(ovs / Dnum); ovs = ovs - nD*Dnum;
nh = floor(ovs / hnum); ovs = ovs - nh*hnum;
nm = floor(ovs / mnum); ovs = ovs - nm*mnum;
Y = (int)nY; M = (int)nM + 1; D = (int)nD + 1; h = (int)nh; m = (int)nm; s = (int)ovs;
}
double DateTime::seconds() const {
return (double) (Y*(int)Ynum+(M-1)*(int)Mnum+(D-1)*(int)Dnum+h*(int)hnum+m*(int)mnum+s);
}
DateTime DateTime::operator+ (const DateTime& dt) {
DateTime ndt;
double s1 = (this->sign)*(this->seconds()), s2 = (dt.sign)*(dt.seconds());
ndt.iseconds(s1 + s2);
return ndt;
}
DateTime DateTime::operator- (const DateTime& dt) {
DateTime ndt;
double s1 = (this->sign)*(this->seconds()), s2 = (dt.sign)*(dt.seconds());
ndt.iseconds(s1 - s2);
return ndt;
}
void DateTime::print_all() const {
string str0 = "", str1 = "", str2 = "", str3 = "";
if (sign < 0) str0 = "- ";
if (h < 10) str1 = "0";
if (m < 10) str2 = "0";
if (s < 10) str3 = "0";
string month = "Jan";
if (M == 2) month = "Feb";
if (M == 3) month = "Mar";
if (M == 4) month = "Apr";
if (M == 5) month = "May";
if (M == 6) month = "Jun";
if (M == 7) month = "Jul";
if (M == 8) month = "Aug";
if (M == 9) month = "Sep";
if (M == 10) month = "Oct";
if (M == 11) month = "Nov";
if (M == 12) month = "Dec";
cout << str0 << str1 << h << ":" << str2 << m << ":" << str3 << s
<< ", " << D << " " << month << " " << Y;
}
|