main
1namespace domain
2{
3 using System;
4
5 public class Month : IComparable<Month>, IIncrementable<Month>
6 {
7 DateTime date;
8 public static readonly Month Infinity = new Month(2099, 12);
9
10 public Month(int year, int month)
11 {
12 date = new DateTime(year, month, 01);
13 }
14
15 public Month Plus(int months)
16 {
17 return ToMonth(date.AddMonths(months));
18 }
19
20 public bool IsBefore(Month other)
21 {
22 return this.CompareTo(other) < 0;
23 }
24
25 Month ToMonth(DateTime date)
26 {
27 return new Month(date.Year, date.Month);
28 }
29
30 static public Month Now()
31 {
32 var now = Clock.Now();
33 return new Month(now.Year, now.Month);
34 }
35
36 public int CompareTo(Month other)
37 {
38 return this.date.CompareTo(other.date);
39 }
40
41 public Month Next()
42 {
43 return Plus(1);
44 }
45
46 public bool Equals(Month other)
47 {
48 if (ReferenceEquals(null, other)) return false;
49 if (ReferenceEquals(this, other)) return true;
50 return other.date == date;
51 }
52
53 public override bool Equals(object obj)
54 {
55 if (ReferenceEquals(null, obj)) return false;
56 if (ReferenceEquals(this, obj)) return true;
57 if (obj.GetType() != typeof (Month)) return false;
58 return Equals((Month) obj);
59 }
60
61 public override int GetHashCode()
62 {
63 unchecked
64 {
65 return (date.Year*397) ^ date.Month;
66 }
67 }
68
69 public override string ToString()
70 {
71 return string.Format("{0} {1}", date.Year, date.Month);
72 }
73 }
74}