From 4831b72bd174904f8854826774eb2ed64a9e5c1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Ros?= Date: Wed, 26 Aug 2015 15:18:40 -0700 Subject: [PATCH] Improving DateTimeRelative shape The implementation was not relative for span further than a week. --- src/Orchard.Web/Core/Shapes/DateTimeShapes.cs | 35 ++++++++++++++++--- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/src/Orchard.Web/Core/Shapes/DateTimeShapes.cs b/src/Orchard.Web/Core/Shapes/DateTimeShapes.cs index f63f4bedc..12099ede3 100644 --- a/src/Orchard.Web/Core/Shapes/DateTimeShapes.cs +++ b/src/Orchard.Web/Core/Shapes/DateTimeShapes.cs @@ -1,5 +1,4 @@ -using System; -using System.Globalization; +using System; using System.Web; using System.Web.Mvc; using Orchard.DisplayManagement; @@ -32,8 +31,20 @@ namespace Orchard.Core.Shapes { DateTimeUtc = DateTimeUtc != System.DateTime.MinValue ? DateTimeUtc : dateTimeUtc; // Both capitalizations retained for compatibility. var time = _clock.UtcNow - DateTimeUtc; - if (time.TotalDays > 7 || time.TotalDays < -7) - return Display.DateTime(DateTimeUtc: DateTimeUtc, CustomFormat: null); + if (time.TotalYears() > 1) + return T.Plural("1 year ago", "{0} years ago", time.TotalYears()); + if (time.TotalYears() < -1) + return T.Plural("in 1 year", "in {0} years", -time.TotalYears()); + + if (time.TotalMonths() > 1) + return T.Plural("1 month ago", "{0} months ago", time.TotalMonths()); + if (time.TotalMonths() < -1) + return T.Plural("in 1 month", "in {0} months", -time.TotalMonths()); + + if (time.TotalWeeks() > 1) + return T.Plural("1 week ago", "{0} weeks ago", time.TotalWeeks()); + if (time.TotalWeeks() < -1) + return T.Plural("in 1 week", "in {0} weeks", -time.TotalWeeks()); if (time.TotalHours > 24) return T.Plural("1 day ago", "{0} days ago", time.Days); @@ -71,4 +82,18 @@ namespace Orchard.Core.Shapes { return new MvcHtmlString(_dateLocalizationServices.ConvertToLocalizedString(DateTimeUtc, CustomFormat.Text)); } } -} \ No newline at end of file + + public static class TimespanExtensions { + public static int TotalWeeks(this TimeSpan time) { + return (int)time.TotalDays / 7; + } + + public static int TotalMonths(this TimeSpan time) { + return (int)time.TotalDays / 31; + } + + public static int TotalYears(this TimeSpan time) { + return (int)time.TotalDays / 365; + } + } +}