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

[WIP] Change strategy to add Weeks. Add Week property #807

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ public void HoursFromNowNotTomorrow(int hours, string expected)

[Theory]
[InlineData(1, "yesterday")]
[InlineData(10, "10 days ago")]
[InlineData(27, "27 days ago")]
Copy link
Collaborator

Choose a reason for hiding this comment

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

given this test passes in TimeUnit.Day, why would it result in week/weeks

also 10 days is no "on week"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, I think this is going to be a culture thing and then a question of how precise people would like humanizer to be.

7 days would be "a week ago", and in fact if someone asked me about something 8 days ago I might also accept "a week ago".

Perhaps I should look at making this only provide weeks if the value is one of 7,14,21,28. And then fall backs to months as expected after that 🤔

[InlineData(10, "one week ago")]
[InlineData(27, "3 weeks ago")]
[InlineData(32, "one month ago")]
public void DaysAgo(int days, string expected)
{
Expand All @@ -98,8 +98,8 @@ public void DaysAgo(int days, string expected)

[Theory]
[InlineData(1, "tomorrow")]
[InlineData(10, "10 days from now")]
[InlineData(27, "27 days from now")]
[InlineData(10, "one week from now")]
[InlineData(27, "3 weeks from now")]
[InlineData(32, "one month from now")]
public void DaysFromNow(int days, string expected)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,16 @@ public static string DefaultHumanize(DateTime input, DateTime comparisonBase, Cu
return formatter.DateHumanize(TimeUnit.Day, tense, days);
}

if (ts.TotalDays < 28)
if (ts.TotalDays < 7)
{
return formatter.DateHumanize(TimeUnit.Day, tense, ts.Days);
}

if (ts.TotalDays < 28)
{
return formatter.DateHumanize(TimeUnit.Week, tense, ts.Days / 7);
}

if (ts.TotalDays >= 28 && ts.TotalDays < 30)
{
if (comparisonBase.Date.AddMonths(tense == Tense.Future ? 1 : -1) == input.Date)
Expand All @@ -180,4 +185,4 @@ public static string DefaultHumanize(DateTime input, DateTime comparisonBase, Cu
return formatter.DateHumanize(TimeUnit.Year, tense, years);
}
}
}
}
12 changes: 12 additions & 0 deletions src/Humanizer/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@
<data name="DateHumanize_MultipleDaysAgo" xml:space="preserve">
<value>{0} days ago</value>
</data>
<data name="DateHumanize_SingleWeekAgo" xml:space="preserve">
<value>one week ago</value>
</data>
<data name="DateHumanize_MultipleWeeksAgo" xml:space="preserve">
<value>{0} weeks ago</value>
</data>
<data name="DateHumanize_SingleMonthAgo" xml:space="preserve">
<value>one month ago</value>
</data>
Expand Down Expand Up @@ -207,6 +213,9 @@
<data name="DateHumanize_MultipleSecondsFromNow" xml:space="preserve">
<value>{0} seconds from now</value>
</data>
<data name="DateHumanize_MultipleWeeksFromNow" xml:space="preserve">
<value>{0} weeks from now</value>
</data>
<data name="DateHumanize_MultipleYearsFromNow" xml:space="preserve">
<value>{0} years from now</value>
</data>
Expand All @@ -228,6 +237,9 @@
<data name="DateHumanize_SingleSecondFromNow" xml:space="preserve">
<value>one second from now</value>
</data>
<data name="DateHumanize_SingleWeekFromNow" xml:space="preserve">
<value>one week from now</value>
</data>
<data name="DateHumanize_SingleYearFromNow" xml:space="preserve">
<value>one year from now</value>
</data>
Expand Down