Humanizer icon indicating copy to clipboard operation
Humanizer copied to clipboard

TimeSpan.FromSeconds(-10).Humanize() SHOULD NOT EQUAL TimeSpan.FromSeconds(10).Humanize()

Open ghost opened this issue 3 years ago • 2 comments

A negative TimeSpan should retain its direction when Humanized.

ghost avatar Aug 14 '20 10:08 ghost

We pass an absolute value to BuildFormatTimePart() in the TimeSpanHumanizerExt so we can return the correct word. I would suggest adding negative word when we assemble the List in CreateTheTimePartsWithUpperAndLowerLimits(), but we would still need to fetch the word in a cultureFormat for the correct word and grammar.

private static IEnumerable<string> CreateTheTimePartsWithUpperAndLowerLimits(TimeSpan timespan, CultureInfo culture, TimeUnit maxUnit, TimeUnit minUnit, bool toWords = false)
        {

            /* ... */


            foreach (var timeUnitType in timeUnitsEnumTypes)
            {
                var timepart = GetTimeUnitPart(timeUnitType,timespan, maxUnit, minUnit, cultureFormatter, toWords); 

                if (timepart != null || firstValueFound)
                {
                    firstValueFound = true;
                    timeParts.Add(timepart);
                }
            }

            // Add the Negative word (E.g "Minus") ad the begining or end depending on Locale) of the list

@clairernovotny Since it would affect the IFormatter Interface, I would ask your opinion on this solution.

mr-aboutin avatar Sep 26 '20 20:09 mr-aboutin

100% would be an excellent feature for negative timespans to return "2 Days, 45 Minutes ago"

Fungusware avatar Mar 10 '21 11:03 Fungusware