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

Future date support #68

Closed
wants to merge 5 commits into from
Closed
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
78 changes: 75 additions & 3 deletions src/Humanizer.Tests/DateHumanizeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,89 @@ void Verify(string expectedString, TimeSpan deltaFromNow)
}

[Fact]
public void FutureDates()
public void OneSecondFromNow()
{
Verify(Resources.GetResource(ResourceKeys.DateHumanize.NotYet), new TimeSpan(0, 0, 1, 0));
Verify(Resources.GetResource(ResourceKeys.DateHumanize.SingleSecondFromNow), new TimeSpan(0, 0, 0, 1));
}

[Fact]
public void SecondsFromNow()
{
Verify(string.Format(Resources.GetResource(ResourceKeys.DateHumanize.MultipleSecondsFromNow), 10), new TimeSpan(0, 0, 0, 10));
}

[Fact]
public void OneMinuteFromNow()
{
Verify(Resources.GetResource(ResourceKeys.DateHumanize.SingleMinuteFromNow), new TimeSpan(0, 0, 1, 0));
}

[Fact]
public void AFewMinutesFromNow()
{
Verify(string.Format(Resources.GetResource(ResourceKeys.DateHumanize.MultipleMinutesFromNow), 10), new TimeSpan(0, 0, 10, 0));
}

[Fact]
public void AnHourFromNow()
{
Verify(Resources.GetResource(ResourceKeys.DateHumanize.SingleHourFromNow), new TimeSpan(0, 1, 10, 0));
}

[Fact]
public void HoursFromNow()
{
Verify(string.Format(Resources.GetResource(ResourceKeys.DateHumanize.MultipleHoursFromNow), 10), new TimeSpan(0, 10, 0, 0));
}

[Fact]
public void Tomorrow()
{
Verify(Resources.GetResource(ResourceKeys.DateHumanize.SingleDayFromNow), new TimeSpan(1, 10, 0, 0));
}

[Fact]
public void AFewDaysFromNow()
{
Verify(string.Format(Resources.GetResource(ResourceKeys.DateHumanize.MultipleDaysFromNow), 10), new TimeSpan(10, 0, 0, 0));
}

[Fact]
public void OneMonthFromNow()
{
Verify(Resources.GetResource(ResourceKeys.DateHumanize.SingleMonthFromNow), new TimeSpan(30, 0, 0, 0));
}

[Fact]
public void AFewMonthsFromNow()
{
Verify(string.Format(Resources.GetResource(ResourceKeys.DateHumanize.MultipleMonthsFromNow), 2), new TimeSpan(60, 0, 0, 0));
}

[Fact]
public void OneYearFromNowIsNotAccureate()
{
Verify(Resources.GetResource(ResourceKeys.DateHumanize.SingleYearFromNow), new TimeSpan(360, 0, 0, 0));
}

[Fact]
public void OneYearFromNow()
{
Verify(Resources.GetResource(ResourceKeys.DateHumanize.SingleYearFromNow), new TimeSpan(400, 0, 0, 0));
}

[Fact]
public void FewYearsFromNow()
{
Verify(string.Format(Resources.GetResource(ResourceKeys.DateHumanize.MultipleYearsFromNow), 2), new TimeSpan(900, 0, 0, 0));
}

[Fact]
public void JustNow()
{
Verify(Resources.GetResource(ResourceKeys.DateHumanize.SingleSecondAgo), new TimeSpan(0, 0, 0, -1));
}

[Fact]
public void SecondsAgo()
{
Expand Down
3 changes: 3 additions & 0 deletions src/Humanizer.Tests/Humanizer.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@
<ItemGroup>
<Content Include="ApiApprover\PublicApiApprovalTest.approve_public_api.approved.txt" />
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
9 changes: 0 additions & 9 deletions src/Humanizer.Tests/Localisation/DateHumanizeTests.fi-FI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,5 @@ public void YearsAgo(int years, string expected)
var date = DateTime.UtcNow.AddYears(years);
Assert.Equal(expected, date.Humanize());
}

[Fact]
public void NotYet()
{
// does not yet mean in future?
var date = DateTime.UtcNow.AddDays(1);

Assert.Equal("ei vielä", date.Humanize());
}
}
}
9 changes: 0 additions & 9 deletions src/Humanizer.Tests/Localisation/DateHumanizeTests.nb-NO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,5 @@ public void YearsAgo(int years, string expected)
var date = DateTime.UtcNow.AddYears(years);
Assert.Equal(expected, date.Humanize());
}

[Fact]
public void NotYet()
{
// does not yet mean in future?
var date = DateTime.UtcNow.AddDays(1);

Assert.Equal("ikke enda", date.Humanize());
}
}
}
7 changes: 0 additions & 7 deletions src/Humanizer.Tests/Localisation/DateHumanizeTests.ru-RU.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,5 @@ public void NYearsAgo(int number, string expected)
var humanize = DateTime.UtcNow.Date.AddYears(-1 * number).Humanize();
Assert.Equal(expected, humanize);
}

[Fact]
public void NotYet()
{
var humanize = DateTime.UtcNow.AddDays(1).Humanize();
Assert.Equal("в будущем", humanize);
}
}
}
6 changes: 0 additions & 6 deletions src/Humanizer.Tests/Localisation/ar/DateHumanizeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,5 @@ public void YearsAgo(int years, string expected)
{
Assert.Equal(expected, DateTime.UtcNow.AddYears(years).Humanize());
}

[Fact]
public void NotYet()
{
Assert.Equal("ليس بعد", DateTime.UtcNow.AddDays(1).Humanize());
}
}
}
6 changes: 0 additions & 6 deletions src/Humanizer.Tests/Localisation/es/DateHumanizeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,5 @@ public void YearsAgo(int years, string expected)
{
Assert.Equal(expected, DateTime.UtcNow.AddYears(years).Humanize());
}

[Fact]
public void NotYet()
{
Assert.Equal("aún no", DateTime.UtcNow.AddDays(1).Humanize());
}
}
}
6 changes: 0 additions & 6 deletions src/Humanizer.Tests/Localisation/pt-BR/DateHumanizeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,5 @@ public void YearsAgo(int years, string expected)
{
Assert.Equal(expected, DateTime.UtcNow.AddYears(years).Humanize());
}

[Fact]
public void NotYet()
{
Assert.Equal("ainda não", DateTime.UtcNow.AddDays(1).Humanize());
}
}
}
33 changes: 24 additions & 9 deletions src/Humanizer/DateHumanizeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,40 +33,55 @@ public static string Humanize(this DateTime input, bool utcDate = true, DateTime

var formatter = Configurator.Formatter;

if (input > comparisonBase)
return formatter.DateHumanize_NotYet();
var isFuture = input > comparisonBase;

var ts = new TimeSpan(comparisonBase.Ticks - input.Ticks);
if(isFuture)
ts = new TimeSpan(-ts.Ticks);

double delta = Math.Abs(ts.TotalSeconds);

if (delta < 1 * minute)
if (delta < 1*minute)
{
if(isFuture)
return ts.Seconds == 1 ? formatter.DateHumanize_SingleSecondFromNow() : formatter.DateHumanize_MultipleSecondsFromNow(ts.Seconds);

return ts.Seconds == 1 ? formatter.DateHumanize_SingleSecondAgo() : formatter.DateHumanize_MultipleSecondsAgo(ts.Seconds);
}

if (delta < 2 * minute)
return formatter.DateHumanize_SingleMinuteAgo();
return isFuture ? formatter.DateHumanize_SingleMinuteFromNow() : formatter.DateHumanize_SingleMinuteAgo();

if (delta < 45 * minute)
return formatter.DateHumanize_MultipleMinutesAgo(ts.Minutes);
return isFuture ? formatter.DateHumanize_MultipleMinutesFromNow(ts.Minutes) : formatter.DateHumanize_MultipleMinutesAgo(ts.Minutes);

if (delta < 90 * minute)
return formatter.DateHumanize_SingleHourAgo();
return isFuture ? formatter.DateHumanize_SingleHourFromNow() : formatter.DateHumanize_SingleHourAgo();

if (delta < 24 * hour)
return formatter.DateHumanize_MultipleHoursAgo(ts.Hours);
return isFuture ? formatter.DateHumanize_MultipleHoursFromNow(ts.Hours) : formatter.DateHumanize_MultipleHoursAgo(ts.Hours);

if (delta < 48 * hour)
return formatter.DateHumanize_SingleDayAgo();
return isFuture ? formatter.DateHumanize_SingleDayFromNow() : formatter.DateHumanize_SingleDayAgo();

if (delta < 30 * day)
return formatter.DateHumanize_MultipleDaysAgo(ts.Days);
return isFuture ? formatter.DateHumanize_MultipleDaysFromNow(ts.Days) : formatter.DateHumanize_MultipleDaysAgo(ts.Days);

if (delta < 12 * month)
{
int months = Convert.ToInt32(Math.Floor((double)ts.Days / 30));

if(isFuture)
return months <= 1 ? formatter.DateHumanize_SingleMonthFromNow() : formatter.DateHumanize_MultipleMonthsFromNow(months);

return months <= 1 ? formatter.DateHumanize_SingleMonthAgo() : formatter.DateHumanize_MultipleMonthsAgo(months);
}

int years = Convert.ToInt32(Math.Floor((double)ts.Days / 365));

if(isFuture)
return years <= 1 ? formatter.DateHumanize_SingleYearFromNow() : formatter.DateHumanize_MultipleYearsFromNow(years);

return years <= 1 ? formatter.DateHumanize_SingleYearAgo() : formatter.DateHumanize_MultipleYearsAgo(years);
}
}
Expand Down
70 changes: 65 additions & 5 deletions src/Humanizer/Localisation/DefaultFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@ public virtual string DateHumanize_SingleHourAgo()
return Resources.GetResource(ResourceKeys.DateHumanize.SingleHourAgo);
}

public virtual string DateHumanize_NotYet()
{
return Resources.GetResource(ResourceKeys.DateHumanize.NotYet);
}

public virtual string DateHumanize_SingleMonthAgo()
{
return Resources.GetResource(ResourceKeys.DateHumanize.SingleMonthAgo);
Expand All @@ -67,6 +62,71 @@ public virtual string DateHumanize_SingleDayAgo()
return Resources.GetResource(ResourceKeys.DateHumanize.SingleDayAgo);
}

public virtual string DateHumanize_MultipleDaysFromNow(int numberOfDays)
{
return Format(ResourceKeys.DateHumanize.MultipleDaysFromNow, numberOfDays);
}

public virtual string DateHumanize_MultipleHoursFromNow(int numberOfHours)
{
return Format(ResourceKeys.DateHumanize.MultipleHoursFromNow, numberOfHours);
}

public virtual string DateHumanize_MultipleMinutesFromNow(int numberOfMinutes)
{
return Format(ResourceKeys.DateHumanize.MultipleMinutesFromNow, numberOfMinutes);
}

public virtual string DateHumanize_MultipleMonthsFromNow(int numberOfMonths)
{
return Format(ResourceKeys.DateHumanize.MultipleMonthsFromNow, numberOfMonths);
}

public virtual string DateHumanize_MultipleSecondsFromNow(int numberOfSeconds)
{
return Format(ResourceKeys.DateHumanize.MultipleSecondsFromNow, numberOfSeconds);
}

public virtual string DateHumanize_MultipleYearsFromNow(int numberOfYears)
{
return Format(ResourceKeys.DateHumanize.MultipleYearsFromNow, numberOfYears);
}

public virtual string DateHumanize_SingleMinuteFromNow()
{
return Resources.GetResource(ResourceKeys.DateHumanize.SingleMinuteFromNow);
}

public virtual string DateHumanize_SingleHourFromNow()
{
return Resources.GetResource(ResourceKeys.DateHumanize.SingleHourFromNow);
}

public virtual string DateHumanize_Now()
{
return Resources.GetResource(ResourceKeys.DateHumanize.Now);
}

public virtual string DateHumanize_SingleMonthFromNow()
{
return Resources.GetResource(ResourceKeys.DateHumanize.SingleMonthFromNow);
}

public virtual string DateHumanize_SingleSecondFromNow()
{
return Resources.GetResource(ResourceKeys.DateHumanize.SingleSecondFromNow);
}

public virtual string DateHumanize_SingleYearFromNow()
{
return Resources.GetResource(ResourceKeys.DateHumanize.SingleYearFromNow);
}

public virtual string DateHumanize_SingleDayFromNow()
{
return Resources.GetResource(ResourceKeys.DateHumanize.SingleDayFromNow);
}

public virtual string TimeSpanHumanize_MultipleWeeks(int weeks)
{
return Format(ResourceKeys.TimeSpanHumanize.MultipleWeeks, weeks);
Expand Down
14 changes: 13 additions & 1 deletion src/Humanizer/Localisation/IFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,23 @@ public interface IFormatter
string DateHumanize_MultipleYearsAgo(int numberOfYears);
string DateHumanize_SingleMinuteAgo();
string DateHumanize_SingleHourAgo();
string DateHumanize_NotYet();
string DateHumanize_SingleMonthAgo();
string DateHumanize_SingleSecondAgo();
string DateHumanize_SingleYearAgo();
string DateHumanize_SingleDayAgo();
string DateHumanize_MultipleDaysFromNow(int numberOfDays);
string DateHumanize_MultipleHoursFromNow(int numberOfHours);
string DateHumanize_MultipleMinutesFromNow(int numberOfMinutes);
string DateHumanize_MultipleMonthsFromNow(int numberOfMonths);
string DateHumanize_MultipleSecondsFromNow(int numberOfSeconds);
string DateHumanize_MultipleYearsFromNow(int numberOfYears);
string DateHumanize_SingleMinuteFromNow();
string DateHumanize_SingleHourFromNow();
string DateHumanize_Now();
string DateHumanize_SingleMonthFromNow();
string DateHumanize_SingleSecondFromNow();
string DateHumanize_SingleYearFromNow();
string DateHumanize_SingleDayFromNow();
string TimeSpanHumanize_MultipleWeeks(int weeks);
string TimeSpanHumanize_SingleWeek();
string TimeSpanHumanize_MultipleDays(int days);
Expand Down
Loading