Zeroclickinfo-goodies: New Duration Calculator Goodie

Created on 24 May 2016  路  17Comments  路  Source: duckduckgo/zeroclickinfo-goodies

A simple calculator that enables users to enter queries of time like 3 hours 45 minutes + 1 hour 20 minutes and returns the calculated result.


IA Page: https://duck.co/ia/view/duration_calculator

Maintainer Approved Suggestion

Most helpful comment

@GuiltyDolphin sure... Except that I am going to trouble you with even more questions now

All 17 comments

@GuiltyDolphin We discussed this on slack.

@sam09 Hey Sam, thanks for this!

First things first, make sure you are familiar with the source https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/DateMath.pm - perhaps have a little play with it.

Okay, here's a some info that may be useful:

Packages

I really like DateTime::Duration and DateTime::Format::Human::Duration (you may not need the second one) for working with date units.

Triggering

You shouldn't need to worry - we already have triggers for the common units: second, minute, hour etc. (I don't think it supports mins (and similar) at the moment - you can add this if you wish)

You may need to add another regex or two to match X units + Y units.


I'm working on refactoring the tests at the moment - hopefully it won't create too many issues!

@sam09 If you have any ideas, suggestions or questions don't hesitate to let me know!

When you've got the basics working just ping me in a PR and I'll come and have a look.

Hi.. I tried the to trigger the regex. However I am unable to figure out the trigger for queries like '2 hours + 1 hour 20 min'

@sam09 You'd just want another regex for forms using the existing units; basically you'd want to combine $action_re and $relative_regex (not sure why it's called that!); something like $relative_regex $action_re $relative_regex (with more handling) which should match 5 days + 2 hours and similar.

@GuiltyDolphin As you said the regex would be $relative_regex $action_re $relative_regex. That would match queries like 5 hours 3 minutes + 2 hours.

But the issue would be to extract the quantities and corresponding units from both ends and then perform the action required.
The only solutions I could come up with was add two different regex $relative_regex1 and $relative_regex2 so that it is easier to extract each term.

Or write regex in the complete form and then extract the matching groups using the old-fashioned way ( $1 , $2).

I am hoping there is a better way to do this. Any ideas?

@sam09 The existing method should be fine - just use named captures, e.g., (?<operand_a>$relative_regex)$action_re(?<operand_b>$relative_regex) then you could pull out $+{operand_a}, $+{operand_b}, and $+{action}; to get the units and numbers from operand_a and operand_b you could just match on $relative_regex and pull out the appropriate data.

Can I suggest that this be renamed to Duration Calculator? It's a slightly pedantic point, but time can mean either an "amount" of time or a "point in time"; whereas duration exclusively means the "amount of time". To me "time calculator" could be adding datetimes together

@mintsoft Hmm? This is an extension to the DateMath Goodie; expanding the current features to support unit arithmetic.

@mintsoft I agree that "time calculator" is a little vague. I think "Duration Calculator" makes sense with the package names. "Time" could mean 8:45pm + 2.5 hours or something similar.

Ahk, bummer - @sam09 I forgot about https://github.com/duckduckgo/zeroclickinfo-goodies/pull/2897 - that'll make this pretty incompatible with DateMath (unless we have a very dynamic front end).

It may be worth implementing this as a separate Goodie after all - though I reckon you could use a decent portion of the DateMath codebase for triggering/handling units (and ofc I'll still advise :) ).

@sam09 Here, this should be a decent page for this Goodie https://duck.co/ia/view/duration_calculator

@tagawa Could you set @sam09 as the maintainer on that please?

@GuiltyDolphin I set @sam09 as the maintainer for you 馃憤

@edgesince84 thanks a lot....

@sam09 Basically everything I've said should still apply - you'll want to use the suggested packages and possibly build upon those regexes in DateMath - the main difference being that you'll be working in separate files; you still happy to do this?

@GuiltyDolphin sure... Except that I am going to trouble you with even more questions now

@sam09 don't be afraid to hit us up in Slack if you have any questions!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gautamkrishnar picture gautamkrishnar  路  16Comments

klntsky picture klntsky  路  13Comments

moollaza picture moollaza  路  13Comments

GuiltyDolphin picture GuiltyDolphin  路  12Comments

pjhampton picture pjhampton  路  12Comments