Time formatting improvements
This commit is contained in:
@@ -69,8 +69,15 @@ namespace LeafWeb.Core.DAL
|
||||
li.CurrentStatus == LeafInputStatusType.Pending
|
||||
? (int)li.PendingPriority
|
||||
: int.MinValue)
|
||||
// then the rest by the order they're added in
|
||||
.ThenByDescending(li => li.Id);
|
||||
// then by pending, rest by the order they're added in, ascending
|
||||
.ThenBy(li =>
|
||||
li.CurrentStatus == LeafInputStatusType.Pending
|
||||
? li.Id
|
||||
: int.MaxValue)
|
||||
// then the rest descending by the order last status change
|
||||
.ThenByDescending(li =>
|
||||
(from s in li.StatusHistory
|
||||
select s).Max(s => s.DateTime));
|
||||
}
|
||||
|
||||
public LeafInput GetLeafInput(int id)
|
||||
|
||||
@@ -1,13 +1,26 @@
|
||||
using System;
|
||||
using System.Runtime.Remoting.Messaging;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace LeafWeb.Core.Utility
|
||||
{
|
||||
public static class TimeSpanExtensions
|
||||
{
|
||||
private const string Month = "month";
|
||||
private const string MonthAbr = "mo.";
|
||||
private const string Day = "day";
|
||||
private const string DayAbr = "day";
|
||||
private const string Hour = "hour";
|
||||
private const string HourAbr = "hr.";
|
||||
private const string Minute = "minute";
|
||||
private const string MinuteAbr = "min.";
|
||||
private const string Second = "second";
|
||||
private const string SecondAbr = "sec.";
|
||||
private static Regex PluralizeRegex = new Regex(@"^([^\.]*)(\.?)$", RegexOptions.Compiled);
|
||||
|
||||
public static string ToReadableString(this TimeSpan span)
|
||||
{
|
||||
Func<int, string> pluralize = i => i > 1 ? "s" : string.Empty;
|
||||
// ReSharper disable once UseStringInterpolation
|
||||
var formatted = string.Format("{0}{1}{2}{3}",
|
||||
span.Duration().Days > 0 ? $"{span.Days:0} day{pluralize(span.Days)}, " : string.Empty,
|
||||
span.Duration().Hours > 0 ? $"{span.Hours:0} hour{pluralize(span.Hours)}, " : string.Empty,
|
||||
@@ -21,20 +34,23 @@ namespace LeafWeb.Core.Utility
|
||||
return formatted;
|
||||
}
|
||||
|
||||
public static string ToRoundedReadableString(this TimeSpan span)
|
||||
public static string ToRoundedReadableString(this TimeSpan span, bool abbreviation = true)
|
||||
{
|
||||
Func<int, string> pluralize = i => i > 1 ? "s" : string.Empty;
|
||||
Func<int, string, string> formatTime = (i, s) => $"{i:0} {s}{pluralize(i)}";
|
||||
Func<int, string, string> pluralize =
|
||||
(i, s) => i > 1 ? PluralizeRegex.Replace(s, "$1s$2") : s;
|
||||
|
||||
Func<int, string, string> formatTime = (i, s) => $"{i:0} {pluralize(i, s)}";
|
||||
|
||||
if (span.Duration().Days > 90)
|
||||
return formatTime(span.Days/30, "month");
|
||||
return formatTime(span.Days/30, abbreviation ? MonthAbr : Month);
|
||||
if (span.Duration().Days > 0)
|
||||
return formatTime(span.Days, "day");
|
||||
return formatTime(span.Days, abbreviation ? DayAbr : Day);
|
||||
if (span.Duration().Hours > 0)
|
||||
return formatTime(span.Hours, "hour");
|
||||
return formatTime(span.Hours, abbreviation ? HourAbr : Hour);
|
||||
if (span.Duration().Minutes > 0)
|
||||
return formatTime(span.Minutes, "minute");
|
||||
return formatTime(span.Minutes, abbreviation ? MinuteAbr : Minute);
|
||||
if (span.Duration().Seconds > 0)
|
||||
return formatTime(span.Seconds, "second");
|
||||
return formatTime(span.Seconds, abbreviation ? SecondAbr : Second);
|
||||
|
||||
return "0 seconds";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user