Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to have 'deadlines' extended to work without 'in/within' #21

Open
smt923 opened this issue Oct 25, 2019 · 1 comment
Open

Comments

@smt923
Copy link

smt923 commented Oct 25, 2019

It would be nice to be able to parse "2 hours" or "4 days" instead of requiring the in/within, maybe it's a bit "noisy" but if it were possible via an option or some sort of "aggressive" version of deadline i think it could be nice - sometimes you just want to parse a time no matter what, and it won't always have the in/within at the start

@olebedev
Copy link
Owner

olebedev commented Jun 1, 2023

Hey @smt923,

Sorry for late reply here. This is possible to do. Please keep in mind that by cutting the context that can be matched we generalise the rule so we increase possibilities where the rul will be capturing what is not intended to be captured.

This is possible with slight modifications of the deadline rule. The are couple of things to do, to make it work:

  • introduce an optional parameter to the rule in this line
  • handle the optional parameter while creating the RegExp here

By that, you will be able to use the rule with changed pattern, for example:

w := when.New(nil)
w.Add(deadline({ Aggressive: true }))
text := "2 hours"
r, err := w.Parse(text, time.Now())
// handle the result here

Please feel free to send a PR, happy to review, when you're ready!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants