summaryrefslogtreecommitdiff
path: root/lib/utility/Date.cs
blob: c62e5c2081826dfcf27da8e8cd3f4466ac570934 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System;
using System.Globalization;

namespace jive.utility
{
  [Serializable]
  public class Date :  IComparable<Date>, IComparable, IEquatable<Date>
  {
    readonly long ticks;

    public Date(int year, int month, int day)
    {
      ticks = new DateTime(year, month, day).Ticks;
    }

    public bool is_in(Year the_year)
    {
      return the_year.represents(to_date_time());
    }

    public DateTime to_date_time()
    {
      return new DateTime(ticks);
    }

    static public implicit operator Date(DateTime date)
    {
      return new Date(date.Year, date.Month, date.Day);
    }

    static public implicit operator DateTime(Date date)
    {
      return date.to_date_time();
    }

    public int CompareTo(Date other)
    {
      var the_other_date = other.downcast_to<Date>();
      if (ticks.Equals(the_other_date.ticks))
      {
        return 0;
      }
      return ticks > the_other_date.ticks ? 1 : -1;
    }

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

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

    public override int GetHashCode()
    {
      return ticks.GetHashCode();
    }

    public static bool operator ==(Date left, Date right)
    {
      return Equals(left, right);
    }

    public static bool operator !=(Date left, Date right)
    {
      return !Equals(left, right);
    }

    public override string ToString()
    {
      return new DateTime(ticks, DateTimeKind.Local).ToString("MMM dd yyyy", CultureInfo.InvariantCulture);
    }

    int IComparable.CompareTo(object obj)
    {
      if (obj.is_an_implementation_of<Date>())
        return CompareTo(obj.downcast_to<Date>());
      throw new InvalidOperationException();
    }
  }
}