Pretty-print.
[beagle.git] / Util / Timeline.cs
blob121547700ca3829b963bd48bf11632a9a7c08f84
1 //
2 // Timeline.cs: Store chronological events
3 //
4 // Lukas Lipka <lukas@pmad.net>
5 //
6 // Copyright (C) 2004 Novell, Inc.
7 //
9 using System;
10 using System.Collections;
12 namespace Beagle.Util {
14 public class Timeline {
16 private DateTime TodaySpan;
17 private DateTime YesterdaySpan;
18 private DateTime ThisWeekSpan;
19 private DateTime LastWeekSpan;
21 private ArrayList today;
22 private ArrayList yesterday;
23 private ArrayList thisweek;
24 private ArrayList lastweek;
25 private ArrayList thismonth;
26 private ArrayList thisyear;
27 private ArrayList older;
29 public Timeline ()
31 today = new ArrayList ();
32 yesterday = new ArrayList ();
33 thisweek = new ArrayList ();
34 lastweek = new ArrayList ();
35 thismonth = new ArrayList ();
36 thisyear = new ArrayList ();
37 older = new ArrayList ();
39 UpdateTimespan ();
42 public void UpdateTimespan ()
44 TodaySpan = DateTime.Today;
45 YesterdaySpan = TodaySpan.AddDays (-1);
46 ThisWeekSpan = TodaySpan.AddDays (-((int)DateTime.Now.DayOfWeek));
47 LastWeekSpan = ThisWeekSpan.AddDays (-7);
50 private class TimelineEvent {
51 private DateTime timestamp;
52 private object obj;
54 public DateTime Timestamp {
55 get { return timestamp; }
58 public object Object {
59 get { return obj; }
62 public TimelineEvent (object obj, DateTime timestamp)
64 this.timestamp = timestamp;
65 this.obj = obj;
69 private class ReverseChronoComparer : IComparer {
71 public int Compare (object x, object y)
73 return ((TimelineEvent) y).Timestamp.CompareTo (((TimelineEvent) x).Timestamp);
77 private static ReverseChronoComparer rev_cmp = new ReverseChronoComparer ();
79 private bool IsThisYear (TimelineEvent e)
81 if (e.Timestamp.Year == DateTime.Today.Year)
82 return true;
83 return false;
86 private bool IsThisMonth (TimelineEvent e)
88 if (IsThisYear (e) && e.Timestamp.Month == DateTime.Today.Month)
89 return true;
90 return false;
93 private bool IsLastWeek (TimelineEvent e)
95 if (e.Timestamp >= LastWeekSpan && e.Timestamp < ThisWeekSpan)
96 return true;
97 return false;
100 private bool IsThisWeek (TimelineEvent e)
102 if (e.Timestamp >= ThisWeekSpan)
103 return true;
104 return false;
107 private bool IsToday (TimelineEvent e)
109 if (IsThisYear (e) && e.Timestamp.DayOfYear == DateTime.Today.DayOfYear)
110 return true;
111 return false;
114 private bool IsYesterday (TimelineEvent e)
116 if (e.Timestamp >= YesterdaySpan)
117 return true;
118 return false;
121 public void Add (object obj, DateTime timestamp)
123 TimelineEvent te = new TimelineEvent (obj, timestamp);
125 if (IsToday (te)) today.Add (te);
126 else if (IsYesterday (te)) yesterday.Add (te);
127 else if (IsThisWeek (te)) thisweek.Add (te);
128 else if (IsLastWeek (te)) lastweek.Add (te);
129 else if (IsThisMonth (te)) thismonth.Add (te);
130 else if (IsThisYear (te)) thisyear.Add (te);
131 else older.Add (te);
134 private ArrayList GetObjects (ArrayList list)
136 ArrayList sort = new ArrayList ();
138 list.Sort (rev_cmp);
140 foreach (TimelineEvent timeevent in list)
141 sort.Add (timeevent.Object);
143 return sort;
146 public ArrayList Today {
147 get { return GetObjects (today); }
150 public ArrayList Yesterday {
151 get { return GetObjects (yesterday); }
154 public ArrayList ThisWeek {
155 get { return GetObjects (thisweek); }
158 public ArrayList LastWeek {
159 get { return GetObjects (lastweek); }
162 public ArrayList ThisMonth {
163 get { return GetObjects (thismonth); }
166 public ArrayList ThisYear {
167 get { return GetObjects (thisyear); }
170 public ArrayList Older {
171 get { return GetObjects (older); }
176 #if false
177 public class Driver {
178 public static void Main (string [] args)
180 Timeline t = new Timeline ();
181 t.Add (DateTime.Now, DateTime.Now);
182 t.Add (new DateTime (2004, 12, 25), new DateTime (2004, 12, 25)); //Yesterday
183 t.Add (new DateTime (2004, 12, 12), new DateTime (2004, 12, 1)); //This Month
184 t.Add (new DateTime (2004, 11, 9 ), new DateTime (2004, 11, 9));
187 //DEBUG
188 Console.WriteLine ("Today:");
189 foreach (DateTime d in t.Today)
190 Console.WriteLine ("\t" + d);
192 Console.WriteLine ("Yesterday:");
193 foreach (DateTime d in t.Yesterday)
194 Console.WriteLine ("\t" + d);
196 Console.WriteLine ("ThisWeek:");
197 foreach (DateTime d in t.ThisWeek)
198 Console.WriteLine ("\t" + d);
200 Console.WriteLine ("ThisMonth:");
201 foreach (DateTime d in t.ThisMonth)
202 Console.WriteLine ("\t" + d);
204 Console.WriteLine ("ThisYear:");
205 foreach (DateTime d in t.ThisYear)
206 Console.WriteLine ("\t" + d);
208 Console.WriteLine ("Older:");
209 foreach (DateTime d in t.Older)
210 Console.WriteLine ("\t" + d);
214 #endif