summaryrefslogtreecommitdiff
path: root/src/domain/Month.cs
blob: 92b82f35ccb565f3fa672f6ab54303bb591db26e (plain)
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
namespace domain
{
  using System;

  public class Month : IComparable<Month>, IIncrementable<Month>
  {
    DateTime date;
    public static readonly Month Infinity = new Month(2099, 12);

    public Month(int year, int month)
    {
      date = new DateTime(year, month, 01);
    }

    public Month Plus(int months)
    {
      return ToMonth(date.AddMonths(months));
    }

    public bool IsBefore(Month other)
    {
      return this.CompareTo(other) < 0;
    }

    Month ToMonth(DateTime date)
    {
      return new Month(date.Year, date.Month);
    }

    static public Month Now()
    {
      var now = Clock.Now();
      return new Month(now.Year, now.Month);
    }

    public int CompareTo(Month other)
    {
      return this.date.CompareTo(other.date);
    }

    public Month Next()
    {
      return Plus(1);
    }

    public bool Equals(Month other)
    {
      if (ReferenceEquals(null, other)) return false;
      if (ReferenceEquals(this, other)) return true;
      return other.date == date;
    }

    public override bool Equals(object obj)
    {
      if (ReferenceEquals(null, obj)) return false;
      if (ReferenceEquals(this, obj)) return true;
      if (obj.GetType() != typeof (Month)) return false;
      return Equals((Month) obj);
    }

    public override int GetHashCode()
    {
      unchecked
      {
        return (date.Year*397) ^ date.Month;
      }
    }

    public override string ToString()
    {
      return string.Format("{0} {1}", date.Year, date.Month);
    }
  }
}