main
 1using System;
 2using System.Globalization;
 3using gorilla.commons.utility;
 4
 5namespace Gorilla.Commons.Utility
 6{
 7    [Serializable]
 8    public class Date :  IComparable<Date>, IComparable, IEquatable<Date>
 9    {
10        readonly long ticks;
11        static public readonly Date First = new Date(DateTime.MinValue);
12        static public readonly Date Last = new Date(DateTime.MaxValue);
13
14        public Date( DateTime date)
15        {
16            this.ticks = date.Date.Ticks;
17        }
18
19        public DateTime to_date_time()
20        {
21            return new DateTime(ticks);
22        }
23
24        static public implicit operator Date(DateTime date)
25        {
26            return new Date(date);
27        }
28
29        static public implicit operator DateTime(Date date)
30        {
31            return date.to_date_time();
32        }
33
34        public int CompareTo(Date other)
35        {
36            var the_other_date = other.downcast_to<Date>();
37            if (ticks.Equals(the_other_date.ticks))
38            {
39                return 0;
40            }
41            return ticks > the_other_date.ticks ? 1 : -1;
42        }
43
44        public bool Equals(Date other)
45        {
46            if (ReferenceEquals(null, other)) return false;
47            if (ReferenceEquals(this, other)) return true;
48            return other.ticks == ticks;
49        }
50
51        public override bool Equals(object obj)
52        {
53            if (ReferenceEquals(null, obj)) return false;
54            if (ReferenceEquals(this, obj)) return true;
55            if (obj.GetType() != typeof (Date)) return false;
56            return Equals((Date) obj);
57        }
58
59        public override int GetHashCode()
60        {
61            return ticks.GetHashCode();
62        }
63
64        public static bool operator ==(Date left, Date right)
65        {
66            return Equals(left, right);
67        }
68
69        public static bool operator !=(Date left, Date right)
70        {
71            return !Equals(left, right);
72        }
73
74        public override string ToString()
75        {
76            return new DateTime(ticks, DateTimeKind.Local).ToString("MMM dd yyyy", CultureInfo.InvariantCulture);
77        }
78
79        int IComparable.CompareTo(object obj)
80        {
81            if (obj.is_an_implementation_of<Date>())
82                return CompareTo(obj.downcast_to<Date>());
83            throw new InvalidOperationException();
84        }
85    }
86}