Merge pull request #5744 from OrchardCMS/sebastienros/datetimerelative

Improving DateTimeRelative shape
This commit is contained in:
Sébastien Ros
2015-09-08 15:46:01 -07:00

View File

@@ -1,5 +1,4 @@
using System; using System;
using System.Globalization;
using System.Web; using System.Web;
using System.Web.Mvc; using System.Web.Mvc;
using Orchard.DisplayManagement; using Orchard.DisplayManagement;
@@ -32,8 +31,20 @@ namespace Orchard.Core.Shapes {
DateTimeUtc = DateTimeUtc != System.DateTime.MinValue ? DateTimeUtc : dateTimeUtc; // Both capitalizations retained for compatibility. DateTimeUtc = DateTimeUtc != System.DateTime.MinValue ? DateTimeUtc : dateTimeUtc; // Both capitalizations retained for compatibility.
var time = _clock.UtcNow - DateTimeUtc; var time = _clock.UtcNow - DateTimeUtc;
if (time.TotalDays > 7 || time.TotalDays < -7) if (time.TotalYears() > 1)
return Display.DateTime(DateTimeUtc: DateTimeUtc, CustomFormat: null); 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) if (time.TotalHours > 24)
return T.Plural("1 day ago", "{0} days ago", time.Days); 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)); return new MvcHtmlString(_dateLocalizationServices.ConvertToLocalizedString(DateTimeUtc, CustomFormat.Text));
} }
} }
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;
}
}
} }