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

Commit a559c55

Browse files
Merging functionality into DurationToStringConverter
1 parent 32e28a1 commit a559c55

File tree

5 files changed

+25
-85
lines changed

5 files changed

+25
-85
lines changed

src/GitHub.InlineReviews/Views/CommentView.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
Account="{Binding Author}"/>
5858

5959
<TextBlock Foreground="{DynamicResource GitHubVsToolWindowText}" FontWeight="Bold" Text="{Binding Author.Login}" Margin="4 0"/>
60-
<ui:GitHubActionLink Content="{Binding UpdatedAt, Converter={ui:PositiveDurationToStringConverter}}"
60+
<ui:GitHubActionLink Content="{Binding UpdatedAt, Converter={ui:DurationToStringConverter}}"
6161
Command="{Binding OpenOnGitHub}"
6262
Foreground="{DynamicResource GitHubVsToolWindowText}"
6363
Opacity="0.75" />

src/GitHub.UI/Converters/DurationToStringConverter.cs

Lines changed: 0 additions & 46 deletions
This file was deleted.

src/GitHub.UI/Converters/PositiveDurationToStringConverter.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace GitHub.UI
55
{
6-
public class PositiveDurationToStringConverter : ValueConverterMarkupExtension<PositiveDurationToStringConverter>
6+
public class DurationToStringConverter : ValueConverterMarkupExtension<DurationToStringConverter>
77
{
88
public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
99
{
@@ -22,7 +22,25 @@ public override object Convert(object value, Type targetType, object parameter,
2222
return Resources.JustNow;
2323
}
2424

25-
return DurationToStringConverter.FormatDuration(duration, culture);
25+
const int year = 365;
26+
const int month = 30;
27+
const int day = 24;
28+
const int hour = 60;
29+
const int minute = 60;
30+
31+
if (duration.TotalDays >= year)
32+
return string.Format(culture, (int)(duration.TotalDays / year) > 1 ? Resources.years : Resources.year, (int)(duration.TotalDays / year));
33+
else if (duration.TotalDays >= 360)
34+
return string.Format(culture, Resources.months, 11);
35+
else if (duration.TotalDays >= month)
36+
return string.Format(culture, (int)(duration.TotalDays / (month)) > 1 ? Resources.months : Resources.month, (int)(duration.TotalDays / (month)));
37+
else if (duration.TotalHours >= day)
38+
return string.Format(culture, (int)(duration.TotalHours / day) > 1 ? Resources.days : Resources.day, (int)(duration.TotalHours / day));
39+
else if (duration.TotalMinutes >= hour)
40+
return string.Format(culture, (int)(duration.TotalMinutes / hour) > 1 ? Resources.hours : Resources.hour, (int)(duration.TotalMinutes / hour));
41+
else if (duration.TotalSeconds >= minute)
42+
return string.Format(culture, (int)(duration.TotalSeconds / minute) > 1 ? Resources.minutes : Resources.minute, (int)(duration.TotalSeconds / minute));
43+
return string.Format(culture, duration.TotalSeconds > 1 || duration.Ticks == 0 ? Resources.seconds : Resources.second, duration.TotalSeconds);
2644
}
2745
}
2846
}

src/GitHub.UI/GitHub.UI.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@
120120
<DependentUpon>AutomationIDs.resx</DependentUpon>
121121
</Compile>
122122
<Compile Include="Controls\GitHubProgressBar.cs" />
123-
<Compile Include="Converters\DurationToStringConverter.cs" />
124123
<Compile Include="Converters\HasItemsVisibilityConverter.cs" />
125124
<Compile Include="Properties\AssemblyInfo.cs" />
126125
<Compile Include="..\common\SolutionInfo.cs">

test/GitHub.UI.UnitTests/Converters.cs

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,6 @@
99

1010
public class Converters
1111
{
12-
[TestCase(-1, 0, 0, 0, "-1 second ago")]
13-
[TestCase(0, 0, 0, 0, "0 seconds ago")]
14-
[TestCase(1, 0, 0, 0, "1 second ago")]
15-
[TestCase(2, 0, 0, 0, "2 seconds ago")]
16-
[TestCase(59, 0, 0, 0, "59 seconds ago")]
17-
[TestCase(0, 1, 0, 0, "1 minute ago")]
18-
[TestCase(0, 2, 0, 0, "2 minutes ago")]
19-
[TestCase(0, 59, 0, 0, "59 minutes ago")]
20-
[TestCase(0, 60, 0, 0, "1 hour ago")]
21-
[TestCase(0, 0, 1, 0, "1 hour ago")]
22-
[TestCase(0, 0, 2, 0, "2 hours ago")]
23-
[TestCase(0, 0, 23, 0, "23 hours ago")]
24-
[TestCase(0, 0, 24, 0, "1 day ago")]
25-
[TestCase(0, 0, 0, 1, "1 day ago")]
26-
[TestCase(0, 0, 0, 2, "2 days ago")]
27-
[TestCase(0, 0, 0, 29, "29 days ago")]
28-
[TestCase(0, 0, 0, 30, "1 month ago")]
29-
[TestCase(0, 0, 0, 59, "1 month ago")]
30-
[TestCase(0, 0, 0, 60, "2 months ago")]
31-
[TestCase(0, 0, 0, 364, "11 months ago")]
32-
[TestCase(0, 0, 0, 365, "1 year ago")]
33-
[TestCase(0, 0, 0, 365*2-1, "1 year ago")]
34-
[TestCase(0, 0, 0, 365*2, "2 years ago")]
35-
public void DurationToStringConversion(int sec, int min, int hou, int day, string expected)
36-
{
37-
var ts = new TimeSpan(day, hou, min, sec);
38-
var conv = new DurationToStringConverter();
39-
var ret = (string)conv.Convert(ts, typeof(string), null, CultureInfo.CurrentCulture);
40-
Assert.That(ret, Is.EqualTo(expected));
41-
}
42-
4312
[TestCase(0, 0, -23, 0, "just now")]
4413
[TestCase(-2, 0, 0, 0, "just now")]
4514
[TestCase(-1, 0, 0, 0, "just now")]
@@ -63,12 +32,12 @@ public void DurationToStringConversion(int sec, int min, int hou, int day, strin
6332
[TestCase(0, 0, 0, 60, "2 months ago")]
6433
[TestCase(0, 0, 0, 364, "11 months ago")]
6534
[TestCase(0, 0, 0, 365, "1 year ago")]
66-
[TestCase(0, 0, 0, 365 * 2 - 1, "1 year ago")]
67-
[TestCase(0, 0, 0, 365 * 2, "2 years ago")]
68-
public void PositiveDurationToStringConversion(int sec, int min, int hou, int day, string expected)
35+
[TestCase(0, 0, 0, 365*2-1, "1 year ago")]
36+
[TestCase(0, 0, 0, 365*2, "2 years ago")]
37+
public void DurationToStringConversion(int sec, int min, int hou, int day, string expected)
6938
{
7039
var ts = new TimeSpan(day, hou, min, sec);
71-
var conv = new PositiveDurationToStringConverter();
40+
var conv = new DurationToStringConverter();
7241
var ret = (string)conv.Convert(ts, typeof(string), null, CultureInfo.CurrentCulture);
7342
Assert.That(ret, Is.EqualTo(expected));
7443
}

0 commit comments

Comments
 (0)