Last active
June 1, 2019 14:56
-
-
Save boki1/6bc687c1b1aaf046724e130256730f17 to your computer and use it in GitHub Desktop.
Implementation of struct date
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using std::ostream, std::cout; | |
#include "date.h" | |
namespace ttime | |
{ | |
date::date() : day(1), month(jan), year(1970) | |
{ | |
this->month_lens[feb] += this->is_leap(); | |
} | |
date::date(unsigned day, enum month month, unsigned year) : day(day), month(month), year(year) | |
{ | |
this->month_lens[feb] += this->is_leap(); | |
} | |
bool date::is_leap() const | |
{ | |
return this->year % 4 == 0 && this->year % 100 != 0; | |
} | |
ostream &operator<<(ostream &l, const date &d) | |
{ | |
const char *const months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", | |
"Dec" }; | |
return l << d.day << '.' << months[d.month] << '.' << d.year << '\n'; | |
} | |
bool operator<(const date &l, const date &r) | |
{ | |
return l.year < r.year || (l.year == r.year && (l.month < r.month || (l.month == r.month && l.day < r.day))); | |
} | |
bool operator>(const date &l, const date &r) | |
{ | |
return r < l; | |
} | |
bool operator>=(const date &l, const date &r) | |
{ | |
return !(l < r); | |
} | |
bool operator<=(const date &l, const date &r) | |
{ | |
return !(l > r); | |
} | |
bool operator!=(const date &l, const date &r) | |
{ | |
return l > r || r > l; | |
} | |
bool operator==(const date &l, const date &r) | |
{ | |
return !(l != r); | |
} | |
date operator+(const date &This, unsigned num) | |
{ | |
unsigned d = This.day, m = This.month, y = This.year; | |
d += num; | |
if (d > This.month_lens[m]) | |
{ | |
d -= This.month_lens[m]; | |
if (++m > 12) | |
{ | |
m -= 12; | |
++y; | |
} | |
} | |
date res(d, (enum month) m, y); | |
return res; | |
} | |
date operator++(date &This) | |
{ | |
This = This + 1; | |
return This; | |
} | |
unsigned operator-(const date &This, const date &other) | |
{ | |
if (This == other) return 0; | |
if (This < other) return other - This; | |
date &smaller = (date &) other; | |
unsigned i; | |
for (i = 0; This > smaller; ++i, ++smaller); | |
return i; | |
} | |
extern "C" void init() | |
{ | |
date first_day(25, feb, 2008), second_day(1, mar, 2008); | |
cout << first_day << second_day; | |
cout << second_day - first_day; | |
} | |
} | |
int main() | |
{ | |
ttime::init(); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef DATE_H | |
#define DATE_H | |
#endif // DATE_H | |
#define months_count 12 | |
#include <ostream> | |
using std::ostream; | |
namespace ttime | |
{ | |
enum month | |
{ | |
jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec | |
}; | |
struct date | |
{ | |
friend ostream &operator<<(ostream &stream, const date &date); | |
friend bool operator<(const date &, const date &); | |
bool is_leap() const; | |
friend unsigned operator-(const date &, const date &); | |
friend date operator+(const date &, unsigned); | |
date(unsigned, enum month, unsigned); | |
date(); | |
private: | |
unsigned day:5, month:4, year:32 - 5 - 4; | |
unsigned month_lens[months_count] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment