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

fix time span humanization precision skipping #340

Merged
merged 5 commits into from
Nov 7, 2014
Merged

Conversation

mexx
Copy link
Collaborator

@mexx mexx commented Oct 21, 2014

fixes #339

@MehdiK
Copy link
Member

MehdiK commented Oct 29, 2014

What do you think about the suggestions at #339 (comment)?

/// <returns></returns>
public static string Humanize(this TimeSpan timeSpan, int precision = 1, CultureInfo culture = null)
public static string Humanize(this TimeSpan timeSpan, int precision = 1, CultureInfo culture = null, bool considerEmptyParts = false)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not so sure about the optional param. It's not clear why one would want to use that and it could actually be used without passing the precision, timespan.Humanize(considerEmptyParts: true), which is not good.

I'd suggest adding the following three methods:

public static string Humanize(this TimeSpan timeSpan, CultureInfo culture = null)
public static string Humanize(this TimeSpan timeSpan, int precision, bool ignoreEmptyUnits = false, CultureInfo culture = null)
[Deprecated]
public static string Humanize(this TimeSpan timeSpan, int precision, CultureInfo culture = null)

A couple of things to highlight:

  • precision on the overload is not optional anymore
  • considerEmptyParts has been renamed to ignoreEmptyUnits but the default value is left as false. This means that the logic should change to by default not ignore empty units which will keep the change backward compatible for those who hadn't passed the culture in. Due to the name change the implementation needs to be reversed too.

@MehdiK
Copy link
Member

MehdiK commented Nov 7, 2014

Thanks for the change @mexx. I left a comment on the method signature. What do you think?

Also please add the addition to the readme file.

@mexx
Copy link
Collaborator Author

mexx commented Nov 7, 2014

I've changed the implementation and made the parameters in the overload mandatory,
as it correctly don't make sense to call it without precision.
I also renamed the parameter to countEmptyUnits, for me it better transports the meaning.
For example:

TimeSpan.FromMilliseconds(3603001).Humanize(3, countEmptyUnits:true)

With ignoreEmptyUnits it should be

TimeSpan.FromMilliseconds(3603001).Humanize(3, ignoreEmptyUnits:false)

as by default of current behavior empty units would be ignored.

@MehdiK What do you think?

@MehdiK
Copy link
Member

MehdiK commented Nov 7, 2014

I like it. Thanks.

MehdiK added a commit that referenced this pull request Nov 7, 2014
fix time span humanization precision skipping
@MehdiK MehdiK merged commit 382c03f into Humanizr:master Nov 7, 2014
@mexx mexx deleted the fix-339 branch November 7, 2014 23:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants