Formatting utility for international postal addresses
Project description
Prefix date parser
This is a helper class to parse dates with varied degrees of precision. For
example, a data source might state a date as 2001
, 2001-4
or 2001-04-02
,
with the implication that only the year, month or day is known. This library
will process such partial dates into a structured format and allow their
validation and re-formatting (e.g. turning 2001-4
into 2001-04
above).
The library does not support the complexities of the ISO 8601 and RFC 3339 standards including date ranges and calendar-week/day-of-year notations.
Installation
Install prefixdate
using PyPI:
$ pip install prefixdate
Usage
The library provides a variety of helper functions to parse and format partial dates:
from prefixdate import parse, normalize_date, Precision
# Parse returns a `DatePrefix` object:
date = parse('2001-3')
assert date.text == '2001-03'
date = parse(2001)
assert date.text == '2001'
assert date.precision == Precision.YEAR
# Normalize to a standard string:
assert normalize_date('2001-1') == '2001-01'
assert normalize_date('2001-00-00') == '2001'
assert normalize_date('Boo!') is None
# This also works for datetimes:
from datetime import datetime
now = datetime.utcnow().isoformat()
minute = normalize_date(now, precision=Precision.MINUTE)
# You can also feed in None, date and datetime:
normalize_date(datetime.utcnow())
normalize_date(datetime.date())
normalize_date(None)
Caveats
- Does not process milliseconds yet.
- Does not process invalid dates, like Feb 31st.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for prefixdate-0.1.2-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e0908f5825a371d4e970581d9683da9cca8541ebfbdbae1770421e00692a6013 |
|
MD5 | 366eae42da953b3daa5912a6682e2fe0 |
|
BLAKE2b-256 | b4742d854358da2842a5ce74528c80144670d55e59e27e3a3ff4c39d27cc7212 |