summaryrefslogtreecommitdiffhomepage
path: root/gui
diff options
context:
space:
mode:
Diffstat (limited to 'gui')
-rw-r--r--gui/src/renderer/components/ExpiredAccountAddTime.tsx4
-rw-r--r--gui/src/renderer/components/RedeemVoucher.tsx5
-rw-r--r--gui/src/shared/account-expiry.ts17
-rw-r--r--gui/src/shared/date-helper.ts86
-rw-r--r--gui/src/shared/notifications/close-to-account-expiry.ts10
-rw-r--r--gui/test/unit/date-helper.spec.ts97
6 files changed, 116 insertions, 103 deletions
diff --git a/gui/src/renderer/components/ExpiredAccountAddTime.tsx b/gui/src/renderer/components/ExpiredAccountAddTime.tsx
index 2c593217b8..3b54642978 100644
--- a/gui/src/renderer/components/ExpiredAccountAddTime.tsx
+++ b/gui/src/renderer/components/ExpiredAccountAddTime.tsx
@@ -151,7 +151,9 @@ export function TimeAdded(props: ITimeAddedProps) {
}, [history, finish]);
const duration =
- props.secondsAdded !== undefined ? formatRelativeDate(props.secondsAdded * 1000, 0) : undefined;
+ props.secondsAdded !== undefined
+ ? formatRelativeDate(0, props.secondsAdded * 1000, { capitalize: true, displayMonths: true })
+ : undefined;
let newExpiry = '';
if (props.newExpiry !== undefined) {
diff --git a/gui/src/renderer/components/RedeemVoucher.tsx b/gui/src/renderer/components/RedeemVoucher.tsx
index 34cc89c4b0..7f637ab125 100644
--- a/gui/src/renderer/components/RedeemVoucher.tsx
+++ b/gui/src/renderer/components/RedeemVoucher.tsx
@@ -217,7 +217,10 @@ export function RedeemVoucherAlert(props: IRedeemVoucherAlertProps) {
const locale = useSelector((state) => state.userInterface.locale);
if (response?.type === 'success') {
- const duration = formatRelativeDate(response.secondsAdded * 1000, 0);
+ const duration = formatRelativeDate(0, response.secondsAdded * 1000, {
+ capitalize: true,
+ displayMonths: true,
+ });
const expiry = formatDate(response.newExpiry, locale);
return (
diff --git a/gui/src/shared/account-expiry.ts b/gui/src/shared/account-expiry.ts
index 85d0d78315..1b40848220 100644
--- a/gui/src/shared/account-expiry.ts
+++ b/gui/src/shared/account-expiry.ts
@@ -1,5 +1,10 @@
-import { dateByAddingComponent, DateComponent, DateType, formatTimeLeft } from './date-helper';
-import { capitalize } from './string-helpers';
+import {
+ dateByAddingComponent,
+ DateComponent,
+ DateType,
+ FormatDateOptions,
+ formatRelativeDate,
+} from './date-helper';
export function hasExpired(expiry: DateType): boolean {
return new Date(expiry).getTime() < Date.now();
@@ -18,10 +23,6 @@ export function formatDate(date: DateType, locale: string): string {
);
}
-export function formatRemainingTime(
- expiry: DateType,
- shouldCapitalizeFirstLetter?: boolean,
-): string {
- const remaining = formatTimeLeft(new Date(), expiry);
- return shouldCapitalizeFirstLetter ? capitalize(remaining) : remaining;
+export function formatRemainingTime(expiry: DateType, options?: FormatDateOptions): string {
+ return formatRelativeDate(new Date(), expiry, options);
}
diff --git a/gui/src/shared/date-helper.ts b/gui/src/shared/date-helper.ts
index 017b2048f5..be83473cfa 100644
--- a/gui/src/shared/date-helper.ts
+++ b/gui/src/shared/date-helper.ts
@@ -1,6 +1,7 @@
import { sprintf } from 'sprintf-js';
import { messages } from './gettext';
+import { capitalize } from './string-helpers';
export type DateType = Date | string | number;
@@ -72,74 +73,53 @@ export class DateDiff {
}
}
+export interface FormatDateOptions {
+ suffix?: boolean;
+ displayMonths?: boolean;
+ capitalize?: boolean;
+}
+
+// If withSuffix is true then "left" will be added at the end of the remaining time.
+// If noMonths is true then the following applies:
+// If a user has more than 2 years (730 days) left of time it should be displayed in whole years
+// rounded down If a user has less than 2 years left (e.g. 729 days) then this should be displayed
+// in days.
export function formatRelativeDate(
fromDate: DateType,
toDate: DateType,
- withSuffix = false,
+ options?: FormatDateOptions,
): string {
const diff = new DateDiff(fromDate, toDate);
const years = Math.abs(diff.years);
const months = Math.abs(diff.months);
const days = Math.abs(diff.days);
- const hours = Math.abs(diff.hours);
- const minutes = Math.abs(diff.minutes);
- if (!withSuffix) {
- if (years > 0) {
- return sprintf(messages.ngettext('1 year', '%d years', years), years);
- } else if (months >= 3) {
- return sprintf(messages.ngettext('1 month', '%d months', months), months);
+ if (isNaN(years) || isNaN(months) || isNaN(days)) {
+ return '';
+ }
+
+ let result = '';
+ if (!options?.suffix) {
+ if (options?.displayMonths ? years > 0 : days >= 730) {
+ result = sprintf(messages.ngettext('1 year', '%d years', years), years);
+ } else if (options?.displayMonths && months >= 3) {
+ result = sprintf(messages.ngettext('1 month', '%d months', months), months);
} else if (days > 0) {
- return sprintf(messages.ngettext('1 day', '%d days', days), days);
+ result = sprintf(messages.ngettext('1 day', '%d days', days), days);
} else {
- return messages.gettext('less than a day');
+ result = messages.gettext('less than a day');
}
} else if (diff.milliseconds > 0) {
- if (years > 0) {
- return sprintf(messages.ngettext('1 year left', '%d years left', years), years);
- } else if (months >= 3) {
- return sprintf(messages.ngettext('1 month left', '%d months left', months), months);
+ if (options?.displayMonths ? years > 0 : days >= 730) {
+ result = sprintf(messages.ngettext('1 year left', '%d years left', years), years);
+ } else if (options?.displayMonths && months >= 3) {
+ result = sprintf(messages.ngettext('1 month left', '%d months left', months), months);
} else if (days > 0) {
- return sprintf(messages.ngettext('1 day left', '%d days left', days), days);
+ result = sprintf(messages.ngettext('1 day left', '%d days left', days), days);
} else {
- return messages.gettext('less than a day left');
- }
- } else {
- if (years > 0) {
- return sprintf(messages.ngettext('a year ago', '%d years ago', years), years);
- } else if (months > 0) {
- return sprintf(messages.ngettext('a month ago', '%d months ago', months), months);
- } else if (days > 0) {
- return sprintf(messages.ngettext('a day ago', '%d days ago', days), days);
- } else if (hours > 0) {
- return sprintf(messages.ngettext('an hour ago', '%d hours ago', hours), hours);
- } else if (minutes > 0) {
- return sprintf(messages.ngettext('a minute ago', '%d minutes ago', minutes), minutes);
- } else {
- return messages.gettext('less than a minute ago');
+ result = messages.gettext('less than a day left');
}
}
-}
-/**
- * If a user has more than 2 years (730 days) left of time it should be displayed in whole years rounded down
- * If a user has less than 2 years left (e.g. 729 days) then this should be displayed in days.
- *
- * @param fromDate
- * @param toDate
- */
-export const formatTimeLeft = (fromDate: DateType, toDate: DateType): string => {
- const diff = new DateDiff(fromDate, toDate);
- const years = Math.abs(diff.years);
- const days = Math.abs(diff.days);
-
- if (days < 1) {
- return messages.gettext('less than a day left');
- }
-
- if (days < 730) {
- return sprintf(messages.ngettext('1 day left', '%d days left', days), days);
- }
-
- return sprintf(messages.ngettext('1 year left', '%d years left', years), years);
-};
+ return options?.capitalize ? capitalize(result) : result;
+}
diff --git a/gui/src/shared/notifications/close-to-account-expiry.ts b/gui/src/shared/notifications/close-to-account-expiry.ts
index a3f5e749ad..60feb7ec4d 100644
--- a/gui/src/shared/notifications/close-to-account-expiry.ts
+++ b/gui/src/shared/notifications/close-to-account-expiry.ts
@@ -3,7 +3,6 @@ import { sprintf } from 'sprintf-js';
import { links } from '../../config.json';
import { messages } from '../../shared/gettext';
import { closeToExpiry, formatRemainingTime } from '../account-expiry';
-import { formatRelativeDate } from '../date-helper';
import {
InAppNotification,
InAppNotificationProvider,
@@ -34,7 +33,7 @@ export class CloseToAccountExpiryNotificationProvider
'Account credit expires in %(duration)s. Buy more credit.',
),
{
- duration: formatRelativeDate(new Date(), this.context.accountExpiry),
+ duration: formatRemainingTime(this.context.accountExpiry),
},
);
@@ -54,7 +53,12 @@ export class CloseToAccountExpiryNotificationProvider
public getInAppNotification(): InAppNotification {
const subtitle = sprintf(
messages.pgettext('in-app-notifications', '%(duration)s. Buy more credit.'),
- { duration: formatRemainingTime(this.context.accountExpiry, true) },
+ {
+ duration: formatRemainingTime(this.context.accountExpiry, {
+ capitalize: true,
+ suffix: true,
+ }),
+ },
);
return {
diff --git a/gui/test/unit/date-helper.spec.ts b/gui/test/unit/date-helper.spec.ts
index 5f44c53a2d..74500ff960 100644
--- a/gui/test/unit/date-helper.spec.ts
+++ b/gui/test/unit/date-helper.spec.ts
@@ -100,66 +100,89 @@ describe('Date helper', () => {
});
it('should format positive difference as string', () => {
- const diff1 = date.formatRelativeDate('2021-01-01 13:37:10', '2021-01-01 13:37:20');
+ const diff1 = date.formatRelativeDate(
+ '2021-01-01 13:37:10',
+ '2021-01-01 13:37:20',
+ { displayMonths: true },
+ );
expect(diff1).to.equal('less than a day');
- const diff2 = date.formatRelativeDate('2021-01-01 13:37:10', '2021-01-02 13:37:20');
+ const diff2 = date.formatRelativeDate(
+ '2021-01-01 13:37:10',
+ '2021-01-02 13:37:20',
+ { displayMonths: true },
+ );
expect(diff2).to.equal('1 day');
- const diff3 = date.formatRelativeDate('2021-01-01 13:37:10', '2021-02-25 13:37:20');
+ const diff3 = date.formatRelativeDate(
+ '2021-01-01 13:37:10',
+ '2021-02-25 13:37:20',
+ { displayMonths: true },
+ );
expect(diff3).to.equal('55 days');
- const diff4 = date.formatRelativeDate('2021-01-01 13:37:10', '2021-04-25 13:37:20');
+ const diff4 = date.formatRelativeDate(
+ '2021-01-01 13:37:10',
+ '2021-04-25 13:37:20',
+ { displayMonths: true },
+ );
expect(diff4).to.equal('3 months');
- const diff5 = date.formatRelativeDate('2021-01-01 13:37:10', '2031-04-25 13:37:20');
+ const diff5 = date.formatRelativeDate(
+ '2021-01-01 13:37:10',
+ '2031-04-25 13:37:20',
+ { displayMonths: true },
+ );
expect(diff5).to.equal('10 years');
});
it('should format positive difference as string suffixed with "left"', () => {
- const diff1 = date.formatRelativeDate('2021-01-01 13:37:10', '2021-01-01 13:37:20', true);
+ const diff1 = date.formatRelativeDate(
+ '2021-01-01 13:37:10',
+ '2021-01-01 13:37:20',
+ { suffix: true, displayMonths: true },
+ );
expect(diff1).to.equal('less than a day left');
- const diff2 = date.formatRelativeDate('2021-01-01 13:37:10', '2021-01-02 13:37:20', true);
+ const diff2 = date.formatRelativeDate(
+ '2021-01-01 13:37:10',
+ '2021-01-02 13:37:20',
+ { suffix: true, displayMonths: true },
+ );
expect(diff2).to.equal('1 day left');
- const diff3 = date.formatRelativeDate('2021-01-01 13:37:10', '2021-02-25 13:37:20', true);
+ const diff3 = date.formatRelativeDate(
+ '2021-01-01 13:37:10',
+ '2021-02-25 13:37:20',
+ { suffix: true, displayMonths: true },
+ );
expect(diff3).to.equal('55 days left');
- const diff4 = date.formatRelativeDate('2021-01-01 13:37:10', '2021-04-25 13:37:20', true);
+ const diff4 = date.formatRelativeDate(
+ '2021-01-01 13:37:10',
+ '2021-04-25 13:37:20',
+ { suffix: true, displayMonths: true },
+ );
expect(diff4).to.equal('3 months left');
- const diff5 = date.formatRelativeDate('2021-01-01 13:37:10', '2031-04-25 13:37:20', true);
+ const diff5 = date.formatRelativeDate(
+ '2021-01-01 13:37:10',
+ '2031-04-25 13:37:20',
+ { suffix: true, displayMonths: true },
+ );
expect(diff5).to.equal('10 years left');
});
- it('should format negative difference as string', () => {
- const diff1 = date.formatRelativeDate('2021-01-01 13:37:20', '2021-01-01 13:37:10', true);
- expect(diff1).to.equal('less than a minute ago');
-
- const diff2 = date.formatRelativeDate('2021-01-02 13:37:20', '2021-01-01 13:37:10', true);
- expect(diff2).to.equal('a day ago');
-
- const diff3 = date.formatRelativeDate('2021-02-25 13:37:20', '2021-01-01 13:37:10', true);
- expect(diff3).to.equal('a month ago');
-
- const diff4 = date.formatRelativeDate('2021-04-25 13:37:20', '2021-01-01 13:37:10', true);
- expect(diff4).to.equal('3 months ago');
-
- const diff5 = date.formatRelativeDate('2031-04-25 13:37:20', '2021-01-01 13:37:10', true);
- expect(diff5).to.equal('10 years ago');
- });
-
it('should format time left correctly', () => {
- expect(date.formatTimeLeft('2022-09-01', '2022-09-01')).to.equal('less than a day left');
- expect(date.formatTimeLeft('2022-09-01', '2022-09-02')).to.equal('1 day left');
- expect(date.formatTimeLeft('2022-09-01', '2022-09-05')).to.equal('4 days left');
- expect(date.formatTimeLeft('2022-09-01', '2022-09-30')).to.equal('29 days left');
- expect(date.formatTimeLeft('2022-09-01', '2023-09-01')).to.equal('365 days left');
- expect(date.formatTimeLeft('2022-09-01', '2024-08-30')).to.equal('729 days left');
- expect(date.formatTimeLeft('2022-09-01', '2024-08-31')).to.equal('2 years left');
- expect(date.formatTimeLeft('2022-09-01', '2024-09-05')).to.equal('2 years left');
- expect(date.formatTimeLeft('2022-09-01', '2025-08-31')).to.equal('2 years left');
- expect(date.formatTimeLeft('2022-09-01', '2025-09-01')).to.equal('3 years left');
+ expect(date.formatRelativeDate('2022-09-01', '2022-09-01')).to.equal('less than a day');
+ expect(date.formatRelativeDate('2022-09-01', '2022-09-02')).to.equal('1 day');
+ expect(date.formatRelativeDate('2022-09-01', '2022-09-05')).to.equal('4 days');
+ expect(date.formatRelativeDate('2022-09-01', '2022-09-30')).to.equal('29 days');
+ expect(date.formatRelativeDate('2022-09-01', '2023-09-01')).to.equal('365 days');
+ expect(date.formatRelativeDate('2022-09-01', '2024-08-30')).to.equal('729 days');
+ expect(date.formatRelativeDate('2022-09-01', '2024-08-31')).to.equal('2 years');
+ expect(date.formatRelativeDate('2022-09-01', '2024-09-05')).to.equal('2 years');
+ expect(date.formatRelativeDate('2022-09-01', '2025-08-31')).to.equal('2 years');
+ expect(date.formatRelativeDate('2022-09-01', '2025-09-01')).to.equal('3 years');
});
});