1 |
7a998d66
|
Eliška Mourycová
|
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 |
55d35561
|
Eliška Mourycová
|
/// <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 |
7a998d66
|
Eliška Mourycová
|
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 |
c4383c00
|
Eliška Mourycová
|
/// <summary>
|
42 |
|
|
/// Parses the date given in format: 1-2020
|
43 |
|
|
/// </summary>
|
44 |
|
|
/// <param name="str">The string to parse</param>
|
45 |
|
|
/// <returns>A new date</returns>
|
46 |
|
|
public static Date ParseDate(string str)
|
47 |
|
|
{
|
48 |
|
|
|
49 |
|
|
uint month, year;
|
50 |
|
|
|
51 |
|
|
string ds = str.Trim();
|
52 |
|
|
string[] splits = ds.Split('-');
|
53 |
|
|
|
54 |
|
|
if(splits.Length != 2)
|
55 |
|
|
{
|
56 |
|
|
Console.WriteLine("Unknown date format. Must be e.g. 1-2020.");
|
57 |
|
|
return null;
|
58 |
|
|
}
|
59 |
|
|
|
60 |
|
|
bool monthOk = uint.TryParse(splits[0], out month);
|
61 |
|
|
bool yearOk = uint.TryParse(splits[1], out year);
|
62 |
|
|
|
63 |
|
|
if(!monthOk || !yearOk)
|
64 |
|
|
{
|
65 |
|
|
Console.WriteLine("Unknown date format. Must be e.g. 1-2020.");
|
66 |
|
|
return null;
|
67 |
|
|
}
|
68 |
|
|
|
69 |
|
|
|
70 |
|
|
try
|
71 |
|
|
{
|
72 |
|
|
Date d = new Date(month, year);
|
73 |
|
|
return d;
|
74 |
|
|
}
|
75 |
|
|
catch (Exception ex)
|
76 |
|
|
{
|
77 |
|
|
return null;
|
78 |
|
|
}
|
79 |
|
|
|
80 |
|
|
|
81 |
|
|
return null;
|
82 |
|
|
}
|
83 |
|
|
|
84 |
7a998d66
|
Eliška Mourycová
|
|
85 |
|
|
#region OVERRIDEN METHODS FOR OF THE OBJECT CLASS
|
86 |
|
|
public override bool Equals(object obj)
|
87 |
|
|
{
|
88 |
d358b79e
|
Roman Kalivoda
|
if (obj is null)
|
89 |
|
|
{
|
90 |
|
|
return false;
|
91 |
|
|
}
|
92 |
|
|
|
93 |
|
|
if (obj.GetType() != typeof(Date))
|
94 |
7a998d66
|
Eliška Mourycová
|
return false;
|
95 |
|
|
|
96 |
|
|
Date other = obj as Date;
|
97 |
|
|
if (other.Month == this.Month && other.Year == this.Year)
|
98 |
|
|
return true;
|
99 |
|
|
return false;
|
100 |
|
|
}
|
101 |
|
|
|
102 |
|
|
public override int GetHashCode()
|
103 |
|
|
{
|
104 |
|
|
int hashCode = -994906903;
|
105 |
|
|
hashCode = hashCode * -1521134295 + Month.GetHashCode();
|
106 |
|
|
hashCode = hashCode * -1521134295 + Year.GetHashCode();
|
107 |
|
|
return hashCode;
|
108 |
|
|
}
|
109 |
|
|
|
110 |
|
|
public override string ToString()
|
111 |
|
|
{
|
112 |
|
|
string mon = Month > 9 ? $"{Month}" : "0" + Month;
|
113 |
|
|
return $"{mon}-{Year}";
|
114 |
|
|
}
|
115 |
|
|
|
116 |
|
|
#endregion
|
117 |
|
|
|
118 |
|
|
#region OVERLOADED OPERATORS
|
119 |
|
|
public static bool operator >(Date d1, Date d2)
|
120 |
|
|
{
|
121 |
|
|
if (d1.Year > d2.Year)
|
122 |
|
|
return true;
|
123 |
|
|
else if (d1.Year < d2.Year)
|
124 |
|
|
return false;
|
125 |
|
|
else
|
126 |
|
|
{
|
127 |
|
|
// the years are equal
|
128 |
|
|
if (d1.Month > d2.Month)
|
129 |
|
|
return true;
|
130 |
|
|
else return false;
|
131 |
|
|
}
|
132 |
|
|
|
133 |
|
|
}
|
134 |
|
|
|
135 |
|
|
public static bool operator <(Date d1, Date d2)
|
136 |
|
|
{
|
137 |
|
|
return !(d1 >= d2);
|
138 |
|
|
}
|
139 |
|
|
|
140 |
|
|
|
141 |
|
|
public static bool operator >=(Date d1, Date d2)
|
142 |
|
|
{
|
143 |
|
|
if (d1.Year > d2.Year)
|
144 |
|
|
return true;
|
145 |
|
|
else if (d1.Year < d2.Year)
|
146 |
|
|
return false;
|
147 |
|
|
else
|
148 |
|
|
{
|
149 |
|
|
// the years are equal
|
150 |
|
|
if (d1.Month > d2.Month)
|
151 |
|
|
return true;
|
152 |
|
|
else if (d1.Month == d2.Month)
|
153 |
|
|
return true;
|
154 |
|
|
else return false;
|
155 |
|
|
}
|
156 |
|
|
}
|
157 |
|
|
|
158 |
|
|
public static bool operator <=(Date d1, Date d2)
|
159 |
|
|
{
|
160 |
|
|
return !(d1 > d2);
|
161 |
|
|
}
|
162 |
|
|
|
163 |
6d0d1410
|
Eliška Mourycová
|
//public static bool operator ==(Date d1, Date d2)
|
164 |
|
|
//{
|
165 |
|
|
// return d1.Equals(d2);
|
166 |
|
|
//}
|
167 |
|
|
|
168 |
|
|
//public static bool operator !=(Date d1, Date d2)
|
169 |
|
|
//{
|
170 |
|
|
// return !d1.Equals(d2);
|
171 |
|
|
//}
|
172 |
7a998d66
|
Eliška Mourycová
|
|
173 |
|
|
#endregion
|
174 |
|
|
}
|
175 |
|
|
}
|