This repository has been archived by the owner on Nov 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
date.hpp
177 lines (124 loc) · 4.85 KB
/
date.hpp
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
#ifndef __DATE
#define __DATE
#include <string>
#include <iostream>
#include "type.hpp"
namespace lab2 {
/**
* Abstract class for a Date which specifies required
* functionality of derived classes "Gregorian" and
* "Julian". Read the method comments for detailed
* instructions of how each method should behave.
* Follows the specification:
* http://www.tondering.dk/claus/cal/julperiod.php
* @author Bastian Fredriksson
* @author Sai Man Wong
*/
class Date {
// ------------------------------ PROTECTED ----------------------------
protected:
// ------------------------- Protected variables -------------------------
int mYear, mMonth, mDay;
// ------------------ Additional optional functions -----------------------
/* Convert specified Type to current Type*/
void convert(Type);
/* Get the Julian for specified Type */
int julian_day(Type) const;
/* Get the Julian for this object */
int julian_day() const;
/* Increments the current day by n */
Date& add_days(int);
/* Increments the current day by 1 */
Date& add_day();
/* Decrements the current day by n */
Date& remove_days(int);
/* Decrements the current day by 1 */
Date& remove_day();
// ------------------------------- PUBLIC --------------------------
private:
// ------------------ Additional optional functions -----------------------
/* Checks leap year */
bool is_leap_year() const;
public:
// ------------------ Additional optional functions -----------------------
/* Get Type of object */
virtual Type type() const { return Type(D); }
// ------------------ Constructors / destructor ---------------------
/* Construct a date from the triple (year, month, day)
* specified in the date format used by the class. Throws
* std::invalid_argument if an invalid date is given. In
* particular, the date must be in the range 1858-01-01
* to 2558-12-31. */
Date(int year, int month, int day);
/* Copy constructor */
Date (const Date& d);
/* Default constructor */
Date() = default;
/* Important to define the destructor as virtual! */
virtual ~Date() { };
// ----------------------- Mandatory functions -----------------------
/* Returns the year of this date. */
int year() const;
/* Returns the month of this date. */
unsigned int month() const;
/* Returns the day of this date. */
unsigned int day() const;
/* Returns the number associated with the current weekday. */
unsigned int week_day() const;
/* Returns the number of days in a week. */
unsigned int days_per_week() const;
/* Returns the number of days in the current month. */
unsigned int days_this_month() const;
/* Returns the name of the current weekday. */
std::string week_day_name() const;
/* Returns name of current month. */
std::string month_name() const;
/* Increments the current year by 1. */
Date& add_year();
/* Increments the current year by the number specified. */
Date& add_year(int);
/* Increments the current month by 1. */
Date& add_month();
/* Increments the current year by the number specified. */
Date& add_month(int);
/* This function shall return the MJD representation of the current day. */
int mod_julian_day() const;
// -------------------- Virtual operators/functions ------------------
/* Copy assignment operator */
virtual Date& operator=(const Date& d) {
mYear = d.year();
mMonth = d.month();
mDay = d.day();
return *this;
}
/* << operator */
friend std::ostream& operator <<(std::ostream&, const Date&);
/* == Operator */
friend bool operator==(const Date&, const Date&);
/* != Operator */
friend bool operator!=(const Date&, const Date&);
/* - Operator */
friend int operator-(const Date&, const Date&);
/* Postfix increment i++ */
Date operator++(int);
/* Prefix increment ++i */
Date& operator++();
/* Postfix decrement i-- */
Date operator--(int);
/* Prefix decement --i; */
Date& operator--(); // Prefix decrement
/* Addition assignment */
Date& operator+=(int);
/* Subraction assignment */
Date& operator-=(int);
/* Greater than operator */
bool operator>(const Date&) const;
/* Greater than or equal to operator */
bool operator>=(const Date&) const;
/* Less than operator */
bool operator<(const Date&) const;
/* Less than or equal to operator */
bool operator<=(const Date&) const;
};
}
#endif