mirror of
https://codeberg.org/davrot/forgejo.git
synced 2025-06-09 21:00:04 +02:00
Replace the 'relative-time' element scripting with custom, translatable rewrite (#6154)
This is my take to fix #6078 Should also resolve #6111 As far as I can tell, Forgejo uses only a subset of the relative-time functionality, and as far as I can see, this subset can be implemented using browser built-in date conversion and arithmetic. So I wrote a JavaScript to format the relative-time element accordingly, and a Go binding to generate the translated elements. This is my first time writing Go code, and my first time coding for a large-scale server application, so please tell me if I'm doing something wrong, or if the whole approach is not acceptable. --- Screenshot: Localized times in Low German  Screenshot: The same with Forgejo in English  --- ## Checklist The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org). ### Tests - I added test coverage for Go changes... - [x] in their respective `*_test.go` for unit tests. - [ ] in the `tests/integration` directory if it involves interactions with a live Forgejo server. - I added test coverage for JavaScript changes... - [x] in `web_src/js/*.test.js` if it can be unit tested. - [ ] in `tests/e2e/*.test.e2e.js` if it requires interactions with a live Forgejo server (see also the [developer guide for JavaScript testing](https://codeberg.org/forgejo/forgejo/src/branch/forgejo/tests/e2e/README.md#end-to-end-tests)). ### Documentation - [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change. - [x] I did not document these changes and I do not expect someone else to do it. ### Release notes - [x] I do not want this change to show in the release notes. - [ ] I want the title to show in the release notes with a link to this pull request. - [ ] I want the content of the `release-notes/<pull request number>.md` to be be used for the release notes instead of the title. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/6154 Reviewed-by: 0ko <0ko@noreply.codeberg.org> Reviewed-by: Michael Kriese <michael.kriese@gmx.de> Co-authored-by: Benedikt Straub <benedikt-straub@web.de> Co-committed-by: Benedikt Straub <benedikt-straub@web.de>
This commit is contained in:
parent
37d566bdb0
commit
cf03286b5b
19 changed files with 698 additions and 35 deletions
75
web_src/js/webcomponents/i18n.js
Normal file
75
web_src/js/webcomponents/i18n.js
Normal file
|
@ -0,0 +1,75 @@
|
|||
const {pageData} = window.config;
|
||||
|
||||
/**
|
||||
* A list of plural rules for all languages.
|
||||
* `plural_rules.go` defines the index for each of the 14 known plural rules.
|
||||
*
|
||||
* `pageData.PLURAL_RULE_LANG` is the index of the plural rule for the current language.
|
||||
* `pageData.PLURAL_RULE_FALLBACK` is the index of the plural rule for the default language,
|
||||
* to be used when a string is not translated in the current language.
|
||||
*
|
||||
* Each plural rule is a function that maps an amount `n` to the appropriate plural form index.
|
||||
* Which index means which rule is specific for each language and also defined in `plural_rules.go`.
|
||||
* The actual strings are in `pageData.PLURALSTRINGS_LANG` and `pageData.PLURALSTRINGS_FALLBACK`
|
||||
* respectively, which is an array indexed by the plural form index.
|
||||
*
|
||||
* Links to the language plural rule and form definitions:
|
||||
* https://codeberg.org/forgejo/forgejo/src/branch/forgejo/modules/translation/plural_rules.go
|
||||
* https://www.unicode.org/cldr/charts/46/supplemental/language_plural_rules.html
|
||||
* https://translate.codeberg.org/languages/$LANGUAGE_CODE/#information
|
||||
* https://github.com/WeblateOrg/language-data/blob/main/languages.csv
|
||||
*/
|
||||
const PLURAL_RULES = [
|
||||
// [ 0] Common 2-form, e.g. English, German
|
||||
function (n) { return n !== 1 ? 1 : 0 },
|
||||
|
||||
// [ 1] Bengali 2-form
|
||||
function (n) { return n > 1 ? 1 : 0 },
|
||||
|
||||
// [ 2] Icelandic 2-form
|
||||
function (n) { return n % 10 !== 1 || n % 100 === 11 ? 1 : 0 },
|
||||
|
||||
// [ 3] Filipino 2-form
|
||||
function (n) { return n !== 1 && n !== 2 && n !== 3 && (n % 10 === 4 || n % 10 === 6 || n % 10 === 9) ? 1 : 0 },
|
||||
|
||||
// [ 4] One form
|
||||
function (_) { return 0 },
|
||||
|
||||
// [ 5] Czech 3-form
|
||||
function (n) { return (n === 1) ? 0 : (n >= 2 && n <= 4) ? 1 : 2 },
|
||||
|
||||
// [ 6] Russian 3-form
|
||||
function (n) { return n % 10 === 1 && n % 100 !== 11 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2 },
|
||||
|
||||
// [ 7] Polish 3-form
|
||||
function (n) { return n === 1 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2 },
|
||||
|
||||
// [ 8] Latvian 3-form
|
||||
function (n) { return (n % 10 === 0 || n % 100 >= 11 && n % 100 <= 19) ? 0 : ((n % 10 === 1 && n % 100 !== 11) ? 1 : 2) },
|
||||
|
||||
// [ 9] Lithunian 3-form
|
||||
function (n) { return (n % 10 === 1 && (n % 100 < 11 || n % 100 > 19)) ? 0 : ((n % 10 >= 2 && n % 10 <= 9 && (n % 100 < 11 || n % 100 > 19)) ? 1 : 2) },
|
||||
|
||||
// [10] French 3-form
|
||||
function (n) { return (n === 0 || n === 1) ? 0 : ((n !== 0 && n % 1000000 === 0) ? 1 : 2) },
|
||||
|
||||
// [11] Catalan 3-form
|
||||
function (n) { return (n === 1) ? 0 : ((n !== 0 && n % 1000000 === 0) ? 1 : 2) },
|
||||
|
||||
// [12] Slovenian 4-form
|
||||
function (n) { return n % 100 === 1 ? 0 : n % 100 === 2 ? 1 : n % 100 === 3 || n % 100 === 4 ? 2 : 3 },
|
||||
|
||||
// [13] Arabic 6-form
|
||||
function (n) { return n === 0 ? 0 : n === 1 ? 1 : n === 2 ? 2 : n % 100 >= 3 && n % 100 <= 10 ? 3 : n % 100 >= 11 ? 4 : 5 },
|
||||
];
|
||||
|
||||
/**
|
||||
* Look up the correct localized plural form for amount `n` for the string with the translation key `key`.
|
||||
* If the current language does not contain a translation for this key, returns the text in the default language,
|
||||
* or `null` if `suppress_fallback` is set to `true`.
|
||||
*/
|
||||
export function GetPluralizedString(key, n, suppress_fallback) {
|
||||
const result = pageData.PLURALSTRINGS_LANG[key]?.[PLURAL_RULES[pageData.PLURAL_RULE_LANG](n)];
|
||||
if (result || suppress_fallback) return result;
|
||||
return pageData.PLURALSTRINGS_FALLBACK[key][PLURAL_RULES[pageData.PLURAL_RULE_FALLBACK](n)];
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
import './polyfills.js';
|
||||
import '@github/relative-time-element';
|
||||
import './i18n.js';
|
||||
import './relative-time.js';
|
||||
import './origin-url.js';
|
||||
import './overflow-menu.js';
|
||||
import './absolute-date.js';
|
||||
|
|
144
web_src/js/webcomponents/relative-time.js
Normal file
144
web_src/js/webcomponents/relative-time.js
Normal file
|
@ -0,0 +1,144 @@
|
|||
import {GetPluralizedString} from './i18n.js';
|
||||
import dayjs from 'dayjs';
|
||||
const {pageData} = window.config;
|
||||
|
||||
export const HALF_MINUTE = 30 * 1000;
|
||||
export const ONE_MINUTE = 60 * 1000;
|
||||
export const ONE_HOUR = 60 * ONE_MINUTE;
|
||||
export const ONE_DAY = 24 * ONE_HOUR;
|
||||
|
||||
const ABSOLUTE_DATETIME_FORMAT = new Intl.DateTimeFormat(navigator.language, {
|
||||
year: 'numeric',
|
||||
month: 'short',
|
||||
day: 'numeric',
|
||||
hour: '2-digit',
|
||||
minute: '2-digit',
|
||||
timeZoneName: 'short',
|
||||
});
|
||||
const FALLBACK_DATETIME_FORMAT = new Intl.RelativeTimeFormat(navigator.language, {style: 'long'});
|
||||
|
||||
function GetPluralizedStringOrFallback(key, n, unit) {
|
||||
const translation = GetPluralizedString(key, n, true);
|
||||
if (translation) return translation.replace('%d', n);
|
||||
return FALLBACK_DATETIME_FORMAT.format(-n, unit);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the displayed text of the given relative-time DOM element with its
|
||||
* human-readable, localized relative time string.
|
||||
* Returns the recommended interval in milliseconds until the object should be updated again,
|
||||
* or null if the object is invalid.
|
||||
*/
|
||||
export function DoUpdateRelativeTime(object, now) {
|
||||
const absoluteTime = object.getAttribute('datetime');
|
||||
if (!absoluteTime) {
|
||||
return null; // Object does not contain a datetime.
|
||||
}
|
||||
|
||||
if (!now) now = Date.now();
|
||||
|
||||
const nowJS = dayjs(now);
|
||||
const thenJS = dayjs(absoluteTime);
|
||||
|
||||
object.setAttribute('data-tooltip-content', ABSOLUTE_DATETIME_FORMAT.format(thenJS.toDate()));
|
||||
|
||||
if (nowJS.isBefore(thenJS)) {
|
||||
// Datetime is in the future.
|
||||
object.textContent = pageData.DATETIMESTRINGS.FUTURE;
|
||||
return -Math.floor(nowJS.diff(thenJS, 'millisecond'));
|
||||
}
|
||||
|
||||
const years = Math.floor(nowJS.diff(thenJS, 'year'));
|
||||
if (years >= 1) {
|
||||
// Datetime is at least one year ago.
|
||||
if (years === 1 && pageData.DATETIMESTRINGS['relativetime.1year']) {
|
||||
// Datetime is one year ago.
|
||||
object.textContent = pageData.DATETIMESTRINGS['relativetime.1year'];
|
||||
} else if (years === 2 && pageData.DATETIMESTRINGS['relativetime.2years']) {
|
||||
// Datetime is two year ago.
|
||||
object.textContent = pageData.DATETIMESTRINGS['relativetime.2years'];
|
||||
} else {
|
||||
// Datetime is more than a year ago.
|
||||
object.textContent = GetPluralizedStringOrFallback('relativetime.years', years, 'year');
|
||||
}
|
||||
return ONE_DAY;
|
||||
}
|
||||
|
||||
const months = Math.floor(nowJS.diff(thenJS, 'month'));
|
||||
if (months >= 1) {
|
||||
// Datetime is at least one month but less than a year ago.
|
||||
if (months === 1 && pageData.DATETIMESTRINGS['relativetime.1month']) {
|
||||
// Datetime is one month ago.
|
||||
object.textContent = pageData.DATETIMESTRINGS['relativetime.1month'];
|
||||
} else if (months === 2 && pageData.DATETIMESTRINGS['relativetime.2months']) {
|
||||
// Datetime is two months ago.
|
||||
object.textContent = pageData.DATETIMESTRINGS['relativetime.2months'];
|
||||
} else {
|
||||
// Datetime is several months ago (but less than a year).
|
||||
object.textContent = GetPluralizedStringOrFallback('relativetime.months', months, 'month');
|
||||
}
|
||||
return ONE_DAY;
|
||||
}
|
||||
|
||||
const weeks = Math.floor(nowJS.diff(thenJS, 'week'));
|
||||
if (weeks >= 1) {
|
||||
// Datetime is at least one week but less than a month ago.
|
||||
if (weeks === 1 && pageData.DATETIMESTRINGS['relativetime.1week']) {
|
||||
// Datetime is one week ago.
|
||||
object.textContent = pageData.DATETIMESTRINGS['relativetime.1week'];
|
||||
} else if (weeks === 2 && pageData.DATETIMESTRINGS['relativetime.2weeks']) {
|
||||
// Datetime is two week ago.
|
||||
object.textContent = pageData.DATETIMESTRINGS['relativetime.2weeks'];
|
||||
} else {
|
||||
// Datetime is several weeks ago (but less than a month).
|
||||
object.textContent = GetPluralizedStringOrFallback('relativetime.weeks', weeks, 'week');
|
||||
}
|
||||
return ONE_DAY;
|
||||
}
|
||||
|
||||
const days = Math.floor(nowJS.diff(thenJS, 'day'));
|
||||
if (days >= 1) {
|
||||
if (days === 1 && pageData.DATETIMESTRINGS['relativetime.1day']) {
|
||||
// Datetime is one day ago.
|
||||
object.textContent = pageData.DATETIMESTRINGS['relativetime.1day'];
|
||||
} else if (days === 2 && pageData.DATETIMESTRINGS['relativetime.2days']) {
|
||||
// Datetime is two days ago.
|
||||
object.textContent = pageData.DATETIMESTRINGS['relativetime.2days'];
|
||||
} else {
|
||||
// Datetime is several days but less than a week ago.
|
||||
object.textContent = GetPluralizedStringOrFallback('relativetime.days', days, 'day');
|
||||
}
|
||||
return ONE_DAY;
|
||||
}
|
||||
|
||||
const hours = Math.floor(nowJS.diff(thenJS, 'hour'));
|
||||
if (hours >= 1) {
|
||||
// Datetime is one or more hours but less than a day ago.
|
||||
object.textContent = GetPluralizedStringOrFallback('relativetime.hours', hours, 'hour');
|
||||
return ONE_HOUR;
|
||||
}
|
||||
|
||||
const minutes = Math.floor(nowJS.diff(thenJS, 'minute'));
|
||||
if (minutes >= 1) {
|
||||
// Datetime is one or more minutes but less than an hour ago.
|
||||
object.textContent = GetPluralizedStringOrFallback('relativetime.mins', minutes, 'minute');
|
||||
return ONE_MINUTE;
|
||||
}
|
||||
|
||||
// Datetime is very recent.
|
||||
object.textContent = pageData.DATETIMESTRINGS.NOW;
|
||||
return HALF_MINUTE;
|
||||
}
|
||||
|
||||
/** Update the displayed text of one relative-time DOM element with its human-readable, localized relative time string. */
|
||||
function UpdateRelativeTime(object) {
|
||||
const next = DoUpdateRelativeTime(object);
|
||||
if (next !== null) setTimeout(() => { UpdateRelativeTime(object) }, next);
|
||||
}
|
||||
|
||||
/** Update the displayed text of all relative-time DOM elements with their respective human-readable, localized relative time string. */
|
||||
function UpdateAllRelativeTimes() {
|
||||
for (const object of document.querySelectorAll('relative-time')) UpdateRelativeTime(object);
|
||||
}
|
||||
|
||||
document.addEventListener('DOMContentLoaded', UpdateAllRelativeTimes);
|
212
web_src/js/webcomponents/relative-time.test.js
Normal file
212
web_src/js/webcomponents/relative-time.test.js
Normal file
|
@ -0,0 +1,212 @@
|
|||
import {DoUpdateRelativeTime, HALF_MINUTE, ONE_MINUTE, ONE_HOUR, ONE_DAY} from './relative-time.js';
|
||||
|
||||
test('CalculateRelativeTimes', () => {
|
||||
window.config.pageData.PLURAL_RULE_LANG = 0;
|
||||
window.config.pageData.DATETIMESTRINGS = {
|
||||
'FUTURE': 'in future',
|
||||
'NOW': 'now',
|
||||
'relativetime.1day': 'yesterday',
|
||||
'relativetime.1week': 'last week',
|
||||
'relativetime.1month': 'last month',
|
||||
'relativetime.1year': 'last year',
|
||||
'relativetime.2days': 'two days ago',
|
||||
'relativetime.2weeks': 'two weeks ago',
|
||||
'relativetime.2months': 'two months ago',
|
||||
'relativetime.2years': 'two years ago',
|
||||
};
|
||||
window.config.pageData.PLURALSTRINGS_LANG = {
|
||||
'relativetime.mins': ['%d minute ago', '%d minutes ago'],
|
||||
'relativetime.hours': ['%d hour ago', '%d hours ago'],
|
||||
'relativetime.days': ['%d day ago', '%d days ago'],
|
||||
'relativetime.weeks': ['%d week ago', '%d weeks ago'],
|
||||
'relativetime.months': ['%d month ago', '%d months ago'],
|
||||
'relativetime.years': ['%d year ago', '%d years ago'],
|
||||
};
|
||||
const mock = document.createElement('relative-time');
|
||||
|
||||
const now = Date.parse('2024-10-27T04:05:30+01:00'); // One hour after DST switchover, CET.
|
||||
|
||||
expect(DoUpdateRelativeTime(mock, now)).toEqual(null);
|
||||
expect(mock.textContent).toEqual('');
|
||||
|
||||
mock.setAttribute('datetime', '2024-10-27T04:06:40+01:00');
|
||||
expect(DoUpdateRelativeTime(mock, now)).toEqual(70000);
|
||||
expect(mock.textContent).toEqual('in future');
|
||||
|
||||
mock.setAttribute('datetime', '2024-10-27T04:05:10+01:00');
|
||||
expect(DoUpdateRelativeTime(mock, now)).toEqual(HALF_MINUTE);
|
||||
expect(mock.textContent).toEqual('now');
|
||||
|
||||
mock.setAttribute('datetime', '2024-10-27T04:04:30+01:00');
|
||||
expect(DoUpdateRelativeTime(mock, now)).toEqual(ONE_MINUTE);
|
||||
expect(mock.textContent).toEqual('1 minute ago');
|
||||
|
||||
mock.setAttribute('datetime', '2024-10-27T04:04:00+01:00');
|
||||
expect(DoUpdateRelativeTime(mock, now)).toEqual(ONE_MINUTE);
|
||||
expect(mock.textContent).toEqual('1 minute ago');
|
||||
|
||||
mock.setAttribute('datetime', '2024-10-27T04:03:20+01:00');
|
||||
expect(DoUpdateRelativeTime(mock, now)).toEqual(ONE_MINUTE);
|
||||
expect(mock.textContent).toEqual('2 minutes ago');
|
||||
|
||||
mock.setAttribute('datetime', '2024-10-27T04:00:00+01:00');
|
||||
expect(DoUpdateRelativeTime(mock, now)).toEqual(ONE_MINUTE);
|
||||
expect(mock.textContent).toEqual('5 minutes ago');
|
||||
|
||||
mock.setAttribute('datetime', '2024-10-27T03:59:30+01:00');
|
||||
expect(DoUpdateRelativeTime(mock, now)).toEqual(ONE_MINUTE);
|
||||
expect(mock.textContent).toEqual('6 minutes ago');
|
||||
|
||||
mock.setAttribute('datetime', '2024-10-27T03:01:00+01:00');
|
||||
expect(DoUpdateRelativeTime(mock, now)).toEqual(ONE_HOUR);
|
||||
expect(mock.textContent).toEqual('1 hour ago');
|
||||
|
||||
mock.setAttribute('datetime', '2024-10-27T01:00:00+02:00');
|
||||
expect(DoUpdateRelativeTime(mock, now)).toEqual(ONE_HOUR);
|
||||
expect(mock.textContent).toEqual('4 hours ago'); // This tests DST switchover
|
||||
|
||||
mock.setAttribute('datetime', '2024-10-27T00:01:00+02:00');
|
||||
expect(DoUpdateRelativeTime(mock, now)).toEqual(ONE_HOUR);
|
||||
expect(mock.textContent).toEqual('5 hours ago');
|
||||
|
||||
mock.setAttribute('datetime', '2024-10-26T22:00:00+02:00');
|
||||
expect(DoUpdateRelativeTime(mock, now)).toEqual(ONE_HOUR);
|
||||
expect(mock.textContent).toEqual('7 hours ago');
|
||||
|
||||
mock.setAttribute('datetime', '2024-10-26T05:08:00+02:00');
|
||||
expect(DoUpdateRelativeTime(mock, now)).toEqual(ONE_HOUR);
|
||||
expect(mock.textContent).toEqual('23 hours ago');
|
||||
|
||||
mock.setAttribute('datetime', '2024-10-26T04:00:00+02:00');
|
||||
expect(DoUpdateRelativeTime(mock, now)).toEqual(ONE_DAY);
|
||||
expect(mock.textContent).toEqual('yesterday');
|
||||
|
||||
mock.setAttribute('datetime', '2024-10-25T01:00:00+02:00');
|
||||
expect(DoUpdateRelativeTime(mock, now)).toEqual(ONE_DAY);
|
||||
expect(mock.textContent).toEqual('two days ago');
|
||||
|
||||
mock.setAttribute('datetime', '2024-10-21T01:00:00+02:00');
|
||||
expect(DoUpdateRelativeTime(mock, now)).toEqual(ONE_DAY);
|
||||
expect(mock.textContent).toEqual('6 days ago');
|
||||
|
||||
mock.setAttribute('datetime', '2024-10-20T01:00:00+02:00');
|
||||
expect(DoUpdateRelativeTime(mock, now)).toEqual(ONE_DAY);
|
||||
expect(mock.textContent).toEqual('last week');
|
||||
|
||||
mock.setAttribute('datetime', '2024-10-14T01:00:00+02:00');
|
||||
expect(DoUpdateRelativeTime(mock, now)).toEqual(ONE_DAY);
|
||||
expect(mock.textContent).toEqual('last week');
|
||||
|
||||
mock.setAttribute('datetime', '2024-10-13T01:00:00+02:00');
|
||||
expect(DoUpdateRelativeTime(mock, now)).toEqual(ONE_DAY);
|
||||
expect(mock.textContent).toEqual('two weeks ago');
|
||||
|
||||
mock.setAttribute('datetime', '2024-10-06T01:00:00+02:00');
|
||||
expect(DoUpdateRelativeTime(mock, now)).toEqual(ONE_DAY);
|
||||
expect(mock.textContent).toEqual('3 weeks ago');
|
||||
|
||||
mock.setAttribute('datetime', '2024-09-25T01:00:00+02:00');
|
||||
expect(DoUpdateRelativeTime(mock, now)).toEqual(ONE_DAY);
|
||||
expect(mock.textContent).toEqual('last month');
|
||||
|
||||
mock.setAttribute('datetime', '2024-08-30T01:00:00+02:00');
|
||||
expect(DoUpdateRelativeTime(mock, now)).toEqual(ONE_DAY);
|
||||
expect(mock.textContent).toEqual('last month');
|
||||
|
||||
mock.setAttribute('datetime', '2024-07-30T01:00:00+02:00');
|
||||
expect(DoUpdateRelativeTime(mock, now)).toEqual(ONE_DAY);
|
||||
expect(mock.textContent).toEqual('two months ago');
|
||||
|
||||
mock.setAttribute('datetime', '2024-05-30T01:00:00+02:00');
|
||||
expect(DoUpdateRelativeTime(mock, now)).toEqual(ONE_DAY);
|
||||
expect(mock.textContent).toEqual('4 months ago');
|
||||
|
||||
mock.setAttribute('datetime', '2024-03-01T01:00:00+01:00');
|
||||
expect(DoUpdateRelativeTime(mock, now)).toEqual(ONE_DAY);
|
||||
expect(mock.textContent).toEqual('7 months ago');
|
||||
|
||||
mock.setAttribute('datetime', '2024-02-29T01:00:00+01:00'); // Leap day handling
|
||||
expect(DoUpdateRelativeTime(mock, now)).toEqual(ONE_DAY);
|
||||
expect(mock.textContent).toEqual('7 months ago');
|
||||
|
||||
mock.setAttribute('datetime', '2024-02-27T01:00:00-03:00');
|
||||
expect(DoUpdateRelativeTime(mock, now)).toEqual(ONE_DAY);
|
||||
expect(mock.textContent).toEqual('7 months ago');
|
||||
|
||||
mock.setAttribute('datetime', '2024-02-27T01:00:00+01:00');
|
||||
expect(DoUpdateRelativeTime(mock, now)).toEqual(ONE_DAY);
|
||||
expect(mock.textContent).toEqual('8 months ago');
|
||||
|
||||
mock.setAttribute('datetime', '2023-11-15T01:00:00+03:00');
|
||||
expect(DoUpdateRelativeTime(mock, now)).toEqual(ONE_DAY);
|
||||
expect(mock.textContent).toEqual('11 months ago');
|
||||
|
||||
mock.setAttribute('datetime', '2023-10-20T01:00:00+08:00');
|
||||
expect(DoUpdateRelativeTime(mock, now)).toEqual(ONE_DAY);
|
||||
expect(mock.textContent).toEqual('last year');
|
||||
|
||||
mock.setAttribute('datetime', '2022-10-30T01:00:00-05:30');
|
||||
expect(DoUpdateRelativeTime(mock, now)).toEqual(ONE_DAY);
|
||||
expect(mock.textContent).toEqual('last year');
|
||||
|
||||
mock.setAttribute('datetime', '2022-10-20T01:00:00+10:00');
|
||||
expect(DoUpdateRelativeTime(mock, now)).toEqual(ONE_DAY);
|
||||
expect(mock.textContent).toEqual('two years ago');
|
||||
|
||||
mock.setAttribute('datetime', '2021-10-20T01:00:00+02:00');
|
||||
expect(DoUpdateRelativeTime(mock, now)).toEqual(ONE_DAY);
|
||||
expect(mock.textContent).toEqual('3 years ago');
|
||||
|
||||
mock.setAttribute('datetime', '2014-10-20T01:00:00+02:00');
|
||||
expect(DoUpdateRelativeTime(mock, now)).toEqual(ONE_DAY);
|
||||
expect(mock.textContent).toEqual('10 years ago');
|
||||
|
||||
// Timezone tests
|
||||
mock.setAttribute('datetime', '2024-10-27T01:05:30-05:00');
|
||||
expect(DoUpdateRelativeTime(mock, now)).toEqual(3 * ONE_HOUR);
|
||||
expect(mock.textContent).toEqual('in future');
|
||||
|
||||
mock.setAttribute('datetime', '2024-10-27T05:05:25+02:00');
|
||||
expect(DoUpdateRelativeTime(mock, now)).toEqual(HALF_MINUTE);
|
||||
expect(mock.textContent).toEqual('now');
|
||||
|
||||
mock.setAttribute('datetime', '2024-10-27T05:04:00+02:00');
|
||||
expect(DoUpdateRelativeTime(mock, now)).toEqual(ONE_MINUTE);
|
||||
expect(mock.textContent).toEqual('1 minute ago');
|
||||
|
||||
mock.setAttribute('datetime', '2024-10-27T05:02:00+02:00');
|
||||
expect(DoUpdateRelativeTime(mock, now)).toEqual(ONE_MINUTE);
|
||||
expect(mock.textContent).toEqual('3 minutes ago');
|
||||
|
||||
mock.setAttribute('datetime', '2024-10-27T04:06:00+02:00');
|
||||
expect(DoUpdateRelativeTime(mock, now)).toEqual(ONE_MINUTE);
|
||||
expect(mock.textContent).toEqual('59 minutes ago');
|
||||
|
||||
mock.setAttribute('datetime', '2024-10-27T04:05:00+02:00');
|
||||
expect(DoUpdateRelativeTime(mock, now)).toEqual(ONE_HOUR);
|
||||
expect(mock.textContent).toEqual('1 hour ago');
|
||||
|
||||
mock.setAttribute('datetime', '2024-10-27T01:00:00+02:00');
|
||||
expect(DoUpdateRelativeTime(mock, now)).toEqual(ONE_HOUR);
|
||||
expect(mock.textContent).toEqual('4 hours ago');
|
||||
|
||||
mock.setAttribute('datetime', '2024-10-27T01:00:00+04:00');
|
||||
expect(DoUpdateRelativeTime(mock, now)).toEqual(ONE_HOUR);
|
||||
expect(mock.textContent).toEqual('6 hours ago');
|
||||
|
||||
mock.setAttribute('datetime', '2024-10-27T01:00:00+10:00');
|
||||
expect(DoUpdateRelativeTime(mock, now)).toEqual(ONE_HOUR);
|
||||
expect(mock.textContent).toEqual('12 hours ago');
|
||||
|
||||
mock.setAttribute('datetime', '2024-10-27T01:00:00Z');
|
||||
expect(DoUpdateRelativeTime(mock, now)).toEqual(ONE_HOUR);
|
||||
expect(mock.textContent).toEqual('2 hours ago');
|
||||
|
||||
mock.setAttribute('datetime', '2024-10-26T15:00:00-01:00');
|
||||
expect(DoUpdateRelativeTime(mock, now)).toEqual(ONE_HOUR);
|
||||
expect(mock.textContent).toEqual('11 hours ago');
|
||||
|
||||
mock.setAttribute('datetime', '2024-10-25T19:00:00-11:00');
|
||||
expect(DoUpdateRelativeTime(mock, now)).toEqual(ONE_HOUR);
|
||||
expect(mock.textContent).toEqual('21 hours ago');
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue