Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 698f7b6

Browse files
authored
Merge pull request #1852 from github/fixes/create-comment-just-now
Treat negative durations in comments as having just occurred
2 parents 6c121c1 + d630243 commit 698f7b6

File tree

4 files changed

+35
-14
lines changed

4 files changed

+35
-14
lines changed

src/GitHub.UI/Converters/DurationToStringConverter.cs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,27 @@ public class DurationToStringConverter : ValueConverterMarkupExtension<DurationT
77
{
88
public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
99
{
10+
TimeSpan duration;
11+
if (value is TimeSpan span)
12+
duration = span;
13+
else if (value is DateTime time)
14+
duration = DateTime.UtcNow - time;
15+
else if (value is DateTimeOffset offset)
16+
duration = DateTimeOffset.UtcNow - offset;
17+
else
18+
return value;
19+
20+
if (duration.Ticks <= 0)
21+
{
22+
return Resources.JustNow;
23+
}
24+
1025
const int year = 365;
1126
const int month = 30;
1227
const int day = 24;
1328
const int hour = 60;
1429
const int minute = 60;
1530

16-
TimeSpan duration;
17-
if (value is TimeSpan)
18-
duration = (TimeSpan)value;
19-
else if (value is DateTime)
20-
duration = DateTime.UtcNow - (DateTime)value;
21-
else if (value is DateTimeOffset)
22-
duration = DateTimeOffset.UtcNow - (DateTimeOffset)value;
23-
else
24-
return value;
25-
2631
if (duration.TotalDays >= year)
2732
return string.Format(culture, (int)(duration.TotalDays / year) > 1 ? Resources.years : Resources.year, (int)(duration.TotalDays / year));
2833
else if (duration.TotalDays >= 360)
@@ -35,7 +40,7 @@ public override object Convert(object value, Type targetType, object parameter,
3540
return string.Format(culture, (int)(duration.TotalMinutes / hour) > 1 ? Resources.hours : Resources.hour, (int)(duration.TotalMinutes / hour));
3641
else if (duration.TotalSeconds >= minute)
3742
return string.Format(culture, (int)(duration.TotalSeconds / minute) > 1 ? Resources.minutes : Resources.minute, (int)(duration.TotalSeconds / minute));
38-
return string.Format(culture, duration.TotalSeconds > 1 ? Resources.seconds : Resources.second, duration.TotalSeconds);
43+
return string.Format(culture, duration.TotalSeconds > 1 || duration.Ticks == 0 ? Resources.seconds : Resources.second, duration.TotalSeconds);
3944
}
4045
}
41-
}
46+
}

src/GitHub.UI/Resources.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/GitHub.UI/Resources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@
129129
<data name="hours" xml:space="preserve">
130130
<value>{0:N0} hours ago</value>
131131
</data>
132+
<data name="JustNow" xml:space="preserve">
133+
<value>just now</value>
134+
</data>
132135
<data name="minute" xml:space="preserve">
133136
<value>{0:N0} minute ago</value>
134137
</data>

test/GitHub.UI.UnitTests/Converters.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99

1010
public class Converters
1111
{
12+
[TestCase(0, 0, -23, 0, "just now")]
13+
[TestCase(-2, 0, 0, 0, "just now")]
14+
[TestCase(-1, 0, 0, 0, "just now")]
15+
[TestCase(0, 0, 0, 0, "just now")]
1216
[TestCase(1, 0, 0, 0, "1 second ago")]
1317
[TestCase(2, 0, 0, 0, "2 seconds ago")]
1418
[TestCase(59, 0, 0, 0, "59 seconds ago")]
@@ -30,11 +34,11 @@ public class Converters
3034
[TestCase(0, 0, 0, 365, "1 year ago")]
3135
[TestCase(0, 0, 0, 365*2-1, "1 year ago")]
3236
[TestCase(0, 0, 0, 365*2, "2 years ago")]
33-
public void TimespanConversion(int sec, int min, int hou, int day, string expected)
37+
public void DurationToStringConversion(int sec, int min, int hou, int day, string expected)
3438
{
3539
var ts = new TimeSpan(day, hou, min, sec);
3640
var conv = new DurationToStringConverter();
3741
var ret = (string)conv.Convert(ts, typeof(string), null, CultureInfo.CurrentCulture);
38-
Assert.That(expected, Is.EqualTo(ret));
42+
Assert.That(ret, Is.EqualTo(expected));
3943
}
4044
}

0 commit comments

Comments
 (0)