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

Convert TimeUnit to symbols #1106

Merged
merged 4 commits into from
Oct 27, 2021
Merged

Conversation

hangy
Copy link
Contributor

@hangy hangy commented Jul 3, 2021

Here is a checklist you should tick through before submitting a pull request:

  • Implementation is clean
  • Code adheres to the existing coding standards; e.g. no curlies for one-line blocks, no redundant empty lines between methods or code blocks, spaces rather than tabs, etc.
  • No Code Analysis warnings
  • There is proper unit test coverage
  • If the code is copied from StackOverflow (or a blog or OSS) full disclosure is included. That includes required license files and/or file headers explaining where the code came from with proper attribution
  • There are very few or no comments (because comments shouldn't be needed if you write clean code)
  • Xml documentation is added/updated for the addition/change
  • Your PR is (re)based on top of the latest commits from the dev branch (more info below)
  • Link to the issue(s) you're fixing from your PR description. Use fixes #<the issue number>
  • Readme is updated if you change an existing feature or add a new one
  • Run either build.cmd or build.ps1 and ensure there are no test failures

@hangy
Copy link
Contributor Author

hangy commented Jul 3, 2021

OK, this probably needs some discussion. 😊

I want to use localized versions of TimeUnit to localize ByteRate.Humanize, which currently only returns English time units.

public string Humanize(string format, TimeUnit timeUnit = TimeUnit.Second)
{
TimeSpan displayInterval;
string displayUnit;
if (timeUnit == TimeUnit.Second)
{
displayInterval = TimeSpan.FromSeconds(1);
displayUnit = "s";
}
else if (timeUnit == TimeUnit.Minute)
{
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");
}
return new ByteSize(Size.Bytes / Interval.TotalSeconds * displayInterval.TotalSeconds)
.Humanize(format) + '/' + displayUnit;
}

I assume that a general method to get a symbol from the TimeUnit could be useful, which is why I added the TimeUnitToSymbolExtensions.ToSymbol method. However, I'm not quite sure how/if it was okay to leak Humanizer.Localisation.TimeUnit to a broader namespace. I'm open for any suggestions. 😊

@hangy hangy force-pushed the timeunit-humanize branch from 2e3c668 to add795c Compare July 3, 2021 14:25
@hangy hangy force-pushed the timeunit-humanize branch from add795c to 630b78c Compare July 3, 2021 14:35
@hangy hangy mentioned this pull request Jul 5, 2021
11 tasks
[InlineData(TimeUnit.Day, "d")]
[InlineData(TimeUnit.Week, "week")]
[InlineData(TimeUnit.Month, "mo")]
[InlineData(TimeUnit.Year, "a")]
Copy link
Member

Choose a reason for hiding this comment

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

What does the a mean here? Not sure I've seen this before, is there an example you can point to?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I took it from Wikipedia, which listed both "a" (per annum?) and "y" (per year) as possible versions in English. Will fix as discussed. 👍🏻

@hangy hangy requested a review from clairernovotny October 27, 2021 18:40
@clairernovotny clairernovotny merged commit bc1a9b7 into Humanizr:main Oct 27, 2021
@hangy hangy deleted the timeunit-humanize branch October 27, 2021 18:50
@hangy
Copy link
Contributor Author

hangy commented Oct 27, 2021

Thanks for the review! 🙂

@neilboyd neilboyd mentioned this pull request Feb 8, 2022
11 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants