Skip to content

Commit

Permalink
Merge pull request #967 from iskcal/iss941
Browse files Browse the repository at this point in the history
Fix issue941
  • Loading branch information
clairernovotny authored Aug 31, 2020
2 parents 67423ac + d9d7116 commit 992ebf6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
6 changes: 5 additions & 1 deletion src/Humanizer.Tests.Shared/DateHumanize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ private static void VerifyWithDate(string expectedString, TimeSpan deltaFromBase
{
Assert.Equal(expectedString, baseDateUtc.Add(deltaFromBase).Humanize(utcDate: true, dateToCompareAgainst: baseDateUtc, culture: culture));
Assert.Equal(expectedString, baseDate.Add(deltaFromBase).Humanize(false, baseDate, culture: culture));

// Compared with default utcDate
Assert.Equal(expectedString, baseDateUtc.Add(deltaFromBase).Humanize(utcDate: null, dateToCompareAgainst: baseDateUtc, culture: culture));
Assert.Equal(expectedString, baseDate.Add(deltaFromBase).Humanize(null, baseDate, culture: culture));
}

public static void Verify(string expectedString, int unit, TimeUnit timeUnit, Tense tense, double? precision = null, CultureInfo culture = null, DateTime? baseDate = null, DateTime? baseDateUtc = null)
Expand Down Expand Up @@ -93,4 +97,4 @@ public static void Verify(string expectedString, int unit, TimeUnit timeUnit, Te
}
}
}
}
}
19 changes: 8 additions & 11 deletions src/Humanizer/DateHumanizeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,15 @@ public static class DateHumanizeExtensions
/// Turns the current or provided date into a human readable sentence
/// </summary>
/// <param name="input">The date to be humanized</param>
/// <param name="utcDate">Boolean value indicating whether the date is in UTC or local</param>
/// <param name="utcDate">Nullable boolean value indicating whether the date is in UTC or local. If null, current date is used with the same DateTimeKind of input</param>
/// <param name="dateToCompareAgainst">Date to compare the input against. If null, current date is used as base</param>
/// <param name="culture">Culture to use. If null, current thread's UI culture is used.</param>
/// <returns>distance of time in words</returns>
public static string Humanize(this DateTime input, bool utcDate = true, DateTime? dateToCompareAgainst = null, CultureInfo culture = null)
public static string Humanize(this DateTime input, bool? utcDate = null, DateTime? dateToCompareAgainst = null, CultureInfo culture = null)
{
var comparisonBase = dateToCompareAgainst ?? DateTime.UtcNow;

if (!utcDate)
{
comparisonBase = comparisonBase.ToLocalTime();
}
var comparisonBase = dateToCompareAgainst.HasValue ? dateToCompareAgainst.Value : DateTime.UtcNow;
utcDate ??= input.Kind != DateTimeKind.Local;
comparisonBase = utcDate.Value ? comparisonBase.ToUniversalTime() : comparisonBase.ToLocalTime();

return Configurator.DateTimeHumanizeStrategy.Humanize(input, comparisonBase, culture);
}
Expand All @@ -33,11 +30,11 @@ public static string Humanize(this DateTime input, bool utcDate = true, DateTime
/// Turns the current or provided date into a human readable sentence, overload for the nullable DateTime, returning 'never' in case null
/// </summary>
/// <param name="input">The date to be humanized</param>
/// <param name="utcDate">Boolean value indicating whether the date is in UTC or local</param>
/// <param name="utcDate">Nullable boolean value indicating whether the date is in UTC or local. If null, current date is used with the same DateTimeKind of input</param>
/// <param name="dateToCompareAgainst">Date to compare the input against. If null, current date is used as base</param>
/// <param name="culture">Culture to use. If null, current thread's UI culture is used.</param>
/// <returns>distance of time in words</returns>
public static string Humanize(this DateTime? input, bool utcDate = true, DateTime? dateToCompareAgainst = null, CultureInfo culture = null)
public static string Humanize(this DateTime? input, bool? utcDate = null, DateTime? dateToCompareAgainst = null, CultureInfo culture = null)
{
if (input.HasValue)
{
Expand Down Expand Up @@ -82,4 +79,4 @@ public static string Humanize(this DateTimeOffset? input, DateTimeOffset? dateTo
}
}
}
}
}

0 comments on commit 992ebf6

Please sign in to comment.