Fixing an issue with output parameters that are of type IntPtr
[castle.git] / MonoRail / Castle.MonoRail.Framework / Helpers / DateFormatHelper.cs
blob54e21473d469b2c80c1bab121cd1e32c88a48627
1 // Copyright 2004-2008 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 #region Constructors
25 /// <summary>
26 /// Initializes a new instance of the <see cref="DateFormatHelper"/> class.
27 /// </summary>
28 public DateFormatHelper() { }
29 /// <summary>
30 /// Initializes a new instance of the <see cref="DateFormatHelper"/> class.
31 /// setting the Controller, Context and ControllerContext.
32 /// </summary>
33 /// <param name="engineContext">The engine context.</param>
34 public DateFormatHelper(IEngineContext engineContext) : base(engineContext) { }
35 #endregion
37 /// <summary>
38 /// Alternative representation of a difference
39 /// between the specified date and now. If within 24hr
40 /// it returns <c>Today</c>. If within 48hr it returns
41 /// <c>Yesterday</c>. If within 40 days it returns
42 /// <c>x days ago</c> and otherwise it returns
43 /// <c>x months ago</c>
44 /// <para>
45 /// TODO: Think about i18n
46 /// </para>
47 /// </summary>
48 /// <param name="date">The date in the past (should be equal or less than now)</param>
49 /// <returns></returns>
50 public String AlternativeFriendlyFormatFromNow(DateTime date)
52 TimeSpan now = new TimeSpan(DateTime.Now.Ticks);
53 TimeSpan cur = new TimeSpan(date.Ticks);
55 TimeSpan diff = now.Subtract(cur);
57 if (diff.TotalHours <= 24)
59 return "Today";
61 else if (diff.TotalHours <= 48)
63 return "Yesterday";
65 else if (diff.TotalDays <= 40)
67 return String.Format("{0} days ago", diff.Days);
69 else
71 return String.Format("{0} months ago", (diff.Days / 30));
75 /// <summary>
76 /// Returns the difference from the
77 /// specified <c>date</c> the the current date
78 /// in a friendly string like "1 day ago"
79 /// <para>
80 /// TODO: Think about i18n
81 /// </para>
82 /// </summary>
83 /// <param name="date">The date in the past (should be equal or less than now)</param>
84 /// <returns></returns>
85 public String FriendlyFormatFromNow(DateTime date)
87 TimeSpan now = new TimeSpan(DateTime.Now.Ticks);
88 TimeSpan cur = new TimeSpan(date.Ticks);
90 TimeSpan diff = now.Subtract(cur);
92 if (diff.TotalSeconds == 0)
94 return "Just now";
97 if (diff.Days == 0)
99 if (diff.Hours == 0)
101 if (diff.Minutes == 0)
103 return String.Format("{0} second{1} ago",
104 diff.Seconds, diff.Seconds > 1 ? "s" : String.Empty);
106 else
108 return String.Format("{0} minute{1} ago",
109 diff.Minutes, diff.Minutes > 1 ? "s" : String.Empty);
112 else
114 return String.Format("{0} hour{1} ago",
115 diff.Hours, diff.Hours > 1 ? "s" : String.Empty);
118 else
120 return String.Format("{0} day{1} ago",
121 diff.Days, diff.Days > 1 ? "s" : String.Empty);
125 /// <summary>
126 /// Formats to short date
127 /// </summary>
128 /// <param name="date"></param>
129 /// <returns>Short date, or <c>String.Empty</c> if <paramref name="date"/> is <c>null</c>.</returns>
130 public String ToShortDate(DateTime? date)
132 return date.HasValue ? date.Value.ToShortDateString() : string.Empty;
135 /// <summary>
136 /// Formats to short date
137 /// </summary>
138 /// <param name="date"></param>
139 /// <returns>Short date and time, or <c>String.Empty</c> if <paramref name="date"/> is <c>null</c>.</returns>
140 public String ToShortDateTime(DateTime? date)
142 return date.HasValue ? date.Value.ToShortDateString() + " " + date.Value.ToShortTimeString() : string.Empty;