Skip to main content

DateParser

qddate.DateParser holds the compiled pattern catalog and the indexes used to narrow candidates before pyparsing runs.

from qddate import DateParser

parser = DateParser()
parser = DateParser(languages=["en", "de"])
parser = DateParser(generate=True, base_only=False)

Constructor​

DateParser(generate=True, patterns=ALL_PATTERNS, base_only=False, languages=None,
pivot_year=None, tz_aware=False, use_fingerprint=True, date_order=None)
ParameterDefaultMeaning
generateTrueExpand each base date pattern with time-of-day and trailing-text variants
patternsqddate.patterns.ALL_PATTERNSPattern dicts to load
base_onlyFalseKeep only base patterns; skip generated "date plus extra text" variants
languagesNoneLanguage code, list of codes, or None for all languages. Czech is cz; cs is accepted too
pivot_yearNone4-digit pivot for two-digit years: with 1968, 99 → 1999 and 20 → 2020. None keeps the raw two-digit year. A bool or other non-integer raises TypeError: pivot_year=True would mean pivot year 1
tz_awareFalseAttach the zone written after the time to the result: +0300, +03:00, Z, GMT+3, UTC-05:30 or an abbreviation (MSK/МСК, CET/CEST, EET/EEST, BST, EST/EDT, PST/PDT, …; see qddate.patterns.TIMEZONE_ABBREVIATIONS)
use_fingerprintTrueUse the fingerprint candidate filter. False selects the legacy 6-level filter and emits DeprecationWarning; it will be removed in 2.0
date_orderNone"DMY", "MDY" or "YMD": how to read a numeric date several patterns read differently (05/06/2022). None keeps day first. Overridable per call

If languages is a non-empty string or list, the catalog is filtered with qddate.patterns.get_patterns_for_languages before generation. Empty list and None both mean "all languages". Invalid codes raise ValueError.

See languages= for allow-list behavior.

Custom pattern lists​

Advanced callers can pass a subset or a locally defined list:

from qddate.patterns import PATTERNS_EN, INTEGER_LIKE_PATTERNS

parser = DateParser(patterns=PATTERNS_EN + INTEGER_LIKE_PATTERNS)

The parser uses exactly those patterns. Each item is a dict with at least key, pattern (pyparsing grammar), length, and usually format.

Methods​

MethodReturnsNotes
parse(text, ...)datetime or NoneBest match as a datetime
match(text, ...)dict or NoneBest match as {"values", "pattern"}
match_all(text, ...)list[dict]Every matching pattern, longest match first, then priority; the first item is what match() returns
match_typed(text, **kwargs)DateMatch or NoneBest match as a typed object (below)
parse_many(texts, **kwargs)iterator of datetime or NoneOne parse() result per input, in order
parse_relative(text, reference=None)datetime or NoneRelative phrases (below)
format_date(dt, pattern_key)strFormats dt with the pattern's format string
start_session(keys) / end_session()NoneRestrict matching to the given pattern keys until the session ends

The keyword filters of parse() and match() (allow_no_year, noprefix, nocharsetfilter, ...) pass through match_typed() and parse_many().

Per-call options​

match(), match_all(), parse(), match_typed() and parse_many() also take these keyword-only options, so one parser can serve pages in different languages and locales:

OptionMethodsMeaning
languages=allLanguage code or list to try first: their patterns and the language-neutral numeric ones run first, the others only when none of them matches. Never excludes a pattern (unlike the constructor's languages=). The candidate index for each set of languages is built once and kept
date_order=all"DMY", "MDY" or "YMD" for this call; None uses the constructor's value
reference=parse, match_typedThe moment that counts as "now" for a date without a year (05.12, 4 Сентября): its year is used. Default: the current time
prefer=parse, match_typedFor a date without a year: "past" moves it to the previous year when it would lie more than qddate.qdparser.NOYEAR_ROLLOVER_GRACE (2 days) after reference; "future" to the next year when it would lie more than that before it. None keeps the reference year
parser.match("18 Juni 1953", languages="de")["pattern"]["key"] # 'dt:date:de_base' (nl_base without the hint)
parser.parse("05/13/2022", languages="ru") # datetime(2022, 5, 13): no Russian pattern reads it, English does
parser.parse("05/06/2022", date_order="MDY") # datetime(2022, 5, 6)
parser.parse("13/05/2022", date_order="MDY") # datetime(2022, 5, 13): not ambiguous
parser.parse("31.12", reference=datetime(2026, 1, 2), prefer="past") # datetime(2025, 12, 31)

date_order decides only between patterns that consume the same text; a longer match still wins. The month-first patterns are date_usa and date_usa_1 (slashes): dotted and dashed dates stay day first under "MDY". "YMD" prefers the year-first patterns (date_3, date_6, date_9, date_10), which matters for eight-digit dates such as 12101112.

parser.match_all("01/02/2020") # date_1 (day first) and date_usa (month first)
list(parser.parse_many(["01.12.2009", "junk"])) # [datetime(2009, 12, 1), None]

parser.start_session(["dt:date:date_eng1"])
parser.parse("01.12.2009") # None: outside the session
parser.parse("5 August 2001") # datetime(2001, 8, 5)
parser.end_session()

The camelCase startSession() / endSession() still work and emit DeprecationWarning.

Dates inside text: search() and find_all()​

match() and match_typed() read a date only at the start of the text. search() returns the first date wherever it starts, find_all() every date left to right; both return DateMatch objects whose span tells where the date is.

parser.search("Posted on May 5, 2022").span # (10, 21)
parser.search("Опубликовано: 5 мая 2022").datetime # datetime(2022, 5, 5, 0, 0)
[m.datetime for m in parser.find_all("From 5 May 2022 to 7 May 2022")]
# [datetime(2022, 5, 5, 0, 0), datetime(2022, 5, 7, 0, 0)]

A date is tried only where one can start: position 0, a run of one, two, four or eight digits that starts a word (not 200 in 1.200, not 2 in v2), or a month or weekday name. At each of them search() finds what match_typed() finds there, so a date at the start gives the same result. Text without a date stays cheap: a phone number's 495 is never tried.

Two options keep words and numbers from reading as dates:

ArgumentDefaultMeaning
max_offsetNoneA date must start at or before this index
labelsNoneTrue: a date that does not start the text (or follow the previous date) must follow a label of qddate.patterns.DATE_LABELS, a dateline or punctuation alone. A string or a list of strings: those labels instead of the built-in ones
parser.search("Some text 5 May 2022", labels=True) # None
parser.search("By Jane Doe | Updated: 5 May 2022", labels=True).span # (23, 33)
parser.search("GENEVA, 29 July 2022", labels=True).span # (8, 20): a dateline
parser.search("Stand der Daten 5. Mai 2022", labels=["Stand der Daten"])

DATE_LABELS holds short labels per language: posted on, published, updated, last updated, опубликовано, обновлено, veröffentlicht am, publié le, mis en ligne le, publicado el, pubblicato il, gepubliceerd op, opublikowano, publikováno, 发布时间, 公開日, 입력, …. A label is compared case-insensitively with the text after the last separator before the date (|, :, ,, ·, a spaced dash, brackets), with the punctuation after it removed, so Updated: and UPDATED match updated. A dateline is up to four words without digits followed by a comma or a spaced dash (GENEVA, , New York – ). Punctuation alone (• , (, – between two dates) is accepted; a symbol is not (+7 495 …, © 2022).

search() and find_all() take reference=, prefer= and the keyword arguments of match() (languages=, date_order=, allow_no_year=, the filter flags). With labels, the labels of every language are accepted whatever the languages= hint.

DateMatch​

match_typed(), search() and find_all() return qddate.DateMatch dataclasses:

m = parser.match_typed("6 Jan 2009")
m.datetime # datetime(2009, 1, 6, 0, 0); pivot_year / tz_aware already applied
m.pattern_key # "dt:date:date_eng1_short"
m.language # "en" (None for language-neutral numeric patterns; "cz" for Czech, even with languages="cs")
m.format # "%d.%b.%Y"
m.raw # "6 Jan 2009"
m.span # (0, 10): raw[start:end] is the date, raw[end:] the rest; start is 0 for match_typed()
m.precision # "day", "hour" ("3 PM"), "minute" or "second"
m.tzinfo # zone written after the time ("+03:00", "МСК") or None
m.has_year # False for "05.12": the year came from reference=
m.to_dict() # the same fields as a plain dict
m.format_date() # formats m.datetime with m.format

tzinfo is set whether or not the parser has tz_aware=True; only with it is m.datetime aware. An abbreviation's timezone carries its name (m.tzinfo.tzname(None) == "МСК"). qddate.qdparser.parse_timezone(text) converts a timezone value of match() the same way.

qddate.format_date(dt, pattern_key) is the module-level form of DateParser.format_date().

Instance reuse​

Keep one instance for the lifetime of the worker. Construction compiles grammars, builds length indexes, and stamps language/separator metadata onto generated variants.

Relative date parsing​

DateParser.parse_relative(text, reference=None) resolves common English and Russian relative-date phrases against a reference time. Default reference is datetime.now(). The result keeps the reference's time of day; only the calendar date moves.

from datetime import datetime
from qddate import DateParser

parser = DateParser()

# Reference is pinned for deterministic tests / scraping historic content.
ref = datetime(2026, 6, 15, 12, 0, 0)

parser.parse_relative("today", ref) # 2026-06-15 12:00:00
parser.parse_relative("yesterday", ref) # 2026-06-14 12:00:00
parser.parse_relative("3 days ago", ref) # 2026-06-12 12:00:00
parser.parse_relative("in 5 days", ref) # 2026-06-20 12:00:00
parser.parse_relative("two weeks ago", ref) # word-form numbers
parser.parse_relative("сегодня", ref) # Russian today
parser.parse_relative("3 дня назад", ref) # Russian 3 days ago
parser.parse_relative("через 5 дней", ref) # Russian in 5 days
parser.parse_relative("not a date", ref) # None

Supported phrases:

DirectionEnglishRussian
Anchorstoday, yesterday, tomorrowсегодня, вчера, завтра
PastN unit(s) agoN <unit> назад
Future(in) N unit(s)через N <unit>

Units: day(s) / days; week(s); month(s); year(s); and Russian день/дня/дней, неделю/недели/недель, месяц/месяца/месяцев, год/года/лет. Both digit-form (3) and word-form (three) numbers are accepted for English.

parse() does not match relative phrases. Call parse_relative() explicitly; there is no constructor switch that adds them to parse().