From 97292830a5e68aecccfa133bd9ecca88b116fce6 Mon Sep 17 00:00:00 2001 From: iskcal Date: Mon, 24 Aug 2020 01:12:00 +0800 Subject: [PATCH] Compare two DateTime object with the same DateTimeKind --- src/Humanizer.Tests.Shared/DateHumanize.cs | 6 +++++- src/Humanizer/DateHumanizeExtensions.cs | 19 ++++++++----------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/Humanizer.Tests.Shared/DateHumanize.cs b/src/Humanizer.Tests.Shared/DateHumanize.cs index edfd67c1a..ce6b6c796 100644 --- a/src/Humanizer.Tests.Shared/DateHumanize.cs +++ b/src/Humanizer.Tests.Shared/DateHumanize.cs @@ -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) @@ -93,4 +97,4 @@ public static void Verify(string expectedString, int unit, TimeUnit timeUnit, Te } } } -} \ No newline at end of file +} diff --git a/src/Humanizer/DateHumanizeExtensions.cs b/src/Humanizer/DateHumanizeExtensions.cs index 7047b2f69..86c6493ac 100644 --- a/src/Humanizer/DateHumanizeExtensions.cs +++ b/src/Humanizer/DateHumanizeExtensions.cs @@ -13,18 +13,15 @@ public static class DateHumanizeExtensions /// Turns the current or provided date into a human readable sentence /// /// The date to be humanized - /// Boolean value indicating whether the date is in UTC or local + /// Nullable boolean value indicating whether the date is in UTC or local. If null, current date is used with the same DateTimeKind of input /// Date to compare the input against. If null, current date is used as base /// Culture to use. If null, current thread's UI culture is used. /// distance of time in words - 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); } @@ -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 /// /// The date to be humanized - /// Boolean value indicating whether the date is in UTC or local + /// Nullable boolean value indicating whether the date is in UTC or local. If null, current date is used with the same DateTimeKind of input /// Date to compare the input against. If null, current date is used as base /// Culture to use. If null, current thread's UI culture is used. /// distance of time in words - 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) { @@ -82,4 +79,4 @@ public static string Humanize(this DateTimeOffset? input, DateTimeOffset? dateTo } } } -} \ No newline at end of file +}