1
|
using System;
|
2
|
using System.Collections.Generic;
|
3
|
using System.Linq;
|
4
|
using System.Text;
|
5
|
using System.Threading.Tasks;
|
6
|
|
7
|
namespace ServerApp.DataDownload
|
8
|
{
|
9
|
public class Date
|
10
|
{
|
11
|
public uint Month { get; }
|
12
|
public uint Year { get; }
|
13
|
public Date(uint month, uint year)
|
14
|
{
|
15
|
if (month == 0)
|
16
|
throw new ArgumentOutOfRangeException("month", "Month must be positive and not zero.");
|
17
|
this.Month = month;
|
18
|
this.Year = year;
|
19
|
}
|
20
|
|
21
|
|
22
|
/// <summary>
|
23
|
/// Increases the month by one. Increases year and sets month to 1 if need be.
|
24
|
/// </summary>
|
25
|
/// <returns>An increased date</returns>
|
26
|
public Date IncreaseMonthByOne()
|
27
|
{
|
28
|
uint newMonth = Month;
|
29
|
newMonth++;
|
30
|
uint newYear = Year;
|
31
|
if (newMonth > 12)
|
32
|
{
|
33
|
// newMonth must be 13
|
34
|
newMonth = 1;
|
35
|
newYear++;
|
36
|
}
|
37
|
|
38
|
return new Date(newMonth, newYear);
|
39
|
}
|
40
|
|
41
|
|
42
|
#region OVERRIDEN METHODS FOR OF THE OBJECT CLASS
|
43
|
public override bool Equals(object obj)
|
44
|
{
|
45
|
if (obj is null)
|
46
|
{
|
47
|
return false;
|
48
|
}
|
49
|
|
50
|
if (obj.GetType() != typeof(Date))
|
51
|
return false;
|
52
|
|
53
|
Date other = obj as Date;
|
54
|
if (other.Month == this.Month && other.Year == this.Year)
|
55
|
return true;
|
56
|
return false;
|
57
|
}
|
58
|
|
59
|
public override int GetHashCode()
|
60
|
{
|
61
|
int hashCode = -994906903;
|
62
|
hashCode = hashCode * -1521134295 + Month.GetHashCode();
|
63
|
hashCode = hashCode * -1521134295 + Year.GetHashCode();
|
64
|
return hashCode;
|
65
|
}
|
66
|
|
67
|
public override string ToString()
|
68
|
{
|
69
|
string mon = Month > 9 ? $"{Month}" : "0" + Month;
|
70
|
return $"{mon}-{Year}";
|
71
|
}
|
72
|
|
73
|
#endregion
|
74
|
|
75
|
#region OVERLOADED OPERATORS
|
76
|
public static bool operator >(Date d1, Date d2)
|
77
|
{
|
78
|
if (d1.Year > d2.Year)
|
79
|
return true;
|
80
|
else if (d1.Year < d2.Year)
|
81
|
return false;
|
82
|
else
|
83
|
{
|
84
|
// the years are equal
|
85
|
if (d1.Month > d2.Month)
|
86
|
return true;
|
87
|
else return false;
|
88
|
}
|
89
|
|
90
|
}
|
91
|
|
92
|
public static bool operator <(Date d1, Date d2)
|
93
|
{
|
94
|
return !(d1 >= d2);
|
95
|
}
|
96
|
|
97
|
|
98
|
public static bool operator >=(Date d1, Date d2)
|
99
|
{
|
100
|
if (d1.Year > d2.Year)
|
101
|
return true;
|
102
|
else if (d1.Year < d2.Year)
|
103
|
return false;
|
104
|
else
|
105
|
{
|
106
|
// the years are equal
|
107
|
if (d1.Month > d2.Month)
|
108
|
return true;
|
109
|
else if (d1.Month == d2.Month)
|
110
|
return true;
|
111
|
else return false;
|
112
|
}
|
113
|
}
|
114
|
|
115
|
public static bool operator <=(Date d1, Date d2)
|
116
|
{
|
117
|
return !(d1 > d2);
|
118
|
}
|
119
|
|
120
|
public static bool operator ==(Date d1, Date d2)
|
121
|
{
|
122
|
return d1.Equals(d2);
|
123
|
}
|
124
|
|
125
|
public static bool operator !=(Date d1, Date d2)
|
126
|
{
|
127
|
return !d1.Equals(d2);
|
128
|
}
|
129
|
|
130
|
#endregion
|
131
|
}
|
132
|
}
|