humanizer

Is it possible to Humanize TimeSpan to years with Humanizer?


We want to leverage the Humanizer package (as we use it elsewhere) and don't want to add more similar libraries or hand rolled code.

I have timespans some of which are thousands of days. I want to humanize them and convert them to years. Now I appreciate a year varies but if I am converting 10000 days to years - I am happy with the loss of precision.

 TimeSpan.FromDays(10000).Humanize(minUnit: TimeUnit.Year);

returns

"no time"

TimeSpan.FromDays(10000).Humanize();

returns

"1428 weeks"

If I convert them to DateTimes by adding to now.

DateTime.Today.Add(TimeSpan.FromDays(10000)).Humanize(); 

returns

"27 years from now"

It always appends from now - can I remove the from now part in an inbuilt way?


Solution

  • As you've found out Humanizer cannot describe TimeSpans with a accuracy larger than weeks. This is because it is impossible to know if a year has 365 or 366 days or how many days a month is.

    However, this is possible when it needs to describe the relative TimeSpan (i.e some time ago) because Humanizer is then able to calculate if the day has 365 or 366 days.

    Once Pull request #604 has been merged this functionality should be available and calculate a year as 365.25 days. This will results in a loss of precision, but as you've mention this is not an issue.