Skip to main content

A super flexible and performant date parser.

Project description

SuperDate: A super easy to use date parsing library.

Building and installation

Available from PyPi; Just pip3 install superdate

Alternatively, clone this repo and then pip3 install.

pip3 install .

Usage

superdate contains two main components; the parse_date function and SuperDate class.

The parse_date function allows for super intuitve date parsing, while the SuperDate class overloads comparison operators to allow for intuitive date comparisions against strings or date / datetime objects.

parse_date

The parse_date function parses dates from strings. These can be plain iso strings or coloquial enslish strings. Plain english strings are parsed with down to second precision using the parsedatetime library.

If a time was detected, then a datetime will be returned. If no time information was detected then a date object will be returned. A datetime can be forced to return with force_time=True.

The difference between this function and vanilla parsedatetime is a cache. Dates parsed from Non-ISO strings are cached for faster lookup. This cache clears if a new second has ticked over since the previous call.

# Some examples. Logged on Febuary 26th 2023.

>>> from superdate import parse_date

>>> parse_date('1970-1-1')
datetime.date(1970, 1, 1)

>>> parse_date('today')
datetime.date(2023, 2, 26)

# Force a time
>>> parse_date('today', force_time=True)
datetime.datetime(2023, 2, 26, 9, 0)

>>> parse_date('now')
datetime.datetime(2023, 2, 26, 14, 32)

>>> parse_date('saturday')
datetime.date(2023, 3, 4)

>>> parse_date('saturday at noon')
datetime.datetime(2023, 3, 4, 12, 0)

>>> parse_date('January')
datetime.date(2023, 1, 26)

>>> parse_date('January next year')
datetime.date(2024, 1, 1)

SuperDate

The SuperDate class wraps the parse_date function, but overloads all comparison operators to allow for more intuitive plain-english comparisons.

Comparisons between dates and datetimes will ignore the time. This is because a very common use case is, for example, a user who asks “what appointments are on wednesday?”

[a for a in appointments if a.date == 'Wednesday']

Typically “Wednesday” would include hour 0, minute 0, and second 0, which isn’t technically equivalent to an appointment at 4pm. Obviously this user wants to see all their appointments on Wednesday, so the time for each appointment will be ignored when comparing against a plain date.

# Some examples
from superdate import SuperDate

SuperDate('Wednesday') == 'Wednesday'  # => True
SuperDate('Wednesday') < 'Wednesday 4pm'  # => False
SuperDate('Wednesday') == 'Wednesday 4pm'  # => True
SuperDate('Wednesday at noon') < 'Wednesday 4pm'  # => True

# Will evaluate to True because we forced a time.
SuperDate('Wednesday', force_time=True) < 'Wednesday 4pm'  # => True

All dot operators (.hour .day .strftime etc…) are forwarded to the underlying python3 standard date / datetime ojbect, so this class should be usable anywhere a regular datetime is.

Maintenance and versioning

Update the CHANGELOG when and version in pyproject.toml when cutting a release.

Build with python3 -m build and use twine upload -r pypi dist/* to upload to pypi.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

superdate-0.2.0.tar.gz (12.0 kB view details)

Uploaded Source

Built Distribution

superdate-0.2.0-py3-none-any.whl (12.2 kB view details)

Uploaded Python 3

File details

Details for the file superdate-0.2.0.tar.gz.

File metadata

  • Download URL: superdate-0.2.0.tar.gz
  • Upload date:
  • Size: 12.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.6

File hashes

Hashes for superdate-0.2.0.tar.gz
Algorithm Hash digest
SHA256 73e4012cd4163e655fb1cfc706ac02c1ff8e94c9ad2496397b8e77651794c19a
MD5 105a8c7d08ce3a3d14c90748255ec256
BLAKE2b-256 59c47133655d281447c48c079fc28113bde7aac38d3f94680ea97d783b49bff0

See more details on using hashes here.

File details

Details for the file superdate-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: superdate-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 12.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.6

File hashes

Hashes for superdate-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ccaf0a3df84e24785fbff3176048bf34af30dd1f80a0244e2d58ab17ae3f6b14
MD5 e26775fe941f9aac13e2a06c97e493a7
BLAKE2b-256 8fb4c9d6606c3b9b37904244ef33a6a5a7f35ee6032a88a765b7e9519e2dedf9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page