2006-04-25 Hendrik Brandt <heb@gnome-de.org>
[beagle.git] / search / Tiles / Utils.cs
blob94f5b91b889198cabbd989f335984360ed7d5194
1 using System;
2 using System.Globalization;
3 using Mono.Unix;
5 namespace Search.Tiles {
7 public struct TileGroupInfo {
8 public TileGroup Group;
9 public string Name;
10 public int Rows;
12 public TileGroupInfo (TileGroup group, string name, int rows)
14 Group = group;
15 Name = name;
16 Rows = rows;
20 public static class Utils {
22 public static TileGroupInfo[] GroupInfo = new TileGroupInfo[] {
23 new TileGroupInfo (TileGroup.Application,
24 Catalog.GetString ("Application"), 1),
25 new TileGroupInfo (TileGroup.Contact,
26 Catalog.GetString ("Contact"), 2),
27 new TileGroupInfo (TileGroup.Folder,
28 Catalog.GetString ("Folder"), 2),
29 new TileGroupInfo (TileGroup.Image,
30 Catalog.GetString ("Image"), 2),
31 new TileGroupInfo (TileGroup.Audio,
32 Catalog.GetString ("Audio"), 2),
33 new TileGroupInfo (TileGroup.Video,
34 Catalog.GetString ("Video"), 2),
35 new TileGroupInfo (TileGroup.Documents,
36 Catalog.GetString ("Documents"), 2),
37 new TileGroupInfo (TileGroup.Conversations,
38 Catalog.GetString ("Conversations"), 5),
39 new TileGroupInfo (TileGroup.Website,
40 Catalog.GetString ("Website"), 2),
41 new TileGroupInfo (TileGroup.Feed,
42 Catalog.GetString ("News Feed"), 2),
43 new TileGroupInfo (TileGroup.Archive,
44 Catalog.GetString ("Archive"), 2),
47 public static DateTime ParseTimestamp (string timestamp)
49 DateTime dt;
50 dt = new DateTime (Int32.Parse (timestamp.Substring (0, 4)),
51 Int32.Parse (timestamp.Substring (4, 2)),
52 Int32.Parse (timestamp.Substring (6, 2)),
53 Int32.Parse (timestamp.Substring (8, 2)),
54 Int32.Parse (timestamp.Substring (10, 2)),
55 Int32.Parse (timestamp.Substring (12, 2)));
56 return dt;
59 private static DateTimeFormatInfo DateTimeFormat = CultureInfo.CurrentCulture.DateTimeFormat;
60 private static string ShortMonthDayPattern = DateTimeFormat.MonthDayPattern.Replace ("MMMM", "MMM");
61 private static string ShortYearMonthPattern = DateTimeFormat.YearMonthPattern.Replace ("MMMM", "MMM");
62 private static string MonthDayPattern = DateTimeFormat.MonthDayPattern;
63 private static string LongDatePattern = DateTimeFormat.LongDatePattern.Replace ("dddd, ", "").Replace ("dddd ", "").Replace (" dddd", "");
65 public static string NiceShortDate (string timestamp)
67 DateTime dt;
69 try {
70 dt = ParseTimestamp (timestamp);
71 } catch {
72 return "";
75 return NiceShortDate (dt);
78 public static string NiceShortDate (DateTime dt)
80 if (dt.Year <= 1970)
81 return "-";
83 dt = dt.ToLocalTime ();
84 DateTime today = DateTime.Today;
85 TimeSpan span = today - dt;
87 if (span.TotalDays < 1)
88 return Catalog.GetString ("Today");
89 else if (span.TotalDays < 2)
90 return Catalog.GetString ("Yesterday");
91 else if (span.TotalDays < 7)
92 return dt.ToString ("dddd"); // "Tuesday"
93 else if (dt.Year == today.Year || span.TotalDays < 180)
94 return dt.ToString (ShortMonthDayPattern); // "Jul 4"
95 else
96 return dt.ToString (ShortYearMonthPattern); // "Jan 2001"
99 public static string NiceLongDate (string timestamp)
101 DateTime dt;
103 try {
104 dt = ParseTimestamp (timestamp);
105 } catch {
106 return "";
109 return NiceLongDate (dt);
112 public static string NiceLongDate (DateTime dt)
114 if (dt.Year <= 1970)
115 return "-";
117 dt = dt.ToLocalTime ();
118 DateTime today = DateTime.Today;
119 TimeSpan span = today - dt;
121 if (span.TotalDays < 1)
122 return Catalog.GetString ("Today");
123 else if (span.TotalDays < 2)
124 return Catalog.GetString ("Yesterday");
125 else if (span.TotalDays < 7)
126 return dt.ToString ("dddd"); // "Tuesday"
127 else if (dt.Year == today.Year || span.TotalDays < 180)
128 return dt.ToString (MonthDayPattern); // "July 4"
129 else
130 return dt.ToString (LongDatePattern); // January 7, 2001
133 public static string NiceVeryLongDate (string timestamp)
135 DateTime dt;
137 try {
138 dt = ParseTimestamp (timestamp);
139 } catch {
140 return "";
143 return NiceVeryLongDate (dt);
146 public static string NiceVeryLongDate (DateTime dt)
148 if (dt.Year <= 1970)
149 return "-";
151 dt = dt.ToLocalTime ();
152 DateTime today = DateTime.Today;
153 TimeSpan span = today - dt;
155 if (span.TotalDays < 1)
156 return Catalog.GetString ("Today");
157 else if (span.TotalDays < 2)
158 return Catalog.GetString ("Yesterday");
159 else if (span.TotalDays < 7)
160 return dt.ToString ("dddd"); // "Tuesday"
161 else if (span.TotalDays < 30)
162 return String.Format (Catalog.GetPluralString ("{0} week ago", "{0} weeks ago", span.Days / 7) + " ({1:MMMM d, yyyy})", span.Days / 7, dt);
163 else if (span.TotalDays < 365 + 180) // Let's say a year and a half to stop saying months
164 return String.Format (Catalog.GetPluralString ("{0} month ago", "{0} months ago", span.Days / 30) + " ({1:MMMM d, yyyy})", span.Days / 30, dt);
165 else
166 return String.Format (Catalog.GetPluralString ("{0} year ago", "{0} years ago", span.Days / 365) + " ({1:MMMM d, yyyy})", span.Days / 365, dt);