Skip to content

Commit

Permalink
Implement localized version of ByteRate.Humanize()
Browse files Browse the repository at this point in the history
This used TimeUnit.ToSymbol() as it's underlying localization, so that
it only needs to be done once.
  • Loading branch information
hangy committed Oct 27, 2021
1 parent 499ba50 commit 925d758
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 25 deletions.
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
@@ -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);
}
}
}

0 comments on commit 925d758

Please sign in to comment.