What I’m trying to do
I have a base table listing visits I have made. Each visit has a duration. I want to summarise the duration in the bottom line. Is there a way to add a custom function to achieve this ?
Things I have tried
I far as I can see there is no property type for duration. The functions I can find only add numbers.
I feel it should be possible with a map() function. Something like:
summaries:
Total time: values.map(duration(value)).sum()
But this does not work.
I have been running my project reporting for some years with dataview. I am trying to convert to bases for reporting. In dv I need two statements to do what I want to do on the same table as shown above:
const total = pages.duration.array().reduce((acc,val) => acc + val, 0);
const d = dv.luxon.Duration.fromMillis(total).rescale().toHuman();
After some testing I found there are at least three misconceptions in my simple approach:
- My way of specifying a duration value as “1h 35 m” is not supported by the base duration() function. Duration expects duration(“1h”) + duration(“35m”).
- It seems the duration() function does not return a number of miliseconds when used in number(duration(“1h”)). I can fake this by adding the duration to now() and subtracting now() from the result though.
- I could find no way to format a duration value like you can do for dates with moments.js. Probably not too big a problem.
- It seems you cannot use Javascript arrow functions as the second parameter in a string.replace(regex,arrow_function)
So I came up with the following formula with a regex that works very well, however because I cannot use arrow functions I cannot use this to calculate the number of minutes:
minutes: duration.replace(/^\s*(?:(\d+)\s*(?:h|hr|hrs|hour|hours)\s*)?(?:(\d+)\s*(?:m|min|mins|minute|minutes)\s*)?$/i, (_, h, m) => `${Number(h||0)} * 60 + ${Number(m||0)}`)
This is as far as i can get with my limited knowledge.