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

Validater for if arrow.get() properly parsed the date. #399

Closed
richard-engineering opened this issue Dec 21, 2016 · 11 comments
Closed

Validater for if arrow.get() properly parsed the date. #399

richard-engineering opened this issue Dec 21, 2016 · 11 comments
Labels

Comments

@richard-engineering
Copy link

Using latest version of arrow

>>> import arrow
>>> arrow.get('10-10-2016')
<Arrow [2016-01-01T00:00:00+00:00]>
@Zamubafoo
Copy link

The issue causing this is that the parser is only parsing the year correctly.

In [2]: arrow.get('02-04-2016')
Out[2]: <Arrow [2016-01-01T00:00:00+00:00]>

This mainly due to the way the formats are generated. Adding some common formats into the mix might alleviate some the issues, but best practice seems to always use tokens to explicitly tell the parser how to read the date.

In [3]: arrow.get('02-04-2016','MM-DD-YYYY')
Out[3]: <Arrow [2016-02-04T00:00:00+00:00]>

@richard-engineering
Copy link
Author

richard-engineering commented Dec 29, 2016

I do not want to have to specify formats. The reason for using arrow over datetime is purely because .get() in theory is supposed to be able to normalize inputs. Other time libraries like moment.js have no problem figuring out what '10-10-2016' means. Or at the very least arrow should not be returning an object with the wrong date and month because that will just mean silent errors.

moment('10-10-2016').format()
'2016-10-10T00:00:00-07:00'

@andrewelkins
Copy link
Contributor

andrewelkins commented Dec 29, 2016

While current Moment will parse that string, it is deprecated and being removed in a future release. We don't plan to support it either as it is an non-standard iso format.

VM190 moment.js:291 Deprecation warning: value provided is not in a recognized ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions. Non ISO date formats are discouraged and will be removed in an upcoming major release. Please refer to http://momentjs.com/guides/#/warnings/js-date/ for more info.
Arguments: 
[0] _isAMomentObject: true, _isUTC: false, _useUTC: false, _l: undefined, _i: 10-10-2016, _f: undefined, _strict: undefined, _locale: [object Object]
Error
    at Function.createFromInputFallback (https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.js:318:94)
    at configFromString (https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.js:2080:15)
    at configFromInput (https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.js:2444:9)
    at prepareConfig (https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.js:2427:9)
    at createFromConfig (https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.js:2394:40)
    at createLocalOrUTC (https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.js:2481:12)
    at createLocal (https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.js:2485:12)
    at hooks (https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.js:16:25)
    at window.onload (https://fiddle.jshell.net/_display/:49:13)

Fiddle to show example: https://jsfiddle.net/f4vxajpj/

@richard-engineering
Copy link
Author

richard-engineering commented Dec 29, 2016

Is there a way at least to see if the format has been properly parsed by arrow? In a generic sense where I don't need to specify a list of formats.

@andrewelkins
Copy link
Contributor

That's tricky because it goes through a bunch of formats to try and match anything in the string. In this case it's able to match on the year, so it is returning that. Not ideal for your use case, I'm aware.

Perhaps something can be done in https://github.com/crsmithdev/arrow/blob/master/arrow/parser.py#L271

@richard-engineering
Copy link
Author

Thanks that looks promising!

@andrewelkins
Copy link
Contributor

Not quite sure how to handle this currently. Reopening for a future look.

@richard-engineering
Copy link
Author

richard-engineering commented Dec 29, 2016

We (or rather I) will probably look at this sometime at work so if I do get a small solution specifically for my use case I'll let you know. My current initial impression (no IDE atm to step through) based on the code is something in .parse() would need to be modified when the formats are being generated. Either that or maybe in builddatetime where a kwarg is passed like 'enforce complete date' or 'validator' (lambda/func) that checks to make sure year/month/date has been parsed (for my use case anyway, for others they may want more accuracy). Thanks for reopening.

@richard-engineering richard-engineering changed the title Incorrect arrow.get date parsing Validater for if arrow.get() properly parsed the date. Dec 29, 2016
@andrewelkins
Copy link
Contributor

andrewelkins commented Dec 31, 2016

@rhan-mentad If you have the time to step through and at minimum get a rough PR together that would be appreciated.

I'd like to handle two situations which might be fixed with the same code:

  • 10/10/2016 #Non-iso format which currently returns 2016-01-01 - At minimum should return an error
  • 2016-1-10 #Non-padded month which currently returns 2016-01-01 - At minimum should return an error

@Zamubafoo
Copy link

It'd be simple, just have it have a callback keyword that it calls to report the tokens it matched. Users can then write a function to check the appropriateness of the parsing or just use the print function to display ut

@andrewelkins
Copy link
Contributor

Will be handled by #91 . Let me know if you make any progress on the PR. I would love to take a look.

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

No branches or pull requests

3 participants