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

Commit 24c16e5

Browse files
committed
Merge pull request #217 from github/feature/pr/duration-converter
A handy date to duration converter
2 parents 99c8e70 + a7dc246 commit 24c16e5

File tree

6 files changed

+421
-0
lines changed

6 files changed

+421
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
using System.Globalization;
3+
4+
namespace GitHub.UI
5+
{
6+
public class DurationToStringConverter : ValueConverterMarkupExtension<DurationToStringConverter>
7+
{
8+
public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
9+
{
10+
const int year = 365;
11+
const int month = 30;
12+
const int day = 24;
13+
const int hour = 60;
14+
const int minute = 60;
15+
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+
26+
if (duration.TotalDays >= year)
27+
return string.Format(culture, (int)(duration.TotalDays / year) > 1 ? Resources.years : Resources.year, (int)(duration.TotalDays / year));
28+
else if (duration.TotalDays >= 360)
29+
return string.Format(culture, Resources.months, 11);
30+
else if (duration.TotalDays >= month)
31+
return string.Format(culture, (int)(duration.TotalDays / (month)) > 1 ? Resources.months : Resources.month, (int)(duration.TotalDays / (month)));
32+
else if (duration.TotalHours >= day)
33+
return string.Format(culture, (int)(duration.TotalHours / day) > 1 ? Resources.days : Resources.day, (int)(duration.TotalHours / day));
34+
else if (duration.TotalMinutes >= hour)
35+
return string.Format(culture, (int)(duration.TotalMinutes / hour) > 1 ? Resources.hours : Resources.hour, (int)(duration.TotalMinutes / hour));
36+
else if (duration.TotalSeconds >= minute)
37+
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);
39+
}
40+
}
41+
}

src/GitHub.UI/GitHub.UI.csproj

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,16 @@
6969
<DesignTime>True</DesignTime>
7070
<DependentUpon>OcticonPaths.resx</DependentUpon>
7171
</Compile>
72+
<Compile Include="Resources.Designer.cs">
73+
<AutoGen>True</AutoGen>
74+
<DesignTime>True</DesignTime>
75+
<DependentUpon>Resources.resx</DependentUpon>
76+
</Compile>
7277
<None Include="..\..\script\Key.snk" Condition="$(Buildtype) == 'Internal'">
7378
<Link>Key.snk</Link>
7479
</None>
7580
<Compile Include="Controls\GitHubProgressBar.cs" />
81+
<Compile Include="Converters\DurationToStringConverter.cs" />
7682
<Compile Include="Converters\HasItemsVisibilityConverter.cs" />
7783
<Compile Include="Properties\AssemblyInfo.cs" />
7884
<Compile Include="..\common\SolutionInfo.cs">
@@ -194,6 +200,11 @@
194200
<Generator>ResXFileCodeGenerator</Generator>
195201
<LastGenOutput>OcticonPaths.Designer.cs</LastGenOutput>
196202
</EmbeddedResource>
203+
<EmbeddedResource Include="Resources.resx">
204+
<Generator>ResXFileCodeGenerator</Generator>
205+
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
206+
<SubType>Designer</SubType>
207+
</EmbeddedResource>
197208
</ItemGroup>
198209
<ItemGroup>
199210
<ProjectReference Include="..\GitHub.Exports\GitHub.Exports.csproj">

src/GitHub.UI/Resources.Designer.cs

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

0 commit comments

Comments
 (0)