Skip to content
This repository has been archived by the owner on Oct 15, 2022. It is now read-only.

New Duration Calculator Goodie #3143

Closed
sam09 opened this issue May 24, 2016 · 17 comments
Closed

New Duration Calculator Goodie #3143

sam09 opened this issue May 24, 2016 · 17 comments

Comments

@sam09
Copy link
Collaborator

sam09 commented May 24, 2016

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

@sam09
Copy link
Collaborator Author

sam09 commented May 24, 2016

@GuiltyDolphin We discussed this on slack.

@GuiltyDolphin
Copy link
Member

@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.

@sam09
Copy link
Collaborator Author

sam09 commented May 30, 2016

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'

@GuiltyDolphin
Copy link
Member

@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.

@sam09
Copy link
Collaborator Author

sam09 commented Jun 1, 2016

@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?

@GuiltyDolphin
Copy link
Member

@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.

@mintsoft
Copy link
Collaborator

mintsoft commented Jun 1, 2016

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

@GuiltyDolphin
Copy link
Member

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

@edgesince84
Copy link
Collaborator

@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.

@GuiltyDolphin GuiltyDolphin changed the title A Time Calculator DateMath: Add support for unit calculations Jun 1, 2016
@GuiltyDolphin
Copy link
Member

GuiltyDolphin commented Jun 2, 2016

Ahk, bummer - @sam09 I forgot about #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 :) ).

@GuiltyDolphin GuiltyDolphin changed the title DateMath: Add support for unit calculations New Duration Calculator Goodie Jun 2, 2016
@GuiltyDolphin
Copy link
Member

@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?

@edgesince84
Copy link
Collaborator

@GuiltyDolphin I set @sam09 as the maintainer for you 👍

@sam09
Copy link
Collaborator Author

sam09 commented Jun 3, 2016

@edgesince84 thanks a lot....

@GuiltyDolphin
Copy link
Member

@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?

@sam09
Copy link
Collaborator Author

sam09 commented Jun 3, 2016

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

@edgesince84
Copy link
Collaborator

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

@sam09
Copy link
Collaborator Author

sam09 commented Jun 25, 2016

#3205 Fixes the issue

@sam09 sam09 closed this as completed Jun 25, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants