From bc06114023787837a0895d829d8a389942960d47 Mon Sep 17 00:00:00 2001 From: rockstardev Date: Sat, 26 May 2018 09:32:20 -0500 Subject: [PATCH 1/4] Support for localizing datetimes base on browser's timezone --- BTCPayServer/Controllers/InvoiceController.UI.cs | 2 +- BTCPayServer/Models/InvoicingModels/InvoicesModel.cs | 5 +---- BTCPayServer/Views/Invoice/ListInvoices.cshtml | 2 +- BTCPayServer/Views/ViewsRazor.cs | 8 ++++++++ BTCPayServer/bundleconfig.json | 3 ++- BTCPayServer/wwwroot/main/site.js | 9 +++++++++ 6 files changed, 22 insertions(+), 7 deletions(-) create mode 100644 BTCPayServer/wwwroot/main/site.js diff --git a/BTCPayServer/Controllers/InvoiceController.UI.cs b/BTCPayServer/Controllers/InvoiceController.UI.cs index 4b32fc9f5..0b04d6a36 100644 --- a/BTCPayServer/Controllers/InvoiceController.UI.cs +++ b/BTCPayServer/Controllers/InvoiceController.UI.cs @@ -425,7 +425,7 @@ namespace BTCPayServer.Controllers { Status = invoice.Status + (invoice.ExceptionStatus == null ? string.Empty : $" ({invoice.ExceptionStatus})"), ShowCheckout = invoice.Status == "new", - Date = (DateTimeOffset.UtcNow - invoice.InvoiceTime).Prettify() + " ago", + Date = invoice.InvoiceTime, InvoiceId = invoice.Id, OrderId = invoice.OrderId ?? string.Empty, RedirectUrl = invoice.RedirectURL ?? string.Empty, diff --git a/BTCPayServer/Models/InvoicingModels/InvoicesModel.cs b/BTCPayServer/Models/InvoicingModels/InvoicesModel.cs index 63385532d..5dfdd5260 100644 --- a/BTCPayServer/Models/InvoicingModels/InvoicesModel.cs +++ b/BTCPayServer/Models/InvoicingModels/InvoicesModel.cs @@ -33,10 +33,7 @@ namespace BTCPayServer.Models.InvoicingModels public class InvoiceModel { - public string Date - { - get; set; - } + public DateTimeOffset Date { get; set; } public string OrderId { get; set; } public string RedirectUrl { get; set; } diff --git a/BTCPayServer/Views/Invoice/ListInvoices.cshtml b/BTCPayServer/Views/Invoice/ListInvoices.cshtml index d6e6915f8..4d90ef99f 100644 --- a/BTCPayServer/Views/Invoice/ListInvoices.cshtml +++ b/BTCPayServer/Views/Invoice/ListInvoices.cshtml @@ -66,7 +66,7 @@ @foreach(var invoice in Model.Invoices) { - @invoice.Date + @invoice.Date.BrowserDate() @if(invoice.RedirectUrl != string.Empty) { diff --git a/BTCPayServer/Views/ViewsRazor.cs b/BTCPayServer/Views/ViewsRazor.cs index 76480739e..2a83b810e 100644 --- a/BTCPayServer/Views/ViewsRazor.cs +++ b/BTCPayServer/Views/ViewsRazor.cs @@ -1,7 +1,9 @@ using System; using System.Collections.Generic; +using System.Globalization; using System.Linq; using System.Threading.Tasks; +using Microsoft.AspNetCore.Html; using Microsoft.AspNetCore.Mvc.ViewFeatures; namespace BTCPayServer.Views @@ -22,5 +24,11 @@ namespace BTCPayServer.Views var activePage = (T)viewData[ACTIVE_PAGE_KEY]; return page.Equals(activePage) ? "active" : null; } + + public static HtmlString BrowserDate(this DateTimeOffset date) + { + var hello = date.ToString("o", CultureInfo.InvariantCulture); + return new HtmlString($"{hello}"); + } } } diff --git a/BTCPayServer/bundleconfig.json b/BTCPayServer/bundleconfig.json index 9d7b0ac6c..4e89d7ace 100644 --- a/BTCPayServer/bundleconfig.json +++ b/BTCPayServer/bundleconfig.json @@ -18,7 +18,8 @@ "wwwroot/vendor/jquery-easing/jquery.easing.js", "wwwroot/vendor/scrollreveal/scrollreveal.min.js", "wwwroot/vendor/magnific-popup/jquery.magnific-popup.js", - "wwwroot/vendor/bootstrap4-creativestart/*.js" + "wwwroot/vendor/bootstrap4-creativestart/*.js", + "wwwroot/main/**/*.js" ] }, { diff --git a/BTCPayServer/wwwroot/main/site.js b/BTCPayServer/wwwroot/main/site.js new file mode 100644 index 000000000..8a182c1ed --- /dev/null +++ b/BTCPayServer/wwwroot/main/site.js @@ -0,0 +1,9 @@ +$(function () { + $(".localizeDate").each(function (index) { + var serverDate = $(this).text(); + var localDate = new Date(serverDate); + + var dateString = localDate.toLocaleDateString() + " " + localDate.toLocaleTimeString(); + $(this).text(dateString); + }); +}); From 5c4200b03665bda8801675ac530515cf03f2fa61 Mon Sep 17 00:00:00 2001 From: rockstardev Date: Sat, 26 May 2018 09:35:42 -0500 Subject: [PATCH 2/4] Updating Invoice details to show local date time --- BTCPayServer/Views/Invoice/Invoice.cshtml | 8 ++++---- BTCPayServer/Views/Invoice/ListInvoices.cshtml | 2 +- BTCPayServer/Views/ViewsRazor.cs | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/BTCPayServer/Views/Invoice/Invoice.cshtml b/BTCPayServer/Views/Invoice/Invoice.cshtml index 343d3c613..cb3cc5699 100644 --- a/BTCPayServer/Views/Invoice/Invoice.cshtml +++ b/BTCPayServer/Views/Invoice/Invoice.cshtml @@ -53,15 +53,15 @@ Created date - @Model.CreatedDate + @Model.CreatedDate.ToBrowserDate() Expiration date - @Model.ExpirationDate + @Model.ExpirationDate.ToBrowserDate() Monitoring date - @Model.MonitoringDate + @Model.MonitoringDate.ToBrowserDate() Transaction speed @@ -289,7 +289,7 @@ @foreach(var evt in Model.Events) { - @evt.Timestamp + @evt.Timestamp.ToBrowserDate() @evt.Message } diff --git a/BTCPayServer/Views/Invoice/ListInvoices.cshtml b/BTCPayServer/Views/Invoice/ListInvoices.cshtml index 4d90ef99f..e7bc473d9 100644 --- a/BTCPayServer/Views/Invoice/ListInvoices.cshtml +++ b/BTCPayServer/Views/Invoice/ListInvoices.cshtml @@ -66,7 +66,7 @@ @foreach(var invoice in Model.Invoices) { - @invoice.Date.BrowserDate() + @invoice.Date.ToBrowserDate() @if(invoice.RedirectUrl != string.Empty) { diff --git a/BTCPayServer/Views/ViewsRazor.cs b/BTCPayServer/Views/ViewsRazor.cs index 2a83b810e..6affde624 100644 --- a/BTCPayServer/Views/ViewsRazor.cs +++ b/BTCPayServer/Views/ViewsRazor.cs @@ -25,7 +25,7 @@ namespace BTCPayServer.Views return page.Equals(activePage) ? "active" : null; } - public static HtmlString BrowserDate(this DateTimeOffset date) + public static HtmlString ToBrowserDate(this DateTimeOffset date) { var hello = date.ToString("o", CultureInfo.InvariantCulture); return new HtmlString($"{hello}"); From 68f2cba60de635093cfdf27bdf589f6772541bcc Mon Sep 17 00:00:00 2001 From: rockstardev Date: Sat, 26 May 2018 09:42:55 -0500 Subject: [PATCH 3/4] Generalizing TimeSpan to Time Ago, reverting invoice list to ago --- BTCPayServer/Extensions.cs | 22 --------------- .../Views/Invoice/ListInvoices.cshtml | 2 +- BTCPayServer/Views/Server/Rates.cshtml | 4 +-- BTCPayServer/Views/ViewsRazor.cs | 28 +++++++++++++++++++ 4 files changed, 31 insertions(+), 25 deletions(-) diff --git a/BTCPayServer/Extensions.cs b/BTCPayServer/Extensions.cs index e6e7e3520..151042737 100644 --- a/BTCPayServer/Extensions.cs +++ b/BTCPayServer/Extensions.cs @@ -36,28 +36,6 @@ namespace BTCPayServer { public static class Extensions { - public static string Prettify(this TimeSpan timeSpan) - { - if (timeSpan.TotalMinutes < 1) - { - return $"{(int)timeSpan.TotalSeconds} second{Plural((int)timeSpan.TotalSeconds)}"; - } - if (timeSpan.TotalHours < 1) - { - return $"{(int)timeSpan.TotalMinutes} minute{Plural((int)timeSpan.TotalMinutes)}"; - } - if (timeSpan.Days < 1) - { - return $"{(int)timeSpan.TotalHours} hour{Plural((int)timeSpan.TotalHours)}"; - } - return $"{(int)timeSpan.TotalDays} day{Plural((int)timeSpan.TotalDays)}"; - } - - private static string Plural(int totalDays) - { - return totalDays > 1 ? "s" : string.Empty; - } - public static string PrettyPrint(this TimeSpan expiration) { StringBuilder builder = new StringBuilder(); diff --git a/BTCPayServer/Views/Invoice/ListInvoices.cshtml b/BTCPayServer/Views/Invoice/ListInvoices.cshtml index e7bc473d9..724da6feb 100644 --- a/BTCPayServer/Views/Invoice/ListInvoices.cshtml +++ b/BTCPayServer/Views/Invoice/ListInvoices.cshtml @@ -66,7 +66,7 @@ @foreach(var invoice in Model.Invoices) { - @invoice.Date.ToBrowserDate() + @invoice.Date.ToBrowserAgo() @if(invoice.RedirectUrl != string.Empty) { diff --git a/BTCPayServer/Views/Server/Rates.cshtml b/BTCPayServer/Views/Server/Rates.cshtml index bbf407366..1dcf9bea6 100644 --- a/BTCPayServer/Views/Server/Rates.cshtml +++ b/BTCPayServer/Views/Server/Rates.cshtml @@ -40,7 +40,7 @@ - + @@ -48,7 +48,7 @@ - +
Quota period@Model.RateLimits.TotalPeriod.Prettify()@Model.RateLimits.TotalPeriod.TimeString()
Requests quota
Quota reset in@Model.RateLimits.CounterReset.Prettify()@Model.RateLimits.CounterReset.TimeString()
} diff --git a/BTCPayServer/Views/ViewsRazor.cs b/BTCPayServer/Views/ViewsRazor.cs index 6affde624..fef222f33 100644 --- a/BTCPayServer/Views/ViewsRazor.cs +++ b/BTCPayServer/Views/ViewsRazor.cs @@ -30,5 +30,33 @@ namespace BTCPayServer.Views var hello = date.ToString("o", CultureInfo.InvariantCulture); return new HtmlString($"{hello}"); } + + public static string ToBrowserAgo(this DateTimeOffset date) + { + var formatted = (DateTimeOffset.UtcNow - date).TimeString() + " ago"; + return formatted; + } + + public static string TimeString(this TimeSpan timeSpan) + { + if (timeSpan.TotalMinutes < 1) + { + return $"{(int)timeSpan.TotalSeconds} second{Plural((int)timeSpan.TotalSeconds)}"; + } + if (timeSpan.TotalHours < 1) + { + return $"{(int)timeSpan.TotalMinutes} minute{Plural((int)timeSpan.TotalMinutes)}"; + } + if (timeSpan.Days < 1) + { + return $"{(int)timeSpan.TotalHours} hour{Plural((int)timeSpan.TotalHours)}"; + } + return $"{(int)timeSpan.TotalDays} day{Plural((int)timeSpan.TotalDays)}"; + } + + private static string Plural(int totalDays) + { + return totalDays > 1 ? "s" : string.Empty; + } } } From 8d95b9fa049f1dc4bbcb26ac7bf71cb01812d9a6 Mon Sep 17 00:00:00 2001 From: rockstardev Date: Mon, 28 May 2018 09:36:49 -0500 Subject: [PATCH 4/4] Renaming method to better communicate function --- BTCPayServer/Views/Invoice/ListInvoices.cshtml | 2 +- BTCPayServer/Views/ViewsRazor.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/BTCPayServer/Views/Invoice/ListInvoices.cshtml b/BTCPayServer/Views/Invoice/ListInvoices.cshtml index 724da6feb..22e40ae10 100644 --- a/BTCPayServer/Views/Invoice/ListInvoices.cshtml +++ b/BTCPayServer/Views/Invoice/ListInvoices.cshtml @@ -66,7 +66,7 @@ @foreach(var invoice in Model.Invoices) { - @invoice.Date.ToBrowserAgo() + @invoice.Date.ToTimeAgo() @if(invoice.RedirectUrl != string.Empty) { diff --git a/BTCPayServer/Views/ViewsRazor.cs b/BTCPayServer/Views/ViewsRazor.cs index fef222f33..fe49b3379 100644 --- a/BTCPayServer/Views/ViewsRazor.cs +++ b/BTCPayServer/Views/ViewsRazor.cs @@ -31,7 +31,7 @@ namespace BTCPayServer.Views return new HtmlString($"{hello}"); } - public static string ToBrowserAgo(this DateTimeOffset date) + public static string ToTimeAgo(this DateTimeOffset date) { var formatted = (DateTimeOffset.UtcNow - date).TimeString() + " ago"; return formatted;