123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- #include <iostream>
- #include <iomanip>
- #include <string>
- #include <math.h>
- class Datetime
- {
- private:
- double julian_days = 0;
- void init(int days)
- {
- julian_days = days;
- }
- void init(int year, int month, int days)
- {
- int a = int(14 - month) / 12;
- int y = year + 4800 - a;
- int m = month + 12*a - 3;
- julian_days = days + int((153*m + 2) / 5) + 365*y + int(y/4) - int(y/100) + int(y/400) - 32045;
- }
- void init(int year, int month, int days, int hours, int minuts, int seconds)
- {
- int a = int(14 - month) / 12;
- int y = year + 4800 - a;
- int m = month + 12*a - 3;
- julian_days = days + int((153*m + 2) / 5) + 365*y + int(y/4) - int(y/100) + int(y/400) - 32045;
- julian_days += double(hours - 12) / 24.0 + double(minuts) / 1440.0 + double(seconds) / 86400.0;
- }
- public:
- Datetime(int days)
- {
- init(days);
- }
- Datetime(int year, int month, int days)
- {
- init(year, month, days);
- }
- Datetime(int year, int month, int days, int hours, int minuts, int seconds)
- {
- init(year, month, days, hours, minuts, seconds);
- }
- Datetime(std::string data)
- {
- int current = 0;
- int dates[6] = {0,0,0,0,0,0};
- int date_index = 0;
- for (int i = 0; i < data.length(); i++) {
- if (data[i] == ':' or data[i] == '-' or data[i] == '.' or data[i] == ' ') {
- if (date_index > 5)
- throw "Неправильный формат даты";
- dates[date_index] = current ;
- date_index++;
- current = 0;
- continue;
- }
- current = current*10 + int(data[i] - '0');
- }
- dates[date_index] = current;
- init(dates[0], dates[1], dates[2], dates[3], dates[4], dates[5]);
- }
- friend std::ostream &operator <<(std::ostream &os, Datetime &dt)
- {
- int a = dt.julian_days + 32044;
- int b = (4*a + 3) / 146097;
- int c = a - ((146097*b) / 4);
- int d = (4*c + 3) / 1461;
- int e = c - ((1461*d) / 4);
- int m = (5*e + 2) / 153;
- int days = e - ((153*m + 2) / 5) + 1;
- int month = m + 3 - 12 * (m/10);
- int years = 100*b + d - 4800 + (m/10);
- int hours = ((dt.julian_days - double(int(dt.julian_days)))*24 + 12*24) / 24;
- int minuts = ((dt.julian_days - double(int(dt.julian_days))) * 1440) - int(dt.julian_days - double(int(dt.julian_days)))*24;
- int seconds = (dt.julian_days - double(int(dt.julian_days))) * 86400 - minuts*60 - int(dt.julian_days - double(int(dt.julian_days)))*24;
- os << std::setw(4) << std::setfill('0') << years << ':'
- << std::setw(2) << std::setfill('0') << month << ':'
- << std::setw(2) << std::setfill('0') << days << 'T'
- << std::setw(2) << std::setfill('0') << hours << ':'
- << std::setw(2) << std::setfill('0') << minuts << ':'
- << std::setw(2) << std::setfill('0') << seconds;
- return os;
- }
- Datetime& operator += (const Datetime& r)
- {
- julian_days += r.julian_days;
- return *this;
- }
- Datetime operator + (const Datetime& r) const
- {
- Datetime res(*this);
- return res += r;
- }
- Datetime& operator -= (const Datetime& r)
- {
- julian_days -= r.julian_days;
- return *this;
- }
- Datetime operator - (const Datetime& r) const
- {
- Datetime res(*this);
- return res -= r;
- }
- };
|