Humanizr/Humanizer · error · NotSupportedException

timeUnit must be Second, Minute, or Hour

Error message

timeUnit must be Second, Minute, or Hour

What it means

Thrown by ByteRate.Humanize(string?, TimeUnit, CultureInfo?) when the timeUnit argument is not TimeUnit.Second, TimeUnit.Minute, or TimeUnit.Hour. The method computes a display interval from the TimeUnit via a switch expression; the default arm throws NotSupportedException because no other TimeUnit (Day, Week, etc.) has a defined byte-rate display interval.

Source

Thrown at src/Humanizer/Bytes/ByteRate.cs:46

    /// </summary>
    /// <param name="timeUnit">Unit of time to calculate rate for (defaults is per second)</param>
    public string Humanize(TimeUnit timeUnit = TimeUnit.Second) =>
        Humanize(null, timeUnit);

    /// <summary>
    /// Calculate rate for the quantity of bytes and interval defined by this instance
    /// </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 culture is used.</param>
    public string Humanize(string? format, TimeUnit timeUnit = TimeUnit.Second, CultureInfo? culture = null)
    {
        var displayInterval = timeUnit switch
        {
            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, culture) + '/' + timeUnit.ToSymbol(culture);
    }

    /// <summary>
    /// Calculates and humanizes this rate using an explicitly selected byte-size unit system.
    /// </summary>
    /// <param name="unitSystem">The byte-size unit system to use.</param>
    /// <param name="format">
    /// The numeric format and optional byte-size unit token. For <see cref="ByteSizeUnitSystem.DecimalSi"/> and
    /// <see cref="ByteSizeUnitSystem.BinaryIec"/>, SI/IEC-prefixed unit tokens are matched case-insensitively,
    /// while <c>b</c> and <c>B</c> remain case-sensitive; output uses canonical symbol casing.
    /// <see cref="ByteSizeUnitSystem.Legacy"/> preserves established matching behavior.
    /// </param>
    /// <param name="timeUnit">The time unit to use for the displayed rate.</param>
    /// <param name="culture">The culture used to format the numeric value, byte-size unit, and time-unit symbol.</param>
    /// <returns>The humanized rate.</returns>

View on GitHub (pinned to ffc2b77c0f)

Solutions

  1. Constrain the timeUnit argument to Second, Minute, or Hour before calling Humanize.
  2. If you need a different time unit, compute the rate manually using ByteRate.Size and Interval rather than relying on Humanize's timeUnit switch.
  3. Validate user-supplied TimeUnit input against the supported subset before passing it to Humanize.

Example fix

// before
var rate = byteRate.Humanize(null, TimeUnit.Day);

// after
var rate = byteRate.Humanize(null, TimeUnit.Second);
Defensive patterns

Strategy: validation

Validate before calling

if (timeUnit is not (TimeUnit.Second or TimeUnit.Minute or TimeUnit.Hour))
    throw new ArgumentOutOfRangeException(nameof(timeUnit), "Must be Second, Minute, or Hour");
var rate = byteRate.Humanize(format, timeUnit, culture);

Type guard

static bool IsValidByteRateTimeUnit(TimeUnit unit) =>
    unit is TimeUnit.Second or TimeUnit.Minute or TimeUnit.Hour;

Prevention

When it happens

Trigger: Calling byteRate.Humanize(format, timeUnit) where timeUnit is TimeUnit.Day, TimeUnit.Week, TimeUnit.Month, TimeUnit.Year, or any other TimeUnit enum value outside the three supported ones.

Common situations: A developer iterates over TimeUnit enum values and passes each to Humanize without filtering. Or a caller derives the TimeUnit from user input or configuration without constraining it to the three supported values.

Related errors


AI-assisted analysis of Humanizr/Humanizer@ffc2b77c0f (2026-08-13). Data as JSON: /api/errors/7d8d5de326e5f25c. Report an issue: GitHub.