Time formatting improvements

This commit is contained in:
2017-02-06 12:17:46 -05:00
parent b4440ecd95
commit 7dcd9dec75
6 changed files with 81 additions and 15 deletions
+1
View File
@@ -59,6 +59,7 @@
<Compile Include="Parsers\LeafInputCsvParserTests.cs" />
<Compile Include="Remote\PiscalSshClientTests.cs" />
<Compile Include="Utility\MemoizerTests.cs" />
<Compile Include="Utility\TimeSpanExtensionsTests.cs" />
<Compile Include="Utility\StringExtensionsTests.cs" />
<Compile Include="Utility\TimeInProgressEstimaterTests.cs" />
</ItemGroup>
@@ -0,0 +1,42 @@
using System;
using LeafWeb.Core.Utility;
using NUnit.Framework;
namespace LeafWeb.Core.Tests.Utility
{
[TestFixture]
public class TimeSpanExtensionsTests
{
[Test]
public void ToRoundedReadableString_1Sec()
{
var ts = TimeSpan.FromSeconds(1);
var result = ts.ToRoundedReadableString(abbreviation:true);
Assert.That(result, Is.EqualTo("1 sec."));
}
[Test]
public void ToRoundedReadableString_2Sec()
{
var ts = TimeSpan.FromSeconds(2);
var result = ts.ToRoundedReadableString(abbreviation:true);
Assert.That(result, Is.EqualTo("2 secs."));
}
[Test]
public void ToRoundedReadableString_1Hr()
{
var ts = TimeSpan.FromHours(1);
var result = ts.ToRoundedReadableString(abbreviation:true);
Assert.That(result, Is.EqualTo("1 hr."));
}
[Test]
public void ToRoundedReadableString_2Hr()
{
var ts = TimeSpan.FromHours(2);
var result = ts.ToRoundedReadableString(abbreviation:true);
Assert.That(result, Is.EqualTo("2 hrs."));
}
}
}
+9 -2
View File
@@ -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)
+25 -9
View File
@@ -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";
}
+1 -1
View File
@@ -1 +1 @@
C:\Users\poprhythm\AppData\Local\Temp\Temporary ASP.NET Files\vs\f80e29bb\faae20bf\App_Web_all.generated.cs.8f9494c4.ivc2jw58.dll
C:\Users\poprhythm\AppData\Local\Temp\Temporary ASP.NET Files\vs\f80e29bb\faae20bf\App_Web_all.generated.cs.8f9494c4.idigbf5n.dll
+3 -3
View File
@@ -30,7 +30,7 @@
grid.Column("Identifier", "Identifier"),
grid.Column("SiteId", "Site Id"),
grid.Column("Name", "Submitted By"),
grid.Column("TimeInProgress", "Time In Progress", item => TimeInProgress(item.Value)),
grid.Column("TimeInProgress", "Statistics", item => TimeInProgress(item.Value)),
grid.Column("CurrentStatus", "Status", item => Status(item.Value)),
grid.Column("Total Results: " + Model.Items.Count(), format: item => Btns(item.Value))),
htmlAttributes: new {@class = "table table-striped table-bordered table-hover table-condensed"}
@@ -129,6 +129,8 @@ if (leafInput.EndTime.HasValue)
}
var summaryText = string.Join(Environment.NewLine, summary);
<span class="text-nowrap" title="@summaryText">
<i class="fa fa-file-o"></i> @leafInput.InputFiles.Count input @if(leafInput.InputFiles.Count > 1) {<text>files</text>} else { <text>file</text>}
<br />
@if (leafInput.TimeInProgress > TimeSpan.Zero)
{
<text>
@@ -148,8 +150,6 @@ var summaryText = string.Join(Environment.NewLine, summary);
<i class="fa fa-hourglass-half"></i> @Html.Partial("DisplayTemplates/_TimeRemaining", Tuple.Create(leafInput, Model.CompletedLeafInput)) left
</text>
}
<br />
<i class="fa fa-file-o"></i> @leafInput.InputFiles.Count input files
</span>
}
@helper DeleteLink(LeafInput item)