Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detailed time span #65

Merged
merged 6 commits into from
Jan 23, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,22 @@ You can call `Humanizes` on a `TimeSpan` to a get human friendly representation
TimeSpan.FromMilliseconds(1).Humanize() => "1 millisecond"
TimeSpan.FromMilliseconds(2).Humanize() => "2 milliseconds"
TimeSpan.FromDays(1).Humanize() => "1 day"
TimeSpan.FromDays(14).Humanize() => "2 weeks"
TimeSpan.FromDays(16).Humanize() => "2 weeks"
```

There is an optional `precision` parameter for `TimeSpan.Humanize` which allows you to specify the precision of the returned value.
The default value of `precision` is 1 which means only the largest time unit is returned like you saw in `TimeSpan.FromDays(16).Humanize()`.
Here is a few examples of specifying precision:

```C#
TimeSpan.FromDays(1).Humanize(precision:2) => "1 day" // no difference when there is only on unit in the provided TimeSpan
TimeSpan.FromDays(16).Humanize(2) => "2 weeks, 2 days"

// the same TimeSpan value with different precision returns different results
TimeSpan.FromMilliseconds(1299630020).Humanize() => "2 weeks"
TimeSpan.FromMilliseconds(1299630020).Humanize(3) => "2 weeks, 1 day, 1 hour"
TimeSpan.FromMilliseconds(1299630020).Humanize(4) => "2 weeks, 1 day, 1 hour, 30 seconds"
TimeSpan.FromMilliseconds(1299630020).Humanize(5) => "2 weeks, 1 day, 1 hour, 30 seconds, 20 milliseconds"
```

###Inflector methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ public class InflectorExtensions
{
public string Camelize(string input) { }
public string Dasherize(string underscoredWord) { }
public string Hyphenate(string underscoredWord) { }
public string Pascalize(string input) { }
public string Pluralize(string word) { }
public string Singularize(string word) { }
public string Pluralize(string word, Humanizer.Plurality plurality) { }
public string Singularize(string word, Humanizer.Plurality plurality) { }
public string Titleize(string input) { }
public string Underscore(string input) { }
}
Expand Down Expand Up @@ -183,6 +184,14 @@ public class OrdinalizeExtensions
public string Ordinalize(int number) { }
}

public enum Plurality
{
CouldBeEither,
Plural,
Singular,
value__,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weird, what is __value?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw that too. It looks like a bug in ApiApprover for enums. I will check out ApiApprover to see if I can find and fix it.

}

public class PrepositionsExtensions
{
public System.DateTime At(System.DateTime date, int hour, int min, int second, int millisecond) { }
Expand All @@ -191,6 +200,14 @@ public class PrepositionsExtensions
public System.DateTime In(System.DateTime date, int year) { }
}

public enum ShowQuantityAs
{
None,
Numeric,
value__,
Words,
}

public class StringDehumanizeExtensions
{
public string Dehumanize(string input) { }
Expand All @@ -204,7 +221,7 @@ public class StringHumanizeExtensions

public class TimeSpanHumanizeExtensions
{
public string Humanize(System.TimeSpan timeSpan) { }
public string Humanize(System.TimeSpan timeSpan, int precision) { }
}

public class To
Expand All @@ -214,4 +231,9 @@ public class To
public Humanizer.IStringTransformer TitleCase { get; }
public Humanizer.IStringTransformer UpperCase { get; }
public string Transform(string input, Humanizer.IStringTransformer[] transformers) { }
}

public class ToQuantityExtensions
{
public string ToQuantity(string input, int quantity, Humanizer.ShowQuantityAs showQuantityAs) { }
}
11 changes: 7 additions & 4 deletions src/Humanizer.Tests/Humanizer.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@
<ItemGroup>
<Compile Include="CasingTests.cs" />
<Compile Include="Localisation\ar\DateHumanizeTests.cs" />
<Compile Include="Localisation\ar\TimeSpanTests.cs" />
<Compile Include="Localisation\ar\TimeSpanHumanizeTests.cs" />
<Compile Include="Localisation\DateHumanizeTests.nb-NO.cs" />
<Compile Include="Localisation\es\DateHumanizeTests.cs" />
<Compile Include="Localisation\es\TimeSpanTests.cs" />
<Compile Include="Localisation\es\TimeSpanHumanizeTests.cs" />
<Compile Include="Localisation\pt-BR\DateHumanizeTests.cs" />
<Compile Include="Localisation\pt-BR\TimeSpanTests.cs" />
<Compile Include="Localisation\pt-BR\TimeSpanHumanizeTests.cs" />
<Compile Include="RunnableInDebugModeOnlyAttribute.cs" />
<Compile Include="ToQuantityTests.cs" />
<Compile Include="TransformersTests.cs" />
Expand All @@ -100,7 +100,7 @@
<Compile Include="NumberToWordsTests.cs" />
<Compile Include="StringDehumanizeTests.cs" />
<Compile Include="StringHumanizeTests.cs" />
<Compile Include="TimeSpanHumanizeExtensionsTests.cs" />
<Compile Include="TimeSpanHumanizeTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ApiApprover\PublicApiApprovalTest.cs" />
<Compile Include="ApiApprover\PublicApiGenerator.cs" />
Expand All @@ -115,6 +115,9 @@
<Name>Humanizer</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Content Include="ApiApprover\PublicApiApprovalTest.approve_public_api.approved.txt" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\nuget.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using System;
using Humanizer.Tests;
using Xunit;
using Xunit.Extensions;

namespace Humanizer.Tests.Localisation.ar
{
public class TimeSpanHumanizeExtensionsTests : AmbientCulture
public class TimeSpanHumanizeTests : AmbientCulture
{
public TimeSpanHumanizeExtensionsTests() : base("ar") { }
public TimeSpanHumanizeTests() : base("ar") { }

[Theory]
[InlineData(7, "أسبوع واحد")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

namespace Humanizer.Tests.Localisation.es
{
public class TimeSpanHumanizeExtensionsTests : AmbientCulture
public class TimeSpanHumanizeTests : AmbientCulture
{
public TimeSpanHumanizeExtensionsTests() : base("es-ES") { }
public TimeSpanHumanizeTests() : base("es-ES") { }

[Fact]
public void TwoWeeks()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

namespace Humanizer.Tests.Localisation.ptBR
{
public class TimeSpanHumanizeExtensionsTests : AmbientCulture
public class TimeSpanHumanizeTests : AmbientCulture
{
public TimeSpanHumanizeExtensionsTests() : base("pt-BR") { }
public TimeSpanHumanizeTests() : base("pt-BR") { }

[Fact]
public void TwoWeeks()
Expand Down
122 changes: 0 additions & 122 deletions src/Humanizer.Tests/TimeSpanHumanizeExtensionsTests.cs

This file was deleted.

114 changes: 114 additions & 0 deletions src/Humanizer.Tests/TimeSpanHumanizeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
using System;
using Xunit;
using Xunit.Extensions;

namespace Humanizer.Tests
{
public class TimeSpanHumanizeTests : AmbientCulture
{
public TimeSpanHumanizeTests() : base("en-US") { }

[Theory]
[InlineData(14, "2 weeks")]
[InlineData(7, "1 week")]
public void Weeks(int days, string expected)
{
var actual = TimeSpan.FromDays(days).Humanize();
Assert.Equal(expected, actual);
}

[Theory]
[InlineData(6, "6 days")]
[InlineData(2, "2 days")]
[InlineData(1, "1 day")]
public void Days(int days, string expected)
{
var actual = TimeSpan.FromDays(days).Humanize();
Assert.Equal(expected, actual);
}

[Theory]
[InlineData(2, "2 hours")]
[InlineData(1, "1 hour")]
public void Hours(int hours, string expected)
{
var actual = TimeSpan.FromHours(hours).Humanize();
Assert.Equal(expected, actual);
}

[Theory]
[InlineData(2, "2 minutes")]
[InlineData(1, "1 minute")]
public void Minutes(int minutes, string expected)
{
var actual = TimeSpan.FromMinutes(minutes).Humanize();
Assert.Equal(expected, actual);
}

[Theory]
[InlineData(135, "2 minutes")]
[InlineData(60, "1 minute")]
[InlineData(2, "2 seconds")]
[InlineData(1, "1 second")]
public void Seconds(int seconds, string expected)
{
var actual = TimeSpan.FromSeconds(seconds).Humanize();
Assert.Equal(expected, actual);
}

[Theory]
[InlineData(2500, "2 seconds")]
[InlineData(1400, "1 second")]
[InlineData(2, "2 milliseconds")]
[InlineData(1, "1 millisecond")]
public void Milliseconds(int ms, string expected)
{
var actual = TimeSpan.FromMilliseconds(ms).Humanize();
Assert.Equal(expected, actual);
}

[Theory]
[InlineData(0, 3, "no time")]
[InlineData(0, 2, "no time")]
[InlineData(10, 2, "10 milliseconds")]
[InlineData(1400, 2, "1 second, 400 milliseconds")]
[InlineData(2500, 2, "2 seconds, 500 milliseconds")]
[InlineData(120000, 2, "2 minutes")]
[InlineData(62000, 2, "1 minute, 2 seconds")]
[InlineData(62020, 2, "1 minute, 2 seconds")]
[InlineData(62020, 3, "1 minute, 2 seconds, 20 milliseconds")]
[InlineData(3600020, 4, "1 hour, 20 milliseconds")]
[InlineData(3600020, 3, "1 hour, 20 milliseconds")]
[InlineData(3600020, 2, "1 hour, 20 milliseconds")]
[InlineData(3600020, 1, "1 hour")]
[InlineData(3603001, 2, "1 hour, 3 seconds")]
[InlineData(3603001, 3, "1 hour, 3 seconds, 1 millisecond")]
[InlineData(86400000, 3, "1 day")]
[InlineData(86400000, 2, "1 day")]
[InlineData(86400000, 1, "1 day")]
[InlineData(86401000, 1, "1 day")]
[InlineData(86401000, 2, "1 day, 1 second")]
[InlineData(86401200, 2, "1 day, 1 second")]
[InlineData(86401200, 3, "1 day, 1 second, 200 milliseconds")]
[InlineData(1296000000, 1, "2 weeks")]
[InlineData(1296000000, 2, "2 weeks, 1 day")]
[InlineData(1299600000, 2, "2 weeks, 1 day")]
[InlineData(1299600000, 3, "2 weeks, 1 day, 1 hour")]
[InlineData(1299630020, 3, "2 weeks, 1 day, 1 hour")]
[InlineData(1299630020, 4, "2 weeks, 1 day, 1 hour, 30 seconds")]
[InlineData(1299630020, 5, "2 weeks, 1 day, 1 hour, 30 seconds, 20 milliseconds")]
public void TimeSpanWithPrecesion(int milliseconds, int precesion, string expected)
{
var actual = TimeSpan.FromMilliseconds(milliseconds).Humanize(precesion);
Assert.Equal(expected, actual);
}

[Fact]
public void NoTime()
{
var noTime = TimeSpan.Zero;
var actual = noTime.Humanize();
Assert.Equal("no time", actual);
}
}
}
Loading