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

Localize ByteRate humanization #1108

Merged
merged 3 commits into from
Oct 27, 2021
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
6 changes: 3 additions & 3 deletions src/Humanizer.Tests.Shared/Bytes/ByteRateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ public void HumanizesRates(long inputBytes, double perSeconds, string expectedVa
[Theory]
[InlineData(1, 1, TimeUnit.Second, "1 MB/s")]
[InlineData(1, 60, TimeUnit.Minute, "1 MB/min")]
[InlineData(1, 60 * 60, TimeUnit.Hour, "1 MB/hour")]
[InlineData(1, 60 * 60, TimeUnit.Hour, "1 MB/h")]
[InlineData(10, 1, TimeUnit.Second, "10 MB/s")]
[InlineData(10, 60, TimeUnit.Minute, "10 MB/min")]
[InlineData(10, 60 * 60, TimeUnit.Hour, "10 MB/hour")]
[InlineData(10, 60 * 60, TimeUnit.Hour, "10 MB/h")]
[InlineData(1, 10 * 1, TimeUnit.Second, "102.4 KB/s")]
[InlineData(1, 10 * 60, TimeUnit.Minute, "102.4 KB/min")]
[InlineData(1, 10 * 60 * 60, TimeUnit.Hour, "102.4 KB/hour")]
[InlineData(1, 10 * 60 * 60, TimeUnit.Hour, "102.4 KB/h")]
public void TimeUnitTests(long megabytes, double measurementIntervalSeconds, TimeUnit displayInterval, string expectedValue)
{
var size = ByteSize.FromMegabytes(megabytes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Localisation\de-CH\NumberToWordsTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Localisation\de-LI\NumberToWordsTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Localisation\DefaultFormatterTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Localisation\de\Bytes\ByteRateTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Localisation\de\Bytes\ByteSizeExtensionsTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Localisation\de\Bytes\ToFullWordsTest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Localisation\de\Bytes\ToStringTests.cs" />
Expand Down
80 changes: 80 additions & 0 deletions src/Humanizer.Tests.Shared/Localisation/de/Bytes/ByteRateTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System;

using Humanizer.Bytes;
using Humanizer.Localisation;

using Xunit;

namespace Humanizer.Tests.Localisation.de.Bytes
{
[UseCulture("de-DE")]
public class ByteRateTests
{
[Theory]
[InlineData(400, 1, "400 B/s")]
[InlineData(4 * 1024, 1, "4 kB/s")]
[InlineData(4 * 1024 * 1024, 1, "4 MB/s")]
[InlineData(4 * 2 * 1024 * 1024, 2, "4 MB/s")]
[InlineData(4 * 1024, 0.1, "40 kB/s")]
[InlineData(15 * 60 * 1024 * 1024, 60, "15 MB/s")]
public void HumanizesRates(long inputBytes, double perSeconds, string expectedValue)
{
var size = new ByteSize(inputBytes);
var interval = TimeSpan.FromSeconds(perSeconds);

var rate = size.Per(interval).Humanize();

Assert.Equal(expectedValue, rate);
}

[Theory]
[InlineData(1, 1, TimeUnit.Second, "1 MB/s")]
[InlineData(1, 60, TimeUnit.Minute, "1 MB/min")]
[InlineData(1, 60 * 60, TimeUnit.Hour, "1 MB/h")]
[InlineData(10, 1, TimeUnit.Second, "10 MB/s")]
[InlineData(10, 60, TimeUnit.Minute, "10 MB/min")]
[InlineData(10, 60 * 60, TimeUnit.Hour, "10 MB/h")]
[InlineData(1, 10 * 1, TimeUnit.Second, "102,4 kB/s")]
[InlineData(1, 10 * 60, TimeUnit.Minute, "102,4 kB/min")]
[InlineData(1, 10 * 60 * 60, TimeUnit.Hour, "102,4 kB/h")]
public void TimeUnitTests(long megabytes, double measurementIntervalSeconds, TimeUnit displayInterval, string expectedValue)
{
var size = ByteSize.FromMegabytes(megabytes);
var measurementInterval = TimeSpan.FromSeconds(measurementIntervalSeconds);

var rate = size.Per(measurementInterval);
var text = rate.Humanize(displayInterval);

Assert.Equal(expectedValue, text);
}

[Theory]
[InlineData(19854651984, 1, TimeUnit.Second, null, "18,4910856038332 GB/s")]
[InlineData(19854651984, 1, TimeUnit.Second, "#.##", "18,49 GB/s")]
public void FormattedTimeUnitTests(long bytes, int measurementIntervalSeconds, TimeUnit displayInterval, string format, string expectedValue)
{
var size = ByteSize.FromBytes(bytes);
var measurementInterval = TimeSpan.FromSeconds(measurementIntervalSeconds);
var rate = size.Per(measurementInterval);
var text = rate.Humanize(format, displayInterval);

Assert.Equal(expectedValue, text);
}

[Theory]
[InlineData(TimeUnit.Millisecond)]
[InlineData(TimeUnit.Day)]
[InlineData(TimeUnit.Month)]
[InlineData(TimeUnit.Week)]
[InlineData(TimeUnit.Year)]
public void ThowsOnUnsupportedData(TimeUnit units)
{
var dummyRate = ByteSize.FromBits(1).Per(TimeSpan.FromSeconds(1));

Assert.Throws<NotSupportedException>(() =>
{
dummyRate.Humanize(units);
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1639,7 +1639,7 @@ namespace Humanizer.Bytes
public System.TimeSpan Interval { get; }
public Humanizer.Bytes.ByteSize Size { get; }
public string Humanize(Humanizer.Localisation.TimeUnit timeUnit = 1) { }
public string Humanize(string format, Humanizer.Localisation.TimeUnit timeUnit = 1) { }
public string Humanize(string format, Humanizer.Localisation.TimeUnit timeUnit = 1, System.Globalization.CultureInfo culture = null) { }
}
public struct ByteSize : System.IComparable, System.IComparable<Humanizer.Bytes.ByteSize>, System.IEquatable<Humanizer.Bytes.ByteSize>, System.IFormattable
{
Expand Down
35 changes: 11 additions & 24 deletions src/Humanizer/Bytes/ByteRate.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Globalization;

using Humanizer.Localisation;

namespace Humanizer.Bytes
Expand Down Expand Up @@ -47,34 +49,19 @@ public string Humanize(TimeUnit timeUnit = TimeUnit.Second)
/// </summary>
/// <param name="timeUnit">Unit of time to calculate rate for (defaults is per second)</param>
/// <param name="format">The string format to use for the number of bytes</param>
/// <param name="culture">Culture to use. If null, current thread's UI culture is used.</param>
/// <returns></returns>
public string Humanize(string format, TimeUnit timeUnit = TimeUnit.Second)
public string Humanize(string format, TimeUnit timeUnit = TimeUnit.Second, CultureInfo culture = null)
{
TimeSpan displayInterval;
string displayUnit;

if (timeUnit == TimeUnit.Second)
{
displayInterval = TimeSpan.FromSeconds(1);
displayUnit = "s";
}
else if (timeUnit == TimeUnit.Minute)
var displayInterval = timeUnit switch
{
displayInterval = TimeSpan.FromMinutes(1);
displayUnit = "min";
}
else if (timeUnit == TimeUnit.Hour)
{
displayInterval = TimeSpan.FromHours(1);
displayUnit = "hour";
}
else
{
throw new NotSupportedException("timeUnit must be Second, Minute, or Hour");
}

TimeUnit.Second => TimeSpan.FromSeconds(1),
TimeUnit.Minute => TimeSpan.FromMinutes(1),
TimeUnit.Hour => TimeSpan.FromHours(1),
_ => throw new NotSupportedException("timeUnit must be Second, Minute, or Hour"),
};
return new ByteSize(Size.Bytes / Interval.TotalSeconds * displayInterval.TotalSeconds)
.Humanize(format) + '/' + displayUnit;
.Humanize(format, culture) + '/' + timeUnit.ToSymbol(culture);
}
}
}