Skip to main content

fast-parse-time

PyPI version Python Version Downloads Downloads License: MIT Tests

fast-parse-time extracts calendar dates and relative time expressions from text. The calendar-date functions also accept a list of strings and can use an explicit date range to infer missing years. Parsing runs locally without network calls or a language model.

Install

Python 3.10 or later is required.

python -m pip install fast-parse-time

Find dates in a list

extract_date_strings returns the date text in the order it appears. Each list entry can contain a date, a sentence with several dates, or unrelated text. Matches retain their spelling and internal whitespace. Repeated dates remain in the result.

from fast_parse_time import extract_date_strings

texts = ['Term: 3/28/2011 to 4/22/2011', 'Mar 28', 'Apr 22', 'No classes']

assert extract_date_strings(texts) == [
    '3/28/2011', '4/22/2011', 'Mar 28', 'Apr 22',
]

Parse dates and infer missing years

parse_date_strings returns a record for each date occurrence. When at least two distinct full dates have unambiguous interpretations, the earliest and latest define an inclusive range. A date without a year is resolved only when that range supports a single calendar date.

from datetime import date
from fast_parse_time import parse_date_strings

matches = parse_date_strings(['3/28/2011', '4/22/2011', 'Mar 28', 'Apr 22'])

assert [match.date for match in matches] == [
    date(2011, 3, 28), date(2011, 4, 22),
    date(2011, 3, 28), date(2011, 4, 22),
]

The same inference works when the range crosses a year boundary. Both calendar functions accept a whole string as well as a list.

from datetime import date
from fast_parse_time import parse_date_strings

matches = parse_date_strings(
    'Term runs from 11/28/2025 to 4/14/2026; meetings on Nov 22 and March 13'
)

assert matches[-1].date == date(2026, 3, 13)
assert matches[-2].text == 'Nov 22'
assert matches[-2].year is None

March 13 occurs once inside that range, in 2026. November 22 falls outside it in both years, so its year remains unknown. The parser keeps the occurrence in the result rather than substituting the current year.

Use date_range=(start, end) to supply your own bounds. If several dates remain possible, .date is None and .candidates contains the alternatives. The calendar-date guide covers numeric date order, leap days, and unresolved results.

Extract explicit dates and relative expressions

parse_dates returns explicit date text with classification labels, along with relative expressions such as 5 days ago. Use parse_date_strings when you need calendar dates as Python date objects.

from datetime import datetime
from fast_parse_time import parse_dates

result = parse_dates('Meeting on 04/08/2024 about issues from 5 days ago')

assert result.explicit_dates[0].text == '04/08/2024'
assert result.explicit_dates[0].date_type == 'FULL_EXPLICIT_DATE'
assert result.relative_times[0].to_datetime(datetime(2026, 1, 1)) == datetime(2025, 12, 27)

You can also request relative offsets directly. Past references produce negative offsets; future references produce positive offsets.

from datetime import timedelta
from fast_parse_time import resolve_to_timedelta

assert resolve_to_timedelta('5 days ago') == [timedelta(days=-5)]
assert resolve_to_timedelta('half an hour ago') == [timedelta(minutes=-30)]
assert resolve_to_timedelta('tomorrow') == [timedelta(days=1)]

Choose a function

What you need Function Result
Date strings from prose or a list extract_date_strings list[str]
Calendar dates with range-based year inference parse_date_strings list[ParsedDate]
Explicit date classifications and relative references parse_dates ParseResult
Explicit date classifications, including year-only and month/year expressions extract_explicit_dates dict[str, str]
Relative expressions as quantities and units extract_relative_times list[RelativeTime]
Relative expressions resolved against a reference time resolve_to_datetime list[datetime]
Relative expressions as signed offsets resolve_to_timedelta list[timedelta]

Interpreting results

Numeric dates can have more than one interpretation. parse_date_strings preserves that ambiguity by default; use date_order='mdy' or 'dmy' when the source's convention is known. A classification such as FULL_EXPLICIT_DATE identifies a complete date expression without selecting its month/day order.

Keep unrelated periods in separate batches, or supply explicit bounds. Relative month and year offsets use 30-day months and 365-day years. Pass a fixed reference when resolving offsets if the result needs to be reproducible.

Documentation

License and author

The library is available under the MIT license. Craig Trim maintains it: craigtrim@gmail.com.

Report an issue.

Metadata

Release files for fast-parse-time 1.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fast-parse-time 1.5.0
File Size Uploaded
fast_parse_time-1.5.0.tar.gz 1.9 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for fast-parse-time 1.5.0
File Interpreter ABI Platform
fast_parse_time-1.5.0-py3-none-any.whl Python 3 none any Details

Total release size: 3.8 MB

Release files / fast_parse_time-1.5.0.tar.gz

Download URL fast_parse_time-1.5.0.tar.gz
Size 1.9 MB
Tags Source
SHA-256 checksum
How to use checksums
3ec2e4f295a2335dfbd37258acbe72b97c45ac052d6bb7a0835fde9bb14e8181
BLAKE2b-256 checksum
How to use checksums
fdd1a079993c57e63aafc9db60d9df860107d1da693963a03a5d231445e18adc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Release files / fast_parse_time-1.5.0-py3-none-any.whl

Download URL fast_parse_time-1.5.0-py3-none-any.whl
Size 1.9 MB
Tags Python 3
SHA-256 checksum
How to use checksums
9fadfb7fbb65b08727c8397580b626697cd8d22f25dea8891a5fcd56709dd73a
BLAKE2b-256 checksum
How to use checksums
b64d7ade48563f97313457637c1b64072ea6f23011342ec95b69f0dccf83c130
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Release history Release notifications | RSS feed

This release

1.5.0 This release

2 release files

1.3.2

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.1.15

2 release files

0.1.14

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page