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

Add automatic AM/PM logic #102

Open
squatto opened this issue Apr 23, 2021 · 0 comments
Open

Add automatic AM/PM logic #102

squatto opened this issue Apr 23, 2021 · 0 comments

Comments

@squatto
Copy link

squatto commented Apr 23, 2021

It is 3:25pm where I live, and if I do r at 4:30 Go home it sets a reminder at 4:30am tomorrow morning. The date parsing library is interpreting it correctly, albeit a bit too literal. I have to do r at 16:30 or r at 4:30 tonight to get it to work as intended. The same thing happens if I do r at 3:45 - even though that is in 20 minutes, it interprets it as tomorrow morning at 3:45am.

It would be helpful to add some logic that determines the nearest future hour instead of parsing it literally. I think the conditions would be:

  1. It is currently PM
  2. No AM/PM is specified in the command
  3. The provided hour is between 0 and 12
  4. The provided hour is >= the current hour

The Chrono docs actually has a code snippet in the "Refiner" section that does something very similar:

https://github.com/wanasit/chrono/blob/b6140ec17996fec84c790891ecbfd8b7d44fbb5b/README.md#L218-L245


// In the example, the custom refiner assigns PM to parsing results with ambiguous meridiem (http://en.wikipedia.org/wiki/12-hour_clock).

const custom = chrono.casual.clone();
custom.refiners.push({
    refine: (context, results) => {
        // If there is no AM/PM (meridiem) specified,
        //  let all time between 1:00 - 4:00 be PM (13.00 - 16.00)
        results.forEach((result) => {
            if (!result.start.isCertain('meridiem') &&
                result.start.get('hour') >= 1 && result.start.get('hour') < 4) {

                result.start.assign('meridiem', 1);
                result.start.assign('hour', result.start.get('hour') + 12);
            }
        });
        return results;
    }
});

// This will be parsed as PM.
// > Tue Dec 16 2014 14:30:00 GMT-0600 (CST) 
custom.parseDate("This is at 2.30");

// Unless the 'AM' part is specified
// > Tue Dec 16 2014 02:30:00 GMT-0600 (CST)
custom.parseDate("This is at 2.30 AM");

With a little bit of tweaking, I think that could do exactly what I'm talking about. If you'd like, I'd be happy to PR the change. It will likely be a couple of weeks from now, but I'd be happy to.

Thanks for such an amazing workflow! It's indispensable to my daily routine.

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

1 participant