From edfc9aa1399c1855a5728d2c2566513e0cdde120 Mon Sep 17 00:00:00 2001 From: Mikhail Shabanov Date: Tue, 18 May 2021 17:41:36 +0200 Subject: [PATCH] feat: time interval extension method (aka pipe) --- .../Helpers/DatesHelperTests.cs | 32 +++++++++++++ .../Helpers/DateDifferenceHelper.cs | 48 +++++++++++++++++++ .../Model/TimeInterval.cs | 11 +++++ .../Pages/FindClanListItem.razor | 5 +- .../Resources/App.de.resx | 9 ++++ .../Resources/App.ru.resx | 9 ++++ 6 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 WotBlitzStatisticsPro.Blazor.Tests/Helpers/DatesHelperTests.cs create mode 100644 WotBlitzStatisticsPro.Blazor/Helpers/DateDifferenceHelper.cs create mode 100644 WotBlitzStatisticsPro.Blazor/Model/TimeInterval.cs diff --git a/WotBlitzStatisticsPro.Blazor.Tests/Helpers/DatesHelperTests.cs b/WotBlitzStatisticsPro.Blazor.Tests/Helpers/DatesHelperTests.cs new file mode 100644 index 0000000..0e9d67f --- /dev/null +++ b/WotBlitzStatisticsPro.Blazor.Tests/Helpers/DatesHelperTests.cs @@ -0,0 +1,32 @@ +using System; +using FluentAssertions; +using NUnit.Framework; +using WotBlitzStatisticsPro.Blazor.Helpers; +using WotBlitzStatisticsPro.Blazor.Model; + +namespace WotBlitzStatisticsPro.Blazor.Tests.Helpers +{ + public class DatesHelperTests + { + [SetUp] + public void Init() + { + } + + [Test] + public void ShouldCalculateYears() + { + var currentDate = new DateTime(2021, 5, 18); + + for (int i = 0; i < 10; i++) + { + var initialTime = currentDate.AddYears(-(1 + i * 3)).AddMonths(-i).AddDays(-i); + var result = DateDifferenceHelper.ParseTheDifference(currentDate - initialTime); + result.interval.Should().Be(TimeInterval.Year); + result.value.Should().Be(1 + i * 3); + } + } + + // ToDo: Write months, days, hours and minutes scenarios + } +} \ No newline at end of file diff --git a/WotBlitzStatisticsPro.Blazor/Helpers/DateDifferenceHelper.cs b/WotBlitzStatisticsPro.Blazor/Helpers/DateDifferenceHelper.cs new file mode 100644 index 0000000..3396792 --- /dev/null +++ b/WotBlitzStatisticsPro.Blazor/Helpers/DateDifferenceHelper.cs @@ -0,0 +1,48 @@ +using System; +using Microsoft.Extensions.Localization; +using WotBlitzStatisticsPro.Blazor.Model; + +namespace WotBlitzStatisticsPro.Blazor.Helpers +{ + public static class DateDifferenceHelper + { + public static string SinceTime(this int timeInLinuxTimeStamp, IStringLocalizer localizer) + { + var currentDate = DateTime.Now; + var timeToCalculate = new DateTime(1970, 1, 1) + TimeSpan.FromSeconds(timeInLinuxTimeStamp); + + var difference = ParseTheDifference(currentDate - timeToCalculate); + string ago = difference.interval switch + { + TimeInterval.Year => localizer.GetString("years ago"), + TimeInterval.Month => localizer.GetString("months ago"), + TimeInterval.Day => localizer.GetString("days ago"), + TimeInterval.Hour => localizer.GetString("hours ago"), + TimeInterval.Minute => localizer.GetString("minutes ago") + }; + + return $"{difference.value} {ago}"; + } + + public static (int value, TimeInterval interval) ParseTheDifference(TimeSpan difference) + { + // ToDo: Check hours and mins if Days is 0 + return difference.TotalDays switch + { + >= 365 => (CalculateYears(difference.TotalDays), TimeInterval.Year), + (> 30) and (< 365) => (CalculateMonths(difference.TotalDays), TimeInterval.Month), + _ => (Convert.ToInt32(difference.TotalDays), TimeInterval.Day) + }; + } + + private static int CalculateYears(double days) + { + return Convert.ToInt32(days) / 365; + } + + private static int CalculateMonths(double days) + { + return Convert.ToInt32(days) / 30; + } + } +} \ No newline at end of file diff --git a/WotBlitzStatisticsPro.Blazor/Model/TimeInterval.cs b/WotBlitzStatisticsPro.Blazor/Model/TimeInterval.cs new file mode 100644 index 0000000..e72f553 --- /dev/null +++ b/WotBlitzStatisticsPro.Blazor/Model/TimeInterval.cs @@ -0,0 +1,11 @@ +namespace WotBlitzStatisticsPro.Blazor.Model +{ + public enum TimeInterval + { + Minute, + Hour, + Day, + Month, + Year + } +} \ No newline at end of file diff --git a/WotBlitzStatisticsPro.Blazor/Pages/FindClanListItem.razor b/WotBlitzStatisticsPro.Blazor/Pages/FindClanListItem.razor index 6118be0..394b750 100644 --- a/WotBlitzStatisticsPro.Blazor/Pages/FindClanListItem.razor +++ b/WotBlitzStatisticsPro.Blazor/Pages/FindClanListItem.razor @@ -1,4 +1,5 @@ -@inject IStringLocalizer Localize +@using WotBlitzStatisticsPro.Blazor.Helpers +@inject IStringLocalizer Localize
@@ -13,7 +14,7 @@ @Clan.MembersCount
- @Clan.CreatedAt + @Clan.CreatedAt.SinceTime(Localize)
diff --git a/WotBlitzStatisticsPro.Blazor/Resources/App.de.resx b/WotBlitzStatisticsPro.Blazor/Resources/App.de.resx index e614664..d986568 100644 --- a/WotBlitzStatisticsPro.Blazor/Resources/App.de.resx +++ b/WotBlitzStatisticsPro.Blazor/Resources/App.de.resx @@ -120,6 +120,9 @@ Erstellt + + vor Tagen + Startseite @@ -129,6 +132,9 @@ Mitglieder + + vor Monaten + Suchen @@ -141,4 +147,7 @@ Willkommen bei der WOT Blitz statistics! + + vor Jahren + \ No newline at end of file diff --git a/WotBlitzStatisticsPro.Blazor/Resources/App.ru.resx b/WotBlitzStatisticsPro.Blazor/Resources/App.ru.resx index 784eb17..82b6bcb 100644 --- a/WotBlitzStatisticsPro.Blazor/Resources/App.ru.resx +++ b/WotBlitzStatisticsPro.Blazor/Resources/App.ru.resx @@ -120,6 +120,9 @@ Создан + + дней назад + На главную @@ -129,6 +132,9 @@ Участников + + месяцев назад + Поиск @@ -141,4 +147,7 @@ Добро пожаловать в WOT Blitz statistics! + + лет назад + \ No newline at end of file