1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
20 /// Simple helper for date formatting
22 public class DateFormatHelper
: AbstractHelper
26 /// Initializes a new instance of the <see cref="DateFormatHelper"/> class.
28 public DateFormatHelper() { }
30 /// Initializes a new instance of the <see cref="DateFormatHelper"/> class.
31 /// setting the Controller, Context and ControllerContext.
33 /// <param name="engineContext">The engine context.</param>
34 public DateFormatHelper(IEngineContext engineContext
) : base(engineContext
) { }
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>
45 /// TODO: Think about i18n
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)
61 else if (diff
.TotalHours
<= 48)
65 else if (diff
.TotalDays
<= 40)
67 return String
.Format("{0} days ago", diff
.Days
);
71 return String
.Format("{0} months ago", (diff
.Days
/ 30));
76 /// Returns the difference from the
77 /// specified <c>date</c> the the current date
78 /// in a friendly string like "1 day ago"
80 /// TODO: Think about i18n
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)
101 if (diff
.Minutes
== 0)
103 return String
.Format("{0} second{1} ago",
104 diff
.Seconds
, diff
.Seconds
> 1 ? "s" : String
.Empty
);
108 return String
.Format("{0} minute{1} ago",
109 diff
.Minutes
, diff
.Minutes
> 1 ? "s" : String
.Empty
);
114 return String
.Format("{0} hour{1} ago",
115 diff
.Hours
, diff
.Hours
> 1 ? "s" : String
.Empty
);
120 return String
.Format("{0} day{1} ago",
121 diff
.Days
, diff
.Days
> 1 ? "s" : String
.Empty
);
126 /// Formats to short date
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
;
136 /// Formats to short date
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
;