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

BCE years trigger ISO8601Error #79

Open
jonasengelmann opened this issue Feb 24, 2023 · 4 comments
Open

BCE years trigger ISO8601Error #79

jonasengelmann opened this issue Feb 24, 2023 · 4 comments

Comments

@jonasengelmann
Copy link

I work with historical data and also BCE dates, however these currently result in an error, e.g.:
Unrecognised ISO 8601 date format: '-0663-01-01'

Yet according to ISO 8601 BCE years should be denoted with a preceding - sign, e.g. -0002-04-12 is even listed as an example.

@dymil
Copy link

dymil commented Jul 19, 2023

I'm also affected but note this is known behaviour:

standard. The only limitations it has, are given by the Python datetime.date

Probably the fix is to replace the built-in Python library with another implementation, e.g., NumPy, but that is also probably a breaking change.

@micahcochran
Copy link

micahcochran commented Sep 8, 2023

@dymil hit the nail on the head with the response. I wrote this so I would understand the problem a little better. Python's datetime.datetime doesn't support negative years.

>>> from datetime import datetime

>>> datetime(63,1,1)
datetime.datetime(63, 1, 1, 0, 0)

>>> datetime(-63,1,1)
ValueError: year -63 is out of range

Python date libraries like Pendulum (pendulum.datetime) and Arrow (arrow.Arrow) have the same problem because these libraries use datetime.datetime for their internal representation.

The expanded parameter of parse_date's documentation suggests that negative years are supported source code. The regular expressions support negative years, if only a datetime.datetime function (or one that took similar parameters) would support negative years.

I agree with @dymil NumPy is a great alternative that does the ISO parsing itself and supports negative years.

If there were another library that took a datetime.datetime-like parameters and supported negative years, then I think it would be a good move to add a datetime_func= parameter to the parse_date() function. This approach would add no new imports for isodate. Is there a Python date library that supports negative/BCE dates?

@jonasengelmann
Copy link
Author

Thanks for your clarifications! I have worked with flexidate before, maybe it is worth a try.
In any case, I think the current error message when parsing BCE years is misleading, perhaps it could be updated in the meantime?

@micahcochran
Copy link

In any case, I think the current error message when parsing BCE years is misleading, perhaps it could be updated in the meantime?

expanded= parameter

@jonasengelmann If you want to parse negative years per the current documentation, it should be called like this.

>>> isodate.parse_date('-0663-01-01', expanded=True)
ValueError: year -663 is out of range

I think a ValueError is a descriptive error message.

If you want, I can put a PR together to allow a FlexiDate object to be able to passed instead of the Python date function. Only if you want me to.

Here's what I'd propose:

>>> import flexidate
>>> isodate.parse_date('-0663-01-01', expanded=True, date_obj=flexidate.Flexidate)
<class 'flexidate.FlexiDate'> -0663-01-01

This has the benefit of adding no more dependencies to the isodate library, but still allowing it to be useful for this edge case.

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

3 participants