nr-date
nr-date is a fast, regex-based Python library designed for parsing human-readable dates and ISO 8601 durations in pure Python. It provides functionality to handle various date and time formats and is known for its performance compared to some standard library alternatives. The library is currently at version 2.1.0 and receives periodic updates, with major releases occurring roughly every few years.
Common errors
-
ModuleNotFoundError: No module named 'nr_date'
cause The package is installed as 'nr-date' on PyPI, but its internal Python module structure does not expose `nr_date` directly as a top-level module. The primary components are within `nr.parsing.date`.fixChange your import statement from `import nr_date` to `from nr.parsing.date import ISO_8601, duration` (or other specific components you need). -
AttributeError: module 'nr.parsing.date' has no attribute 'parse'
cause The `nr.parsing.date` module itself does not expose a global `parse` function. Instead, parsing is done through specific parser objects like `ISO_8601` or `duration` that are imported from the module.fixInstead of `nr.parsing.date.parse('...')`, you should use `ISO_8601.parse('...')` or `duration.parse('...')` depending on what you intend to parse.
Warnings
- gotcha When installing `nr-date` via pip, the core functionalities for parsing dates and durations are typically found within the `nr.parsing.date` module, not directly under `nr_date`. Attempting `import nr_date` will likely result in a `ModuleNotFoundError`.
- gotcha As a regex-based parser, `nr-date` can be sensitive to the exact formatting of input strings. While flexible, unexpected input variations (e.g., missing optional separators or components) might lead to `None` being returned or incorrect parsing if not explicitly handled by a custom regex format.
Install
-
pip install nr-date
Imports
- ISO_8601
import nr_date
from nr.parsing.date import ISO_8601
- duration
import nr_date
from nr.parsing.date import duration
Quickstart
from nr.parsing.date import ISO_8601, duration
# Parse an ISO 8601 datetime string
datetime_obj = ISO_8601.parse('2021-04-21T10:13:00.124+0000')
print(f"Parsed datetime: {datetime_obj}")
# Parse an ISO 8601 duration string
duration_obj = duration.parse('P3Y6M4DT12H30M5S')
print(f"Parsed duration: {duration_obj}")