OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Add 'daysAndHours' format to js ChronicDuration
Christophe Bliard5e2603d03ffb
diff --git a/frontend/src/app/shared/helpers/chronic_duration.js b/frontend/src/app/shared/helpers/chronic_duration.jsindex 8cd347d5ce7..9bc13b38e4f 100644--- a/frontend/src/app/shared/helpers/chronic_duration.js+++ b/frontend/src/app/shared/helpers/chronic_duration.js@@ -42,6 +42,9 @@ export class DurationParseError extends Error {// On average, there's a little over 4 weeks in month.const FULL_WEEKS_PER_MONTH = 4;+// 365.25 days in a year.+const SECONDS_PER_YEAR = 31557600;+const HOURS_PER_DAY = 24;const DAYS_PER_MONTH = 30;@@ -102,7 +105,7 @@ function durationUnitsSecondsMultiplier(unit, opts) {switch (unit) {case 'years':- return 31557600;+ return SECONDS_PER_YEAR;case 'months':return 3600 * hoursPerDay * daysPerMonth;case 'weeks':@@ -193,12 +196,6 @@ function filterThroughWhiteList(string, opts) {function cleanup(string, opts) {let res = string.toLowerCase();- /*- * TODO The Ruby implementation of this algorithm uses the Numerizer module,- * which converts strings like "forty two" to "42", but there is no- * JavaScript equivalent of Numerizer. Skip it for now until Numerizer is- * ported to JavaScript.- */res = filterByType(res);res = res.replace(FLOAT_MATCHER, (n) => ` ${n} `)@@ -211,6 +208,9 @@ function humanizeTimeUnit(number, unit, pluralize, keepZero) {if (number === '0' && !keepZero) {return null;}+ if (unit === undefined) {+ return null;+ }let res = number + unit;// A poor man's pluralizerif (number !== '1' && pluralize) {@@ -251,9 +251,9 @@ export function outputChronicDuration(seconds, opts = {}) {const hour = 60 * minute;const day = hoursPerDay * hour;const month = daysPerMonth * day;- const year = 31557600;+ const year = SECONDS_PER_YEAR;- if (units.seconds >= 31557600 && units.seconds % year < units.seconds % month) {+ if (units.seconds >= SECONDS_PER_YEAR && units.seconds % year < units.seconds % month) {units.years = Math.trunc(units.seconds / year);units.months = Math.trunc((units.seconds % year) / month);units.days = Math.trunc(((units.seconds % year) % month) / day);@@ -330,6 +330,29 @@ export function outputChronicDuration(seconds, opts = {}) {pluralize: true,};break;+ case 'daysAndHours':+ dividers = {+ // days: 'd',+ hours: 'h',+ keepZero: true,+ };++ units.days += units.weeks * daysPerWeek+ units.weeks = 0+ units.days += units.months * daysPerMonth+ units.months = 0+ units.days += Math.floor(units.years * SECONDS_PER_YEAR / 3600 / 24)+ units.years = 0+ if (units.days > 0) {+ dividers.days = 'd';+ }++ units.hours += (((units.minutes * 60) + units.seconds) / 3600.0)+ units.hours = parseFloat(Math.round(units.hours * 100)) / 100;+ const hoursDecimalPlaces =+ units.hours % 1 !== 0 ? Math.min(2, units.hours.toString().split('.').reverse()[0].length) : null;++ breakcase 'chrono':dividers = {years: ':',diff --git a/frontend/src/app/shared/helpers/chronic_duration.spec.ts b/frontend/src/app/shared/helpers/chronic_duration.spec.tsindex 58dcc7e80fe..c2a99a3fb79 100644--- a/frontend/src/app/shared/helpers/chronic_duration.spec.ts+++ b/frontend/src/app/shared/helpers/chronic_duration.spec.ts@@ -160,6 +160,7 @@ describe('outputChronicDuration', () => {short: '1m 20s',default: '1 min 20 secs',long: '1 minute 20 seconds',+ daysAndHours: '0.02h',chrono: '1:20',},[60 + 20.51]: {@@ -167,6 +168,7 @@ describe('outputChronicDuration', () => {short: '1m 20.51s',default: '1 min 20.51 secs',long: '1 minute 20.51 seconds',+ daysAndHours: '0.02h',chrono: '1:20.51',},[60 + 20.51928]: {@@ -174,6 +176,7 @@ describe('outputChronicDuration', () => {short: '1m 20.51928s',default: '1 min 20.51928 secs',long: '1 minute 20.51928 seconds',+ daysAndHours: '0.02h',chrono: '1:20.51928',},[4 * 3600 + 60 + 1]: {@@ -181,6 +184,7 @@ describe('outputChronicDuration', () => {short: '4h 1m 1s',default: '4 hrs 1 min 1 sec',long: '4 hours 1 minute 1 second',+ daysAndHours: '4.02h',chrono: '4:01:01',},[2 * 3600 + 20 * 60]: {@@ -188,20 +192,23 @@ describe('outputChronicDuration', () => {short: '2h 20m',default: '2 hrs 20 mins',long: '2 hours 20 minutes',- chrono: '2:20',- },- [2 * 3600 + 20 * 60]: {- micro: '2h20m',- short: '2h 20m',- default: '2 hrs 20 mins',- long: '2 hours 20 minutes',+ daysAndHours: '2.33h',chrono: '2:20:00',},+ [8 * 24 * 3600 + 3 * 3600 + 30 * 60]: {+ micro: '8d3h30m',+ short: '8d 3h 30m',+ default: '8 days 3 hrs 30 mins',+ long: '8 days 3 hours 30 minutes',+ daysAndHours: '8d 3.5h',+ chrono: '8:03:30:00'+ },[6 * 30 * 24 * 3600 + 24 * 3600]: {micro: '6mo1d',short: '6mo 1d',default: '6 mos 1 day',long: '6 months 1 day',+ daysAndHours: '181d 0h',chrono: '6:01:00:00:00', // Yuck. FIXME},[365.25 * 24 * 3600 + 24 * 3600]: {@@ -209,6 +216,7 @@ describe('outputChronicDuration', () => {short: '1y 1d',default: '1 yr 1 day',long: '1 year 1 day',+ daysAndHours: '366d 0h',chrono: '1:00:01:00:00:00',},[3 * 365.25 * 24 * 3600 + 24 * 3600]: {@@ -216,13 +224,23 @@ describe('outputChronicDuration', () => {short: '3y 1d',default: '3 yrs 1 day',long: '3 years 1 day',+ daysAndHours: '1096d 0h',chrono: '3:00:01:00:00:00',},+ [6 * 365.25 * 24 * 3600 + 3 * 3600]: {+ micro: '6y3h',+ short: '6y 3h',+ default: '6 yrs 3 hrs',+ long: '6 years 3 hours',+ daysAndHours: '2191d 3h',+ chrono: '6:00:00:03:00:00',+ },[3600 * 24 * 30 * 18]: {micro: '18mo',short: '18mo',default: '18 mos',long: '18 months',+ daysAndHours: '540d 0h',chrono: '18:00:00:00:00',},};@@ -336,6 +354,8 @@ describe('outputChronicDuration', () => {Object.entries(EXEMPLARS).forEach(([seconds, formatSpec]) => {const secondsF = parseFloat(seconds);Object.keys(formatSpec).forEach((format) => {+ if (format === 'daysAndHours') return;+it(`outputs a duration for ${seconds} that parses back to the same thing when using the ${format} format`, () => {expect(parseChronicDuration(outputChronicDuration(secondsF, { format }))).toBe(secondsF);});diff --git a/lib/chronic_duration.rb b/lib/chronic_duration.rbindex 720c153935d..1512154e0d2 100644--- a/lib/chronic_duration.rb+++ b/lib/chronic_duration.rb@@ -39,6 +39,9 @@ module ChronicDuration# On average, there's a little over 4 weeks in month.FULL_WEEKS_PER_MONTH = 4+ # 365.25 days in a year.+ SECONDS_PER_YEAR = 31_557_600+@@raise_exceptions = false@@hours_per_day = 24@@days_per_month = 30@@ -88,7 +91,6 @@ module ChronicDurationhours_per_day = opts[:hours_per_day] || ChronicDuration.hours_per_daydays_per_month = opts[:days_per_month] || ChronicDuration.days_per_monthdays_per_week = days_per_month / FULL_WEEKS_PER_MONTH- seconds_per_year = 31_557_600years = months = weeks = days = hours = minutes = 0@@ -98,9 +100,9 @@ module ChronicDurationhour = 60 * minuteday = hours_per_day * hourmonth = days_per_month * day- year = seconds_per_year+ year = SECONDS_PER_YEAR- if seconds >= seconds_per_year && seconds % year < seconds % month+ if seconds >= SECONDS_PER_YEAR && seconds % year < seconds % monthyears = seconds / yearmonths = seconds % year / monthdays = seconds % year % month / day@@ -163,7 +165,7 @@ module ChronicDurationdays += weeks * days_per_weekdays += months * days_per_month- days += years * seconds_per_year / 3600 / 24+ days += years * SECONDS_PER_YEAR / 3600 / 24dividers[:days] = "d" if days > 0years = months = weeks = 0@@ -191,7 +193,6 @@ module ChronicDurationresult = %i[years months weeks days hours minutes seconds].map do |t|next if t == :weeks && !opts[:weeks]- next unless dividers[t]num = eval(t.to_s) # rubocop:disable Security/Evalnum = ("%.#{decimal_places}f" % num) if num.is_a?(Float) && t == :seconds@@ -214,6 +215,7 @@ module ChronicDurationdef humanize_time_unit(number, unit, pluralize, keep_zero)return nil if number == 0 && !keep_zero+ return unless unitres = "#{number}#{unit}"# A poor man's pluralizerdiff --git a/spec/lib/chronic_duration_spec.rb b/spec/lib/chronic_duration_spec.rbindex 3da4e5c17d2..b2e56c36a65 100644--- a/spec/lib/chronic_duration_spec.rb+++ b/spec/lib/chronic_duration_spec.rb@@ -203,6 +203,14 @@ RSpec.describe ChronicDuration dodays_and_hours: "1096d 0h",chrono: "3:00:01:00:00:00"},+ ((6 * 365.25 * 24 * 3600) + (3 * 3600)).to_i => {+ micro: "6y3h",+ short: "6y 3h",+ default: "6 yrs 3 hrs",+ long: "6 years 3 hours",+ days_and_hours: "2191d 3h",+ chrono: "6:00:00:03:00:00"+ },(3600 * 24 * 30 * 18) =>{micro: "18mo",