diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-03-30 18:09:00 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-03-30 18:09:00 +0300 |
commit | f4199ade274d43b2627d17d9087f42d0f8175369 (patch) | |
tree | f8b2311aeae6d4eeed80ee2db7eb8b06e5f7cba8 /app/assets/javascripts/lib | |
parent | e672c333df6dc238e5050ab5eb709d6df5c87380 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/assets/javascripts/lib')
-rw-r--r-- | app/assets/javascripts/lib/utils/datetime/timeago_utility.js | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/app/assets/javascripts/lib/utils/datetime/timeago_utility.js b/app/assets/javascripts/lib/utils/datetime/timeago_utility.js index d68682ebed1..2b898d8b6a5 100644 --- a/app/assets/javascripts/lib/utils/datetime/timeago_utility.js +++ b/app/assets/javascripts/lib/utils/datetime/timeago_utility.js @@ -70,8 +70,41 @@ const memoizedLocale = () => { }; }; +/** + * Registers timeago time duration + */ +const memoizedLocaleDuration = () => { + const cache = []; + + const durations = [ + () => [s__('Duration|%s seconds')], + () => [s__('Duration|%s seconds')], + () => [s__('Duration|1 minute')], + () => [s__('Duration|%s minutes')], + () => [s__('Duration|1 hour')], + () => [s__('Duration|%s hours')], + () => [s__('Duration|1 day')], + () => [s__('Duration|%s days')], + () => [s__('Duration|1 week')], + () => [s__('Duration|%s weeks')], + () => [s__('Duration|1 month')], + () => [s__('Duration|%s months')], + () => [s__('Duration|1 year')], + () => [s__('Duration|%s years')], + ]; + + return (_, index) => { + if (cache[index]) { + return cache[index]; + } + cache[index] = durations[index] && durations[index](); + return cache[index]; + }; +}; + timeago.register(timeagoLanguageCode, memoizedLocale()); timeago.register(`${timeagoLanguageCode}-remaining`, memoizedLocaleRemaining()); +timeago.register(`${timeagoLanguageCode}-duration`, memoizedLocaleDuration()); let memoizedFormatter = null; @@ -133,3 +166,16 @@ export const timeFor = (time, expiredLabel) => { } return timeago.format(time, `${timeagoLanguageCode}-remaining`).trim(); }; + +/** + * Returns a duration of time given an amount. + * + * @param {number} milliseconds - Duration in milliseconds. + * @returns {string} A formatted duration, e.g. "10 minutes". + */ +export const duration = (milliseconds) => { + const now = new Date(); + return timeago + .format(now.getTime() - Math.abs(milliseconds), `${timeagoLanguageCode}-duration`) + .trim(); +}; |