Minor style changes
[castle.git] / MonoRail / Castle.MonoRail.Framework / Helpers / DateFormatHelper.cs
blob98c034467d365c3c4f38d5aeacb719361a8f555a
1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.MonoRail.Framework.Helpers
17 using System;
19 /// <summary>
20 /// Simple helper for date formatting
21 /// </summary>
22 public class DateFormatHelper : AbstractHelper
24 /// <summary>
25 /// Alternative representation of a difference
26 /// between the specified date and now. If within 24hr
27 /// it returns <c>Today</c>. If within 48hr it returns
28 /// <c>Yesterday</c>. If within 40 days it returns
29 /// <c>x days ago</c> and otherwise it returns
30 /// <c>x months ago</c>
31 /// <para>
32 /// TODO: Think about i18n
33 /// </para>
34 /// </summary>
35 /// <param name="date">The date in the past (should be equal or less than now)</param>
36 /// <returns></returns>
37 public String AlternativeFriendlyFormatFromNow(DateTime date)
39 TimeSpan now = new TimeSpan(DateTime.Now.Ticks);
40 TimeSpan cur = new TimeSpan(date.Ticks);
42 TimeSpan diff = now.Subtract(cur);
44 if (diff.TotalHours <= 24)
46 return "Today";
48 else if (diff.TotalHours <= 48)
50 return "Yesterday";
52 else if (diff.TotalDays <= 40)
54 return String.Format("{0} days ago", diff.Days);
56 else
58 return String.Format("{0} months ago", (diff.Days / 30));
62 /// <summary>
63 /// Returns the difference from the
64 /// specified <c>date</c> the the current date
65 /// in a friendly string like "1 day ago"
66 /// <para>
67 /// TODO: Think about i18n
68 /// </para>
69 /// </summary>
70 /// <param name="date">The date in the past (should be equal or less than now)</param>
71 /// <returns></returns>
72 public String FriendlyFormatFromNow(DateTime date)
74 TimeSpan now = new TimeSpan(DateTime.Now.Ticks);
75 TimeSpan cur = new TimeSpan(date.Ticks);
77 TimeSpan diff = now.Subtract(cur);
79 if (diff.TotalSeconds == 0)
81 return "Just now";
84 if (diff.Days == 0)
86 if (diff.Hours == 0)
88 if (diff.Minutes == 0)
90 return String.Format("{0} second{1} ago",
91 diff.Seconds, diff.Seconds > 1 ? "s" : String.Empty);
93 else
95 return String.Format("{0} minute{1} ago",
96 diff.Minutes, diff.Minutes > 1 ? "s" : String.Empty);
99 else
101 return String.Format("{0} hour{1} ago",
102 diff.Hours, diff.Hours > 1 ? "s" : String.Empty);
105 else
107 return String.Format("{0} day{1} ago",
108 diff.Days, diff.Days > 1 ? "s" : String.Empty);
112 /// <summary>
113 /// Formats to short date
114 /// </summary>
115 /// <param name="date"></param>
116 /// <returns>Short date, or <c>String.Empty</c> if <paramref name="date"/> is <c>null</c>.</returns>
117 public String ToShortDate(DateTime? date)
119 return date.HasValue ? date.Value.ToShortDateString() : string.Empty;
122 /// <summary>
123 /// Formats to short date
124 /// </summary>
125 /// <param name="date"></param>
126 /// <returns>Short date and time, or <c>String.Empty</c> if <paramref name="date"/> is <c>null</c>.</returns>
127 public String ToShortDateTime(DateTime? date)
129 return date.HasValue ? date.Value.ToShortDateString() + " " + date.Value.ToShortTimeString() : string.Empty;