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